Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <string.h>
- #include <windows.h>
- #include <valarray>
- #include <memoryapi.h>
- #include <tuple>
- #include <bitset>
- #include <algorithm>
- #include <vector>
- #include <tlhelp32.h>
- #include <unicorn.h>
- #include <x86.h>
- #include <inttypes.h>
- #include <Zydis/Zydis.h>
- using namespace std;
- #pragma section(".DENUVO",read,write)
- __declspec(allocate(".DENUVO"))
- byte MassiveOfOpcodes[0x6000][0x6000];
- #pragma section(".NTDLL",read,write)
- __declspec(allocate(".NTDLL"))
- byte NtdllOpcodes[0x450][0x450];
- #pragma section(".MSVCRT",read,write)
- __declspec(allocate(".MSVCRT"))
- byte MSVCRTOpcodes[0x400][0x400];
- #define UC_EMU_START_ADDRESS 0x15A79131D
- int64_t ADDRESS_TEXT_SECTION_NTDLL, ADDRESS_TEXT_SECTION_MSVCRT;
- void remove_cache(uc_engine* uc, uint64_t address)
- {
- uc_tb tb;
- uc_err err;
- err = uc_ctl_request_cache(uc, address, &tb);
- //printf(">>> TB is cached at 0x%" PRIx64 " which has %" PRIu16" instructions with %" PRIu16 " bytes.\n", tb.pc, tb.icount, tb.size);
- if (err)
- {
- printf("Failed on uc_ctl_request_cache() with error returned %u: %s\n",
- err, uc_strerror(err));
- }
- //printf(">>> removing cache from 0x%" PRIx64 " to 0x%" PRIx64 ".\n", tb.pc, tb.pc + tb.size);
- err = uc_ctl_remove_cache(uc, tb.pc, tb.pc + tb.size);
- if (err)
- {
- printf("Failed on uc_ctl_remove_cache() with error returned %u: %s\n",
- err, uc_strerror(err));
- }
- }
- int XorToMov(uc_engine* uc, uint64_t address, int S_I_Z_E, ZydisEncoderRequest pattern_req, ZydisDisassembledInstruction instruction, uint64_t RIP)
- {
- const int NOP_data[24] = { 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90 };
- pattern_req.mnemonic = ZYDIS_MNEMONIC_MOV;
- pattern_req.machine_mode = ZYDIS_MACHINE_MODE_LONG_64;
- pattern_req.operand_count = 2;
- pattern_req.address_size_hint = ZYDIS_ADDRESS_SIZE_HINT_64;
- pattern_req.operand_size_hint = ZYDIS_OPERAND_SIZE_HINT_64;
- pattern_req.allowed_encodings = ZYDIS_ENCODABLE_ENCODING_LEGACY;
- pattern_req.operands[0].type = instruction.operands[0].type;
- pattern_req.operands[0].reg.value = instruction.operands[0].reg.value;
- pattern_req.operands[1].type = instruction.operands[1].type;
- pattern_req.operands[1].mem.base = instruction.operands[1].mem.base;
- pattern_req.operands[1].mem.displacement = instruction.operands[1].mem.disp.value;
- pattern_req.operands[1].mem.size = S_I_Z_E;
- pattern_req.operands[1].mem.scale = instruction.operands[1].mem.scale;
- ZyanU8 encoded_instruction[ZYDIS_MAX_INSTRUCTION_LENGTH];
- ZyanUSize encoded_length = sizeof(encoded_instruction);
- ZydisEncoderEncodeInstruction(&pattern_req, encoded_instruction, &encoded_length);
- for (int i = 0; i < instruction.info.length; ++i)
- {
- uc_mem_write(uc, RIP + i, &NOP_data[i], 1);
- }
- for (ZyanUSize i = 0; i < encoded_length; ++i)
- {
- uc_mem_write(uc, RIP + i, &encoded_instruction[i], 1);//5
- printf("%02X ", encoded_instruction[i]);
- }
- std::cout << endl;
- remove_cache(uc, address);
- uc_reg_write(uc, UC_X86_REG_RIP, &address);
- return 0;
- }
- static void ZYDIS_DISASM(uc_engine* uc, uint64_t address, uint32_t size, uint64_t RIP)
- {
- ZyanU8 NOP_data[24] = {0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
- ZyanU8 data[16];
- int TF, TS, TT, TFOURTH, TFIFTH, TSIXTH;
- ZydisRegister_ REG_1, REG_2, REG_3, REG_4, REG_5;
- ZyanISize C_0, C_1, C_2, C_3, C_4;
- int X64_REGS[] = { UC_X86_REG_RAX, UC_X86_REG_RBX, UC_X86_REG_RCX,
- UC_X86_REG_RDX, UC_X86_REG_RSI, UC_X86_REG_RDI,
- UC_X86_REG_R8, UC_X86_REG_R9, UC_X86_REG_R10,
- UC_X86_REG_R11, UC_X86_REG_R12, UC_X86_REG_R13,
- UC_X86_REG_R14, UC_X86_REG_R15};
- int X08_REGS[] = { UC_X86_REG_AH, UC_X86_REG_AL, UC_X86_REG_BH, UC_X86_REG_BL,
- UC_X86_REG_CH, UC_X86_REG_CL, UC_X86_REG_DH, UC_X86_REG_DL,
- UC_X86_REG_SIL, UC_X86_REG_DIL, UC_X86_REG_R8B, UC_X86_REG_R9B,
- UC_X86_REG_R10B,UC_X86_REG_R11B,UC_X86_REG_R12B,
- UC_X86_REG_R13B,UC_X86_REG_R14B,UC_X86_REG_R15B};
- ZydisRegister_ X64_ZYDIS_REGS[] = { ZYDIS_REGISTER_RAX, ZYDIS_REGISTER_RBX, ZYDIS_REGISTER_RCX,
- ZYDIS_REGISTER_RDX, ZYDIS_REGISTER_RSI, ZYDIS_REGISTER_RDI,
- ZYDIS_REGISTER_R8, ZYDIS_REGISTER_R9, ZYDIS_REGISTER_R10,
- ZYDIS_REGISTER_R11, ZYDIS_REGISTER_R12, ZYDIS_REGISTER_R13,
- ZYDIS_REGISTER_R14, ZYDIS_REGISTER_R15};
- ZydisRegister_ X32_ZYDIS_REGS[] = { ZYDIS_REGISTER_EAX, ZYDIS_REGISTER_EBX, ZYDIS_REGISTER_ECX, ZYDIS_REGISTER_EDX,
- ZYDIS_REGISTER_ESI, ZYDIS_REGISTER_EDI, ZYDIS_REGISTER_R8D, ZYDIS_REGISTER_R9D,
- ZYDIS_REGISTER_R10D, ZYDIS_REGISTER_R11D, ZYDIS_REGISTER_R12D, ZYDIS_REGISTER_R13D,
- ZYDIS_REGISTER_R14D, ZYDIS_REGISTER_R15D };
- ZydisRegister_ X16_ZYDIS_REGS[] = { ZYDIS_REGISTER_AX, ZYDIS_REGISTER_BX, ZYDIS_REGISTER_CX, ZYDIS_REGISTER_DX,
- ZYDIS_REGISTER_SI, ZYDIS_REGISTER_DI, ZYDIS_REGISTER_R8W, ZYDIS_REGISTER_R9W,
- ZYDIS_REGISTER_R10W, ZYDIS_REGISTER_R11W,ZYDIS_REGISTER_R12W, ZYDIS_REGISTER_R13W,
- ZYDIS_REGISTER_R14W, ZYDIS_REGISTER_R15W };
- ZydisRegister_ X08_ZYDIS_REGS[] = { ZYDIS_REGISTER_AH, ZYDIS_REGISTER_AL, ZYDIS_REGISTER_BH, ZYDIS_REGISTER_BL,
- ZYDIS_REGISTER_CH, ZYDIS_REGISTER_CL, ZYDIS_REGISTER_DH, ZYDIS_REGISTER_DL,
- ZYDIS_REGISTER_SIL, ZYDIS_REGISTER_DIL, ZYDIS_REGISTER_R8B, ZYDIS_REGISTER_R9B,
- ZYDIS_REGISTER_R10B, ZYDIS_REGISTER_R11B, ZYDIS_REGISTER_R12B, ZYDIS_REGISTER_R13B,
- ZYDIS_REGISTER_R14B, ZYDIS_REGISTER_R15B };
- int64_t X64_REGS_PTR[14];
- BYTE X08_REGS_PTR[18];
- ZydisRegister_ SEG_REG_1;
- uc_err err_refresh;
- ZydisDecoder decoder;
- ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
- ZydisEncoderRequest pattern_req;
- memset(&pattern_req, 0, sizeof(pattern_req));
- ZyanU64 runtime_address = RIP;
- ZyanUSize offset = 0;
- const ZyanUSize length = sizeof(data);
- ZydisDisassembledInstruction instruction;
- ZydisDecodedInstruction Instruction;
- ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
- uc_mem_read(uc, address, data, size);
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- size = sizeof(data);
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_PUSH)
- {
- uc_mem_read(uc, address+instruction.info.length, data, size);
- TF = instruction.info.length;
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_LEA && instruction.operands[1].mem.index == ZYDIS_REGISTER_NONE && instruction.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY)
- {
- ZydisDecoderDecodeFull(&decoder, data + offset, length - offset,
- &Instruction, operands);
- ZyanU64 result_address;
- ZydisCalcAbsoluteAddress(&Instruction, &operands[1], runtime_address, &result_address);
- pattern_req.operand_count = 1;
- pattern_req.operands[0].type = ZYDIS_OPERAND_TYPE_IMMEDIATE;//ZYDIS_OPERAND_TYPE_MEMORY
- pattern_req.operands[0].imm.u = result_address;
- uc_mem_read(uc, address + instruction.info.length+TF, data, size);
- TS = instruction.info.length + TF;
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_XCHG && instruction.operands[0].mem.base == ZYDIS_REGISTER_RSP)
- {
- uc_mem_read(uc, address + instruction.info.length + TS, data, size);
- TT = instruction.info.length + TS;
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_RET)
- {
- pattern_req.mnemonic = ZYDIS_MNEMONIC_JMP;
- pattern_req.machine_mode = ZYDIS_MACHINE_MODE_LONG_64;
- ZyanU8 encoded_instruction[ZYDIS_MAX_INSTRUCTION_LENGTH];
- ZyanUSize encoded_length = sizeof(encoded_instruction);
- ZydisEncoderEncodeInstructionAbsolute(&pattern_req, encoded_instruction, &encoded_length, runtime_address);
- for (ZyanUSize i = 0; i < TT+instruction.info.length; ++i)
- {
- uc_mem_write(uc, RIP + i, &NOP_data[i], 1);
- }
- for (ZyanUSize i = 0; i < encoded_length; ++i)
- {
- uc_mem_write(uc, RIP + 1+i, &encoded_instruction[i], 5);
- printf("%02X ", encoded_instruction[i]);
- }
- puts("");
- remove_cache(uc, address);
- uc_reg_write(uc, UC_X86_REG_RIP, &address);
- }
- }
- else if (instruction.info.mnemonic == ZYDIS_MNEMONIC_LEA)
- {
- uc_mem_read(uc, address + TF, data, size);
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- TS = TF + instruction.info.length;
- REG_1 = instruction.operands[0].reg.value;
- pattern_req.operands[0].type = instruction.operands[1].type;
- pattern_req.operands[0].mem.base = instruction.operands[1].mem.base;
- // pattern_req.operands[0].mem.displacement = instruction.operands[1].mem.disp.value;
- pattern_req.operands[0].mem.size = 0x8;
- pattern_req.operands[0].mem.scale = instruction.operands[1].mem.scale;
- uc_mem_read(uc, address + TS, data, size);
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_LEA && instruction.operands[0].reg.value == REG_1)//&& instruction.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY
- {
- TT = TS + instruction.info.length;
- uc_mem_read(uc, address + TT, data, size);
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_MOV && instruction.operands[0].mem.segment == ZYDIS_REGISTER_DS)
- {
- TFOURTH = instruction.info.length + TT;
- pattern_req.mnemonic = ZYDIS_MNEMONIC_MOV;
- pattern_req.machine_mode = ZYDIS_MACHINE_MODE_LONG_64;
- pattern_req.operand_count = 2;
- pattern_req.address_size_hint = ZYDIS_ADDRESS_SIZE_HINT_64;
- pattern_req.operand_size_hint = ZYDIS_OPERAND_SIZE_HINT_64;
- pattern_req.allowed_encodings = ZYDIS_ENCODABLE_ENCODING_LEGACY;
- pattern_req.operands[1].type = instruction.operands[1].type;
- pattern_req.operands[1].reg.value = instruction.operands[1].reg.value;
- ZyanU8 encoded_instruction[ZYDIS_MAX_INSTRUCTION_LENGTH];
- ZyanUSize encoded_length = sizeof(encoded_instruction);
- ZydisEncoderEncodeInstruction(&pattern_req, encoded_instruction, &encoded_length);
- for (ZyanUSize i = 0; i < TFOURTH; ++i)
- {
- uc_mem_write(uc, RIP + i, &NOP_data[i], 1);
- }
- for (C_0 = 0; C_0 < encoded_length; ++C_0)
- {
- uc_mem_write(uc, RIP + C_0, &encoded_instruction[C_0], 1);//5
- printf("%02X ", encoded_instruction[C_0]);
- }
- uc_mem_read(uc, address, data, size);
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- pattern_req.mnemonic = ZYDIS_MNEMONIC_PUSH;
- pattern_req.machine_mode = ZYDIS_MACHINE_MODE_LONG_64;
- pattern_req.operand_count = 1;
- pattern_req.operands[0].type = instruction.operands[0].type;
- pattern_req.operands[0].reg.value = instruction.operands[0].reg.value;
- for (C_1 = 0; C_1 < encoded_length; ++C_1)
- {
- uc_mem_write(uc, RIP + C_0 + C_1, &encoded_instruction[C_1], 1);//5
- printf("%02X ", encoded_instruction[C_1]);
- }
- std::cout << endl;
- remove_cache(uc, address);
- uc_reg_write(uc, UC_X86_REG_RIP, &address);
- }
- }
- }
- }
- else if (instruction.info.mnemonic == ZYDIS_MNEMONIC_MOV && instruction.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY)
- {
- TS = TF + instruction.info.length;
- uc_mem_read(uc, address + TS, data, size);
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_MOV)
- {
- pattern_req.operands[1].type = instruction.operands[1].type;
- pattern_req.operands[1].reg.value = instruction.operands[1].reg.value;
- TT = TS + instruction.info.length;
- uc_mem_read(uc, address + TT, data, size);
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_MOV && instruction.operands[0].type == ZYDIS_OPERAND_TYPE_MEMORY)
- {
- pattern_req.operands[0].type = instruction.operands[0].type;
- pattern_req.operands[0].mem.base = instruction.operands[0].mem.base;
- // pattern_req.operands[0].mem.displacement = instruction.operands[0].mem.disp.value;
- pattern_req.operands[0].mem.size = 0x8;
- pattern_req.operands[0].mem.scale = instruction.operands[0].mem.scale;
- TFOURTH = TT + instruction.info.length;
- uc_mem_read(uc, address + TFOURTH, data, size);
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_MOV && instruction.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY)
- {
- TFIFTH = TFOURTH + instruction.info.length;
- uc_mem_read(uc, address + TFIFTH, data, size);
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_MOV && instruction.operands[0].type == ZYDIS_OPERAND_TYPE_MEMORY)
- {
- TSIXTH = TFIFTH + instruction.info.length;
- pattern_req.mnemonic = ZYDIS_MNEMONIC_MOV;
- pattern_req.machine_mode = ZYDIS_MACHINE_MODE_LONG_64;
- pattern_req.operand_count = 2;
- pattern_req.address_size_hint = ZYDIS_ADDRESS_SIZE_HINT_64;
- pattern_req.operand_size_hint = ZYDIS_OPERAND_SIZE_HINT_64;
- pattern_req.allowed_encodings = ZYDIS_ENCODABLE_ENCODING_LEGACY;
- ZyanU8 encoded_instruction[ZYDIS_MAX_INSTRUCTION_LENGTH];
- ZyanUSize encoded_length = sizeof(encoded_instruction);
- ZydisEncoderEncodeInstruction(&pattern_req, encoded_instruction, &encoded_length);
- for (ZyanISize i = 0; i < TSIXTH; ++i)
- {
- uc_mem_write(uc, RIP + i, &NOP_data[i], 1);
- }
- for (C_0 = 0; C_0 < encoded_length; ++C_0)
- {
- uc_mem_write(uc, RIP + C_0, &encoded_instruction[C_0], 1);//5
- printf("%02X ", encoded_instruction[C_0]);
- }
- pattern_req.mnemonic = ZYDIS_MNEMONIC_PUSH;
- pattern_req.machine_mode = ZYDIS_MACHINE_MODE_LONG_64;
- pattern_req.operand_count = 1;
- pattern_req.operands[0].type = ZYDIS_OPERAND_TYPE_REGISTER;
- pattern_req.operands[0].reg.value = instruction.operands[1].reg.value;
- encoded_instruction[ZYDIS_MAX_INSTRUCTION_LENGTH];
- encoded_length = sizeof(encoded_instruction);
- ZydisEncoderEncodeInstruction(&pattern_req, encoded_instruction, &encoded_length);
- for (C_1 = 0; C_1 < encoded_length; ++C_1)
- {
- uc_mem_write(uc, RIP + C_0 + C_1, &encoded_instruction[C_1], 1);//5
- printf("%02X ", encoded_instruction[C_1]);
- }
- std::cout << endl;
- remove_cache(uc, address);
- uc_reg_write(uc, UC_X86_REG_RIP, &address);
- }
- }
- }
- }
- }
- }
- /*
- else if (instruction.info.mnemonic == ZYDIS_MNEMONIC_PUSH)
- {
- uc_mem_read(uc, address + instruction.info.length, data, size);
- TF = instruction.info.length;
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_LEA)
- {
- TS = instruction.info.length+TF;
- REG_1 = instruction.operands[0].reg.value;
- pattern_req.operands[0].type = instruction.operands[1].type;
- pattern_req.operands[0].mem.base = instruction.operands[1].mem.base;
- // pattern_req.operands[0].mem.displacement = instruction.operands[1].mem.disp.value;
- pattern_req.operands[0].mem.size = 0x8;
- pattern_req.operands[0].mem.scale = instruction.operands[1].mem.scale;
- uc_mem_read(uc, address + instruction.info.length, data, size);
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_LEA && instruction.operands[0].reg.value == REG_1)//&& instruction.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY
- {
- TT = instruction.info.length + TS;
- uc_mem_read(uc, address + instruction.info.length + TS, data, size);
- ZYAN_SUCCESS(ZydisDisassembleIntel(ZYDIS_MACHINE_MODE_LONG_64, runtime_address, data + offset, length - offset,
- &instruction));
- if (instruction.info.mnemonic == ZYDIS_MNEMONIC_MOV && instruction.operands[0].mem.segment == ZYDIS_REGISTER_DS)
- {
- // TT = instruction.info.length + TS;
- pattern_req.mnemonic = ZYDIS_MNEMONIC_MOV;
- pattern_req.machine_mode = ZYDIS_MACHINE_MODE_LONG_64;
- pattern_req.operand_count = 2;
- pattern_req.address_size_hint = ZYDIS_ADDRESS_SIZE_HINT_64;
- pattern_req.operand_size_hint = ZYDIS_OPERAND_SIZE_HINT_64;
- pattern_req.allowed_encodings = ZYDIS_ENCODABLE_ENCODING_LEGACY;
- pattern_req.operands[1].type = instruction.operands[1].type;
- pattern_req.operands[1].reg.value = instruction.operands[1].reg.value;
- ZyanU8 encoded_instruction[ZYDIS_MAX_INSTRUCTION_LENGTH];
- ZyanUSize encoded_length = sizeof(encoded_instruction);
- ZydisEncoderEncodeInstruction(&pattern_req, encoded_instruction, &encoded_length);
- for (ZyanUSize i = 0; i < TT + instruction.info.length; ++i)
- {
- uc_mem_write(uc, RIP + i, &NOP_data[i], 1);
- }
- for (ZyanUSize i = 0; i < encoded_length; ++i)
- {
- uc_mem_write(uc, RIP + i, &encoded_instruction[i], 1);//5
- printf("%02X ", encoded_instruction[i]);
- }
- remove_cache(uc, address);
- uc_reg_write(uc, UC_X86_REG_RIP, &address);
- }
- }
- }
- }
- */
- else if (instruction.info.mnemonic == ZYDIS_MNEMONIC_XOR && instruction.operands[0].reg.value != instruction.operands[1].reg.value)
- {
- if (instruction.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && instruction.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY)
- {
- for (int i = 0; i <= 14; ++i)
- {
- uc_reg_read(uc, X64_REGS[i], &X64_REGS_PTR[i]);
- if (X64_REGS_PTR[i] == 0)
- {
- if (instruction.operands[0].reg.value == X64_ZYDIS_REGS[i])
- {
- cout << "X64" << endl;
- cout << instruction.operands[0].reg.value << endl;
- int S_I_Z_E = 0x8;
- XorToMov(uc, address, S_I_Z_E, pattern_req, instruction, RIP);
- }
- else if (instruction.operands[0].reg.value == X32_ZYDIS_REGS[i])
- {
- cout << "X32" << endl;
- int S_I_Z_E = 0x4;
- XorToMov(uc, address, S_I_Z_E, pattern_req, instruction, RIP);
- }
- else if (instruction.operands[0].reg.value == X16_ZYDIS_REGS[i])
- {
- cout << "X16" << endl;
- int S_I_Z_E = 0x2;
- XorToMov(uc, address, S_I_Z_E, pattern_req, instruction, RIP);
- }
- }
- }
- for (int i = 0; i <= 17; ++i)
- {
- uc_reg_read(uc, X08_REGS[i], &X08_REGS_PTR[i]);
- if (X08_REGS_PTR[i] == 0)
- {
- if (instruction.operands[0].reg.value == X08_ZYDIS_REGS[i])
- {
- cout << "X08" << endl;
- int S_I_Z_E = 0x1;
- XorToMov(uc, address, S_I_Z_E, pattern_req, instruction, RIP);
- }
- }
- }
- }
- else if (instruction.operands[0].type == ZYDIS_OPERAND_TYPE_MEMORY)
- {
- std::cout << "LATER; XOR MEMORY" << endl;
- }
- else if (instruction.operands[0].type == ZYDIS_OPERAND_TYPE_POINTER)
- {
- std::cout << "LATER; XOR POINTER" << endl;
- }
- }
- }
- static void PatternDeobfuscation(uc_engine* uc, uint64_t address,uint32_t size, uint64_t RIP)
- {
- byte tmp[18];
- int XCGH_RET_PATTERN[] = { 0x00, 0x00, 0x48, 0x8D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x87, 0x00, 0x24, 0xC3 }; //PUSH ?? | LEA ??, QWORD PTR DS : [VA - ??] | XCHG QWORD PTR SS : [RSP] , ?? | RET
- int PUSH_R_X[] = { 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 };//PUSH R_X && R_P
- int PUSH_R_N[] = { 0x41, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 };//PUSH R_N
- if (!uc_mem_read(uc, address, tmp, size)) {
- ZYDIS_DISASM(uc, address, size, (uint64_t)RIP);
- uc_mem_read(uc, address, tmp, size);
- std::cout << ">>> OPCODES =";
- for (int i = 0; i < size; i++)
- {
- std::cout << " " << hex << (int)tmp[i];
- }
- std::cout << endl << endl;
- }
- }
- static void hook_code(uc_engine* uc, uint64_t address,
- uint32_t size, void* RIP)
- {
- uc_reg_read(uc, UC_X86_REG_RIP, &RIP);
- std::cout << ">>> RIP = " << hex << RIP << endl;
- std::cout << ">>> SIZE = " << size << endl;
- PatternDeobfuscation(uc, address, size, (uint64_t)RIP);
- }
- int Create_Read_Process()
- {
- char CommandLine[] = "cmd.exe /c G:/Programs/Games/JustCause4/JustCause4/JustCause4.exe";
- char ERROR_IMAGEBASE[] = "IMAGEBASE ERROR";
- char ERROR_CREATEPROCESS[] = "CREATE PROCESS ERROR";
- const char szModuleName[] = "JustCause4.exe";
- wchar_t text_1[220];
- wchar_t text[220];
- int Result_Of_New_Proc;
- long long int BaseAddress_tls_section = 0x0000000142E7A000;
- long long int BaseAddress_text_section = 0x00000015B285000;
- long long int NumberOfBytesRead_tls_section = 0x183A6000;
- long long int NumberOfBytesRead_text_section = 0x8000;
- long long int NumberOfBytesRead_text_section_ntdll = 0x119000;
- long long int NumberOfBytesRead_text_section_msvcrt = 0x75000;
- long long int NumberOfBytesWrite = 0x4;
- HANDLE snapshot, HANDLE_OF_CHILD_PROC, ImageBase, HANDLE_OF_MY_PROC;
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- PROCESSENTRY32 entry;
- entry.dwSize = sizeof(PROCESSENTRY32);
- //////////////////////////////////////////////////////////////// NTDLL
- FARPROC ADDRESS_OF_FUNCTION_IN_TEXT_SECTION_NTDLL = 0;
- // int64_t ADDRESS_TEXT_SECTION_NTDLL = 0;
- char NAME_OF_NTDLL[] = "ntdll.dll";
- char NAME_OF_THE_FIRST_FUNCTION_IN_NTDLL[] = "RtlLargeIntegerToChar";
- HMODULE HANDLE_OF_NTDLL;
- //////////////////////////////////////////////////////////////// NTDLL
- //////////////////////////////////////////////////////////////// MSVCRT
- FARPROC ADDRESS_OF_FUNCTION_IN_TEXT_SECTION_MSVCRT = 0;
- // int64_t ADDRESS_TEXT_SECTION_MSVCRT = 0;
- char NAME_OF_MSVCRT_DLL[] = "msvcrt.dll";
- char NAME_OF_THE_FIRST_FUNCTION_IN_MSVCRT[] = "__STRINGTOLD";
- HMODULE HANDLE_OF_MSVCRT;
- //////////////////////////////////////////////////////////////// MSVCRT
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
- ZeroMemory(&pi, sizeof(pi));
- HANDLE_OF_NTDLL = LoadLibraryA((LPCSTR)NAME_OF_NTDLL);
- if (HANDLE_OF_NTDLL == 0)
- {
- std::cout << "ERROR_OF_HANDLE_OF_NTDLL" << endl;
- return -1;
- }
- ADDRESS_OF_FUNCTION_IN_TEXT_SECTION_NTDLL = GetProcAddress(HANDLE_OF_NTDLL, LPCSTR(NAME_OF_THE_FIRST_FUNCTION_IN_NTDLL));
- ADDRESS_TEXT_SECTION_NTDLL = (int64_t)ADDRESS_OF_FUNCTION_IN_TEXT_SECTION_NTDLL - 0x10;
- CloseHandle(HANDLE_OF_NTDLL);
- HANDLE_OF_MSVCRT = LoadLibraryA((LPCSTR)NAME_OF_MSVCRT_DLL);
- if (HANDLE_OF_MSVCRT == 0)
- {
- std::cout << "ERROR_OF_HANDLE_OF_MSVCRT" << endl;
- return -1;
- }
- ADDRESS_OF_FUNCTION_IN_TEXT_SECTION_MSVCRT = GetProcAddress(HANDLE_OF_MSVCRT, LPCSTR(NAME_OF_THE_FIRST_FUNCTION_IN_MSVCRT));
- ADDRESS_TEXT_SECTION_MSVCRT = (int64_t)ADDRESS_OF_FUNCTION_IN_TEXT_SECTION_MSVCRT - 0x120;
- CloseHandle(HANDLE_OF_MSVCRT);
- mbstowcs(text, CommandLine, strlen(CommandLine) + 1);
- mbstowcs(text_1, szModuleName, strlen(szModuleName) + 1);
- Result_Of_New_Proc = CreateProcessA(NULL, CommandLine, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi);
- if (Result_Of_New_Proc == 0)
- {
- MessageBoxA(NULL, ERROR_CREATEPROCESS, ERROR_CREATEPROCESS, MB_OK);
- }
- snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
- if (Process32First(snapshot, &entry) == TRUE)
- {
- while (Process32Next(snapshot, &entry) == TRUE)
- {
- if (_stricmp((const char*)entry.szExeFile, (const char*)szModuleName) == 0)//wcscmp//_stricmp
- {
- HANDLE_OF_CHILD_PROC = OpenProcess(PROCESS_ALL_ACCESS, TRUE, entry.th32ProcessID);
- HANDLE_OF_MY_PROC = OpenProcess(PROCESS_ALL_ACCESS, TRUE, GetCurrentProcessId());
- ReadProcessMemory(HANDLE_OF_CHILD_PROC, (LPVOID)BaseAddress_tls_section, &MassiveOfOpcodes[1588][0x1000], NumberOfBytesRead_tls_section, NULL);
- ReadProcessMemory(HANDLE_OF_CHILD_PROC, (LPVOID)BaseAddress_text_section, &MassiveOfOpcodes[0x46E0][0x4000], NumberOfBytesRead_text_section, NULL);
- ReadProcessMemory(HANDLE_OF_MY_PROC, (LPVOID)ADDRESS_TEXT_SECTION_NTDLL, &NtdllOpcodes[0][0], NumberOfBytesRead_text_section_ntdll, NULL);
- ReadProcessMemory(HANDLE_OF_MY_PROC, (LPVOID)ADDRESS_TEXT_SECTION_MSVCRT, &MSVCRTOpcodes[0][0], NumberOfBytesRead_text_section_msvcrt, NULL);
- // WaitForSingleObject(pi.hProcess, INFINITE);
- CloseHandle(snapshot);//&MassiveOfOpcodes[1952][8192]//&MassiveOfOpcodes[1952][4096]//&MassiveOfOpcodes[1952][0]
- // CloseHandle(pi.hProcess);
- // CloseHandle(pi.hThread);
- }
- }
- }
- ImageBase = GetModuleHandle(NULL);
- if (ImageBase == 0)
- {
- MessageBoxA(NULL, ERROR_IMAGEBASE, ERROR_IMAGEBASE, MB_OK);
- }
- HANDLE_OF_MY_PROC = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE, true, GetCurrentProcessId());
- return 0;
- };
- int UcEmuDeobfuscate()
- {
- #define STACK_ADDRESS 0x10000
- #define TLS_DENUVO_ADDRESS 0x140941000
- int64_t NTDLL_ADDRESS = ADDRESS_TEXT_SECTION_NTDLL;//0x7FFE849F1000
- int64_t MSVCRT_ADDRESS = ADDRESS_TEXT_SECTION_MSVCRT;//0x7FFE82B61000
- #define GS_BASE_ADDRESS 0x22A000
- uint64_t SIZE_OF_SECTION_TLS_DENUVO = 0x24000000;
- uint64_t SIZE_OF_STACK = 0x7000;
- uint64_t SIZE_OF_NTDLL_TEXT_SECTION = 0x12A000;
- uint64_t SIZE_OF_MSVCRT_TEXT_SECTION = 0x100000;
- uint64_t SIZE_OF_GDT_BASE = 0xB000;
- uc_hook hook_code_;
- uc_engine* uc;
- uc_err err;
- int64_t RAX = 0x0;
- int64_t RBX = 0x0;
- int64_t RCX = 0x0;
- int64_t RDX = 0x0;
- int64_t RSI = 0x0;
- int64_t RDI = 0x0;
- int64_t R8 = 0x0;
- int64_t R9 = 0x0;
- int64_t R10 = 0x0;
- int64_t R11 = 0x0;
- int64_t R12 = 0x0;
- int64_t R13 = 0x0;
- int64_t R14 = 0x0;
- int64_t R15 = 0x0;
- int64_t RIP{};
- int64_t GS_BASE = GS_BASE_ADDRESS;
- int64_t RFLAGS{};
- int64_t RSP = STACK_ADDRESS + TLS_DENUVO_ADDRESS;
- int64_t RBP = 0x0;
- printf("Emulate x64 code\n");
- err = uc_open(UC_ARCH_X86, UC_MODE_64, &uc);
- if (err != UC_ERR_OK) {
- printf("Failed on uc_open() with error returned: %u\n", err);
- return -1;
- }
- uc_mem_map(uc, STACK_ADDRESS, SIZE_OF_STACK, UC_PROT_ALL);
- uc_mem_map(uc, TLS_DENUVO_ADDRESS, SIZE_OF_SECTION_TLS_DENUVO, UC_PROT_ALL);
- uc_mem_map(uc, NTDLL_ADDRESS, SIZE_OF_NTDLL_TEXT_SECTION, UC_PROT_ALL);
- uc_mem_map(uc, MSVCRT_ADDRESS, SIZE_OF_MSVCRT_TEXT_SECTION, UC_PROT_ALL);
- uc_mem_map(uc, GS_BASE_ADDRESS, SIZE_OF_GDT_BASE, UC_PROT_ALL);
- if (uc_mem_write(uc, TLS_DENUVO_ADDRESS, &MassiveOfOpcodes, sizeof(MassiveOfOpcodes))) {
- printf("Failed to write emulation code to memory, quit!\n");
- return -1;
- }
- if (uc_mem_write(uc, NTDLL_ADDRESS, &NtdllOpcodes, sizeof(NtdllOpcodes))) {
- printf("Failed to write emulation code to memory, quit!\n");
- return -1;
- }
- if (uc_mem_write(uc, MSVCRT_ADDRESS, &MSVCRTOpcodes, sizeof(MSVCRTOpcodes))) {
- printf("Failed to write emulation code to memory, quit!\n");
- return -1;
- }
- std::cout << ">>> RAX = " << hex << RAX << endl;
- std::cout << ">>> RBX = " << hex << RBX << endl;
- std::cout << ">>> RCX = " << hex << RCX << endl;
- std::cout << ">>> RDX = " << hex << RDX << endl;
- std::cout << ">>> RSI = " << hex << RSI << endl;
- std::cout << ">>> RDI = " << hex << RDI << endl;
- std::cout << ">>> R8 = " << hex << R8 << endl;
- std::cout << ">>> R9 = " << hex << R9 << endl;
- std::cout << ">>> R10 = " << hex << R10 << endl;
- std::cout << ">>> R11 = " << hex << R11 << endl;
- std::cout << ">>> R12 = " << hex << R12 << endl;
- std::cout << ">>> R13 = " << hex << R13 << endl;
- std::cout << ">>> R14 = " << hex << R14 << endl;
- std::cout << ">>> R15 = " << hex << R15 << endl << endl;
- std::cout << ">>> RIP = " << hex << RIP << endl << endl;
- std::cout << ">>> RFLAGS = " << hex << RFLAGS << endl << endl;
- std::cout << ">>> RSP = " << hex << RSP << endl;
- std::cout << ">>> RBP = " << hex << RBP << endl << endl;
- uc_reg_write(uc, UC_X86_REG_RAX, &RAX);
- uc_reg_write(uc, UC_X86_REG_RBX, &RBX);
- uc_reg_write(uc, UC_X86_REG_RCX, &RCX);
- uc_reg_write(uc, UC_X86_REG_RDX, &RDX);
- uc_reg_write(uc, UC_X86_REG_RSI, &RSI);
- uc_reg_write(uc, UC_X86_REG_RDI, &RDI);
- uc_reg_write(uc, UC_X86_REG_R8, &R8);
- uc_reg_write(uc, UC_X86_REG_R9, &R9);
- uc_reg_write(uc, UC_X86_REG_R10, &R10);
- uc_reg_write(uc, UC_X86_REG_R11, &R11);
- uc_reg_write(uc, UC_X86_REG_R12, &R12);
- uc_reg_write(uc, UC_X86_REG_R13, &R13);
- uc_reg_write(uc, UC_X86_REG_R14, &R14);
- uc_reg_write(uc, UC_X86_REG_R15, &R15);
- uc_reg_write(uc, UC_X86_REG_RIP, &RIP);
- uc_reg_write(uc, UC_X86_REG_GS_BASE, &GS_BASE);
- uc_reg_write(uc, UC_X86_REG_RFLAGS, &RFLAGS);
- uc_reg_write(uc, UC_X86_REG_RSP, &RSP);
- uc_reg_write(uc, UC_X86_REG_RBP, &RBP);
- uc_hook_add(uc, &hook_code_, UC_HOOK_CODE, hook_code, NULL, 1, 0);
- err = uc_emu_start(uc, UC_EMU_START_ADDRESS, UC_EMU_START_ADDRESS + sizeof(MassiveOfOpcodes) + sizeof(NtdllOpcodes) + sizeof(MSVCRTOpcodes), 0, 60);
- if (err) {
- printf("Failed on uc_emu_start() with error returned %u: %s\n",
- err, uc_strerror(err));
- }
- printf("Emulation done. Below is the CPU context\n\n");
- uc_reg_read(uc, UC_X86_REG_RAX, &RAX);
- uc_reg_read(uc, UC_X86_REG_RBX, &RBX);
- uc_reg_read(uc, UC_X86_REG_RCX, &RCX);
- uc_reg_read(uc, UC_X86_REG_RDX, &RDX);
- uc_reg_read(uc, UC_X86_REG_RSI, &RSI);
- uc_reg_read(uc, UC_X86_REG_RDI, &RDI);
- uc_reg_read(uc, UC_X86_REG_R8, &R8);
- uc_reg_read(uc, UC_X86_REG_R9, &R9);
- uc_reg_read(uc, UC_X86_REG_R10, &R10);
- uc_reg_read(uc, UC_X86_REG_R11, &R11);
- uc_reg_read(uc, UC_X86_REG_R12, &R12);
- uc_reg_read(uc, UC_X86_REG_R13, &R13);
- uc_reg_read(uc, UC_X86_REG_R14, &R14);
- uc_reg_read(uc, UC_X86_REG_R15, &R15);
- uc_reg_read(uc, UC_X86_REG_RIP, &RIP);
- uc_reg_read(uc, UC_X86_REG_RFLAGS, &RFLAGS);
- uc_reg_read(uc, UC_X86_REG_RSP, &RSP);
- uc_reg_read(uc, UC_X86_REG_RBP, &RBP);
- std::cout << ">>> RAX = " << hex << RAX << endl;
- std::cout << ">>> RBX = " << hex << RBX << endl;
- std::cout << ">>> RCX = " << hex << RCX << endl;
- std::cout << ">>> RDX = " << hex << RDX << endl;
- std::cout << ">>> RSI = " << hex << RSI << endl;
- std::cout << ">>> RDI = " << hex << RDI << endl;
- std::cout << ">>> R8 = " << hex << R8 << endl;
- std::cout << ">>> R9 = " << hex << R9 << endl;
- std::cout << ">>> R10 = " << hex << R10 << endl;
- std::cout << ">>> R11 = " << hex << R11 << endl;
- std::cout << ">>> R12 = " << hex << R12 << endl;
- std::cout << ">>> R13 = " << hex << R13 << endl;
- std::cout << ">>> R14 = " << hex << R14 << endl;
- std::cout << ">>> R15 = " << hex << R15 << endl << endl;
- std::cout << ">>> RIP = " << hex << RIP << endl << endl;
- std::cout << ">>> RFLAGS = " << hex << RFLAGS << endl << endl;
- std::cout << ">>> RSP = " << hex << RSP << endl;
- std::cout << ">>> RBP = " << hex << RBP << endl;
- //(UC_ERR_READ_UNMAPPED) - the problem of command which we want read
- uc_close(uc);
- return 0;
- }
- int main()
- {
- Create_Read_Process();
- UcEmuDeobfuscate();
- }
Advertisement
Add Comment
Please, Sign In to add comment