/*jshint esversion: 6 */ (function(window, document) { "use strict"; // form labels often need unique IDs - this can be used to generate some window.Patcher_uniqueid = 0; var createID = function() { window.Patcher_uniqueid++; return "dllpatch_" + window.Patcher_uniqueid; }; var bytesMatch = function(buffer, offset, bytes) { for(var i = 0; i < bytes.length; i++) { if(buffer[offset+i] != bytes[i]) return false; } return true; }; var bytesToHex = function(bytes) { var s = '' for(var i = 0; i < bytes.length; i++) { s += bytes[i].toString(16).toUpperCase().padStart(2, '0'); } return s; } var hexToBytes = function(hex) { var bytes = []; for(var i = 0; i < hex.length; i += 2) { bytes.push(parseInt(hex.substr(i, 2), 16)); } return bytes; } var replace = function(buffer, offset, bytes) { for(var i = 0; i < bytes.length; i++) { buffer[offset+i] = bytes[i]; } }; var whichBytesMatch = function(buffer, offset, bytesArray) { for(var i = 0; i < bytesArray.length; i++) { if(bytesMatch(buffer, offset, bytesArray[i])) return i; } return -1; }; // shorthand functions var createElementClass = function(elName, className, textContent, innerHTML) { var el = document.createElement(elName); el.className = className || ''; el.textContent = textContent || ''; // optional // overrides textContent with HTML if provided if(innerHTML) { el.innerHTML = innerHTML; } return el; }; var createInput = function(type, id, className) { var el = document.createElement('input'); el.type = type; el.id = id; el.className = className || ''; return el; }; var createLabel = function(labelText, htmlFor, className) { var el = document.createElement('label'); el.textContent = labelText; el.htmlFor = htmlFor; el.className = className || ''; return el; }; // Each unique kind of patch should have createUI, validatePatch, applyPatch, // updateUI class StandardPatch { constructor(options) { this.name = options.name; this.patches = options.patches; this.tooltip = options.tooltip; this.danger = options.danger; } createUI(parent) { var id = createID(); var label = this.name; var patch = createElementClass('div', 'patch'); this.checkbox = createInput('checkbox', id); patch.appendChild(this.checkbox); patch.appendChild(createLabel(label, id)); if(this.tooltip) { patch.appendChild(createElementClass('div', 'tooltip', this.tooltip)); } if(this.danger) { patch.appendChild(createElementClass('div', 'danger tooltip', this.danger)); } parent.appendChild(patch); } updateUI(file) { this.checkbox.checked = this.checkPatchBytes(file) === "on"; } validatePatch(file) { var status = this.checkPatchBytes(file); if(status === "on") { console.log('"' + this.name + '"', "is enabled!"); } else if(status === "off") { console.log('"' + this.name + '"', "is disabled!"); } else { return '"' + this.name + '" is neither on nor off! Have you got the right file?'; } } applyPatch(file) { this.replaceAll(file, this.checkbox.checked); } replaceAll(file, featureOn) { for(var i = 0; i < this.patches.length; i++) { replace(file, this.patches[i].offset, featureOn? this.patches[i].on : this.patches[i].off); } } checkPatchBytes(file) { var patchStatus = ""; for(var i = 0; i < this.patches.length; i++) { var patch = this.patches[i]; if(bytesMatch(file, patch.offset, patch.off)) { if(patchStatus === "") { patchStatus = "off"; } else if(patchStatus != "off"){ return "on/off mismatch within patch"; } } else if(bytesMatch(file, patch.offset, patch.on)) { if(patchStatus === "") { patchStatus = "on"; } else if(patchStatus != "on"){ return "on/off mismatch within patch"; } } else { return "patch neither on nor off"; } } return patchStatus; } } class DynamicPatch { constructor(options) { this.name = options.name; this.patches = options.patches; this.tooltip = options.tooltip; this.danger = options.danger; this.mode = options.mode; this.target = options.target; } createUI(parent) { var id = createID(); var label = this.name; this.ui = createElementClass('div', 'patch'); this.checkbox = createInput('checkbox', id); this.ui.appendChild(this.checkbox); this.ui.appendChild(createLabel(label, id)); if(this.tooltip) { this.ui.appendChild(createElementClass('div', 'tooltip', this.tooltip)); } if(this.danger) { this.ui.appendChild(createElementClass('div', 'danger tooltip', this.danger)); } parent.appendChild(this.ui); } updateUI(file) { if (this.mode === 'all') { this.checkbox.checked = this.checkPatchAll(file, true) === "on"; } else { this.checkbox.checked = this.checkPatch(file, true) === "on"; } } validatePatch(file) { var status = this.mode === 'all' ? this.checkPatchAll(file) : this.checkPatch(file); if(status === "on") { console.log('"' + this.name + '"', "is enabled!"); } else if(status === "off") { console.log('"' + this.name + '"', "is disabled!"); } else { return '"' + this.name + '" is neither on nor off! Have you got the right file?'; } } applyPatch(file) { this.replaceAll(file, this.checkbox.checked); } replaceAll(file, featureOn) { for(let patch of this.patches) { if (Array.isArray(patch.offset)) { for(const offset of patch.offset) { if (this.target === 'string') { replace(file, offset, new TextEncoder().encode(featureOn? patch.on : patch.off)); } else { patch.on = patch.on.map((byte, idx) => byte === 'XX' ? file[offset + idx] : byte); patch.off = patch.off.map((byte, idx) => byte === 'XX' ? file[offset + idx] : byte); replace(file, offset, featureOn? patch.on : patch.off); } } } else { if (this.target === 'string') { replace(file, patch.offset, new TextEncoder().encode(featureOn? patch.on : patch.off)); } else { patch.on = patch.on.map((byte, idx) => byte === 'XX' ? file[patch.offset + idx] : byte); patch.off = patch.off.map((byte, idx) => byte === 'XX' ? file[patch.offset + idx] : byte); replace(file, patch.offset, featureOn? patch.on : patch.off); } } } } checkPatch(file, updateUiFlag = false) { var patchStatus = ""; let listUi; if (updateUiFlag) { listUi = document.createElement('ul'); this.ui.appendChild(listUi); } for(var i = 0; i < this.patches.length; i++) { var patch = this.patches[i]; var offOffset = this.searchPatchOffset(file, patch.off, i); var onOffset = this.searchPatchOffset(file, patch.on, i); this.patches[i].offset = offOffset === -1 ? onOffset : offOffset; if(offOffset > 0) { if (updateUiFlag) { if (this.target === 'string') { listUi.appendChild(createElementClass('li', 'patch-off', null, '0x' + offOffset.toString(16) + ' ' + patch.off + ' will be replaced with '+ patch.on +'')); } else { listUi.appendChild(createElementClass('li', 'patch-off', '0x' + offOffset.toString(16) + ' will be replaced')); } } if(patchStatus === "") { patchStatus = "off"; } } else if(onOffset > 0) { if (updateUiFlag) { if (this.target === 'string') { listUi.appendChild(createElementClass('li', 'patch-on', null, '0x' + onOffset.toString(16) + ' ' + patch.on + ' will be replaced with '+ patch.off +'')); } else { listUi.appendChild(createElementClass('li', 'patch-on', '0x' + onOffset.toString(16) + ' will be replaced')); } } if(patchStatus === "") { patchStatus = "on"; } } else if (this.mode === 'all') { continue; } else { return "patch string not found"; } } return patchStatus; } checkPatchAll(file, updateUiFlag = false) { var patchStatus = ""; let listUi; if (updateUiFlag) { listUi = document.createElement('ul'); this.ui.appendChild(listUi); } for(let patch of this.patches) { var offOffset = this.searchPatchOffsetAll(file, patch.off); var onOffset = this.searchPatchOffsetAll(file, patch.on); patch.offset = offOffset.length === 0 ? onOffset : offOffset; if(offOffset.length > 0) { if (updateUiFlag) { for(const offset of offOffset) { listUi.appendChild(createElementClass('li', 'patch-off', '0x' + offset.toString(16) + ' will be replaced')); } } if(patchStatus === "") { patchStatus = "off"; } } else if(onOffset.length > 0) { if (updateUiFlag) { for(const offset of onOffset) { listUi.appendChild(createElementClass('li', 'patch-on', '0x' + offset.toString(16) + ' will be replaced')); } } if(patchStatus === "") { patchStatus = "on"; } } else { return "patch string not found"; } } return patchStatus; } searchPatchOffset(file, search, offset) { let searchBytes; if (this.target === 'string') { searchBytes = new TextEncoder().encode(search); } else { searchBytes = search; } Uint8Array.prototype.indexOfArr = function(searchElements, fromIndex) { fromIndex = fromIndex || 0; var index = Array.prototype.indexOf.call(this, searchElements[0], fromIndex); if(searchElements.length === 1 || index === -1) { return { match: false, index: -1, }; } for(var i = index, j = 0; j < searchElements.length && i < this.length; i++, j++) { if (this.target !== 'string' && searchElements[j] === 'XX') { continue; } if(this[i] !== searchElements[j]) { return { match: false, index, }; } } return { match: true, index, }; }; var idx = 0; var foundCount = 0; for (var i = 0; i < file.length; i++) { var result = file.indexOfArr(searchBytes, idx); if (result.match) { if (offset === foundCount) { return result.index; } foundCount++; } else if (result.index === -1) { break; } idx = result.index + 1; } return -1; } searchPatchOffsetAll(file, search) { let searchBytes; if (this.target === 'string') { searchBytes = new TextEncoder().encode(search); } else { searchBytes = search; } Uint8Array.prototype.indexOfArr = function(searchElements, fromIndex) { fromIndex = fromIndex || 0; var index = Array.prototype.indexOf.call(this, searchElements[0], fromIndex); if(searchElements.length === 1 || index === -1) { return { match: false, index: -1, }; } for(var i = index, j = 0; j < searchElements.length && i < this.length; i++, j++) { if (this.target !== 'string' && searchElements[j] === 'XX') { continue; } if(this[i] !== searchElements[j]) { return { match: false, index, }; } } return { match: true, index, }; }; var idx = 0; var foundOffsetArray = []; for (var i = 0; i < file.length; i++) { var result = file.indexOfArr(searchBytes, idx); if (result.match) { foundOffsetArray.push(result.index); } else if (result.index === -1) { break; } idx = result.index + 1; } return foundOffsetArray; } } // Each unique kind of patch should have createUI, validatePatch, applyPatch, // updateUI // The DEFAULT state is always the 1st element in the patches array class UnionPatch { constructor(options) { this.name = options.name; this.offset = options.offset; this.patches = options.patches; this.tooltip = options.tooltip; this.danger = options.danger; } createUI(parent) { this.radios = []; var radio_id = createID(); var container = createElementClass('div', 'patch-union'); container.appendChild(createElementClass('span', 'patch-union-title', this.name + ':')); if(this.tooltip) { container.appendChild(createElementClass('div', 'tooltip', this.tooltip)); } if(this.danger) { container.appendChild(createElementClass('div', 'danger tooltip', this.danger)); } container.appendChild(document.createElement('span')); for(var i = 0; i < this.patches.length; i++) { var patch = this.patches[i]; var id = createID(); var label = patch.name; var patchDiv = createElementClass('div', 'patch'); var radio = createInput('radio', id); radio.name = radio_id; this.radios.push(radio); patchDiv.appendChild(radio); patchDiv.appendChild(createLabel(label, id)); if(patch.tooltip) { patchDiv.appendChild(createElementClass('div', 'tooltip', patch.tooltip)); } if(patch.danger) { patchDiv.appendChild(createElementClass('div', 'danger tooltip', patch.danger)); } container.appendChild(patchDiv); } parent.appendChild(container); } updateUI(file) { for(var i = 0; i < this.patches.length; i++) { if(bytesMatch(file, this.offset, this.patches[i].patch)) { this.radios[i].checked = true; return; } } // Default fallback this.radios[0].checked = true; } validatePatch(file) { for(var i = 0; i < this.patches.length; i++) { if(bytesMatch(file, this.offset, this.patches[i].patch)) { console.log(this.name, "has", this.patches[i].name, "enabled"); return; } } return '"' + this.name + '" doesn\'t have a valid patch! Have you got the right file?'; } applyPatch(file) { var patch = this.getSelected(); replace(file, this.offset, patch.patch); } getSelected() { for(var i = 0; i < this.patches.length; i++) { if(this.radios[i].checked) { return this.patches[i]; } } return null; } } // Each unique kind of patch should have createUI, validatePatch, applyPatch, // updateUI class NumberPatch { constructor(options) { this.name = options.name; this.tooltip = options.tooltip; this.danger = options.danger; this.offset = options.offset; this.size = options.size; this.min = options.min; this.max = options.max; this.littleEndian = options.littleEndian ?? true; if (![1, 2, 4].includes(this.size)) { throw new Error(`Unsupported number size: ${this.size}.`); } } createUI(parent) { var id = createID(); var label = this.name; var patch = createElementClass('div', 'patch'); patch.appendChild(createLabel(label, id)); this.number = createInput('number', id); this.number.style.width = "3rem"; if (this.min !== null) { this.number.min = this.min; } if (this.max) { this.number.max = this.max; } patch.appendChild(this.number); if (this.tooltip) { patch.appendChild(createElementClass('div', 'tooltip', this.tooltip)); } if (this.danger) { patch.appendChild(createElementClass('div', 'danger tooltip', this.danger)); } parent.appendChild(patch); } updateUI(file) { const arr = new Uint8Array(this.size); const view = new DataView(arr.buffer); for (var i = 0; i < this.size; i++) { arr[i] = file[this.offset + i]; } if (this.size === 1) { this.number.value = view.getInt8(0); } else if (this.size === 2) { this.number.value = view.getInt16(0, this.littleEndian); } else if (this.size === 4) { this.number.value = view.getInt32(0, this.littleEndian); } } validatePatch(file) { return; } applyPatch(file) { // Convert user inputted number to little endian const arr = new Uint8Array(this.size); const view = new DataView(arr.buffer); if (this.size === 1) { view.setInt8(0, this.number.value); } else if (this.size === 2) { view.setInt16(0, this.number.value, this.littleEndian); } else if (this.size === 4) { view.setInt32(0, this.number.value, this.littleEndian); } for (var i = 0; i < this.size; i++) { file[this.offset + i] = arr[i]; } } } // Each unique kind of patch should have createUI, validatePatch, applyPatch, // updateUI class HexPatch { constructor(options) { this.name = options.name; this.tooltip = options.tooltip; this.danger = options.danger; this.offset = options.offset; this.off = options.off; } createUI(parent) { this.radios = []; var radio_id = createID(); // Title of the radio option. var container = createElementClass('div', 'patch-union'); container.appendChild(createElementClass('span', 'patch-union-title', this.name + ':')); if(this.tooltip) { container.appendChild(createElementClass('div', 'tooltip', this.tooltip)); } if(this.danger) { container.appendChild(createElementClass('div', 'danger tooltip', this.danger)); } container.appendChild(document.createElement('span')); // Default option; tooltip shows default hex value. var id = createID(); var patchDiv = createElementClass('div', 'patch'); var radio = createInput('radio', id); radio.name = radio_id; this.radios.push(radio); patchDiv.appendChild(radio); patchDiv.appendChild(createLabel('Default', id)); patchDiv.appendChild(createElementClass('div', 'tooltip', 'Value ' + bytesToHex(this.off))); container.appendChild(patchDiv); // Custom option. id = createID(); patchDiv = createElementClass('div', 'patch'); radio = createInput('radio', id); radio.name = radio_id; this.radios.push(radio); patchDiv.appendChild(radio); patchDiv.appendChild(createLabel('Custom ' + this.off.length + '-byte hex value: ', id)); this.valueHex = document.createElement('input'); this.valueHex.type = 'text'; this.valueHex.id = id; patchDiv.appendChild(this.valueHex); patchDiv.appendChild(createElementClass('div', 'danger tooltip', 'Invalid values will not be applied.')); container.appendChild(patchDiv); parent.appendChild(container); } updateUI(file) { if(bytesMatch(file, this.offset, this.off)) { this.radios[0].checked = true; return; } this.valueHex.value = bytesToHex(file.slice(this.offset, this.offset + this.off.length)); this.radios[1].checked = true; } validatePatch(file) { if(bytesMatch(file, this.offset, this.off)) { console.log(this.name, "has default hex value"); return; } console.log(this.name, "has custom hex value"); } applyPatch(file) { if(this.radios[0].checked) { replace(file, this.offset, this.off); return; } if(this.radios[1].checked) { if(!this.valueHex.value.match(/^[0-9a-fA-F]+$/)) { alert('Patch "' + this.name + '" not applied - invalid hex!'); return; } if(this.valueHex.value.length != this.off.length * 2) { alert('Patch "' + this.name + '" not applied - invalid length!!'); return; } replace(file, this.offset, hexToBytes(this.valueHex.value)); return; } } } var loadPatch = function(_this, self, patcher) { patcher.loadPatchUI(); patcher.updatePatchUI(); patcher.container.style.display = ''; var successStr = patcher.filename; if (typeof _this.description === "string") { successStr += "(" + patcher.description + ")"; } self.successDiv.innerHTML = successStr + " loaded successfully!"; }; /** * Check if the input is a PE executable, and if it is, parses the PE * just enough to see if it's packed with HyperTech CrackProof, by * checking if the first DLL import is "KeRnEl32.dLl". * * If there was any error parsing the executable or if it was packed, * this function returns a string. If there were no errors, this returns * undefined. * * @param {Uint8Array} dllFile * @returns {string | undefined} */ var checkUnpackedExecutable = function(dllFile) { // Note to anyone looking: all offsets are absolute file offsets. const DOS_EXECUTABLE_MAGIC = [0x4D, 0x5A]; const PE_EXECUTABLE_MAGIC = [0x50, 0x45, 0x00, 0x00]; const PE_ROM_MAGIC = 0x107; const PE32_MAGIC = 0x10B; const PE32_PLUS_MAGIC = 0x20B; const OFFSET_IMAGE_OPTIONAL_HEADER32_NUMBER_OF_RVAS = 0x5C; const OFFSET_IMAGE_OPTIONAL_HEADER64_NUMBER_OF_RVAS = 0x6C; const SIZEOF_IMAGE_IMPORT_DESCRIPTOR = 0x28; const CRACKPROOF_KERNEL32_DLL = [ 0x4B, 0x65, 0x52, 0x6E, 0x45, 0x6C, 0x33, 0x32, 0x2E, 0x64, 0x4C, 0x6C, 0x00 ]; // "KeRnEl32.dLl\0" // We do have patchers for other types of files, like chu.acf, // so skip the check if we're not looking at a PE executable. if (!bytesMatch(dllFile, 0x00, DOS_EXECUTABLE_MAGIC)) { return undefined; } const dllFileView = new DataView(dllFile.buffer); const peHeaderOffset = dllFileView.getUint32(0x3C, true); // e_lfanew if (!bytesMatch(dllFile, peHeaderOffset, PE_EXECUTABLE_MAGIC)) { // Probably a file that accidentally looks like a DOS executable... return undefined; } const numberOfSections = dllFileView.getUint16(peHeaderOffset + 0x06, true); const sizeOfOptionalHeader = dllFileView.getUint16(peHeaderOffset + 0x14, true); const startOfOptionalHeader = peHeaderOffset + 0x18; const startOfSections = startOfOptionalHeader + sizeOfOptionalHeader; const rva2offset = (rva) => { for (let i = 0; i < numberOfSections; i++) { const sectionHeaderOffset = startOfSections + 0x28 * i; const virtualSize = dllFileView.getUint32(sectionHeaderOffset + 0x08, true); const virtualAddress = dllFileView.getUint32(sectionHeaderOffset + 0x0C, true); const pointerToRawData = dllFileView.getUint32(sectionHeaderOffset + 0x14, true); if (virtualAddress <= rva && (virtualAddress + virtualSize) > rva) { return rva - virtualAddress + pointerToRawData; } } return -1; }; const magic = dllFileView.getUint16(startOfOptionalHeader, true); if (![PE_ROM_MAGIC, PE32_MAGIC, PE32_PLUS_MAGIC].includes(magic)) { return `Unknown PE format ${magic}.`; } const numberOfRvaAndSizesOffset = startOfOptionalHeader + ( magic === PE32_PLUS_MAGIC ? OFFSET_IMAGE_OPTIONAL_HEADER64_NUMBER_OF_RVAS : OFFSET_IMAGE_OPTIONAL_HEADER32_NUMBER_OF_RVAS ); const numberOfRvaAndSizes = dllFileView.getUint32(numberOfRvaAndSizesOffset, true); // No import directory, apparently. Not our problem then. if (numberOfRvaAndSizes < 2) { return undefined; } const startOfDataDirectories = numberOfRvaAndSizesOffset + 0x04; // The import directory is the second directory (index 1), as can be seen in winnt.h: // #define IMAGE_DIRECTORY_ENTRY_IMPORT 1 const importDirectoryRva = dllFileView.getUint32(startOfDataDirectories + 0x08, true); const importDirectorySize = dllFileView.getUint32(startOfDataDirectories + 0x0C, true); // No imports, also not our problem. if (importDirectorySize < SIZEOF_IMAGE_IMPORT_DESCRIPTOR) { return undefined; } const importDirectoryOffset = rva2offset(importDirectoryRva); if (importDirectoryOffset === -1) { return "Could not convert import directory RVA to offset."; } const importDllNameRva = dllFileView.getUint32(importDirectoryOffset + 12, true); const importDllNameOffset = rva2offset(importDllNameRva); if (importDllNameOffset === -1) { return "Could not convert import DLL name RVA to offset."; } if (bytesMatch(dllFile, importDllNameOffset, CRACKPROOF_KERNEL32_DLL)) { return "This executable is packed with CrackProof. Please obtain an unpacked executable."; } return undefined; } class PatchContainer { constructor(patchers) { this.patchers = patchers; this.createUI(); } getSupportedDLLs() { var dlls = []; for (var i = 0; i < this.patchers.length; i++) { var name = this.patchers[i].filename; if (dlls.indexOf(name) === -1) { dlls.push(name); } } return dlls; } createUI() { var self = this; var container = createElementClass('div', 'patchContainer'); var header = this.getSupportedDLLs().join(", "); container.innerHTML = "