#include "PacketId.h" #define MAX_SIG_FUNCTION_SIZE 200 bool CompareSigs(unsigned char* pFunctionSig, unsigned char* pStoredSig, int sigSize); bool ExtraProcessing(unsigned short header, unsigned char* pPacket); int GetCodeSigSize(unsigned char* pCodeStart, vector* pCharBuffer, bool stepIntoCalls) { unsigned char* pCodeStartBak=pCodeStart; ud_t dis; ud_init(&dis); ud_set_syntax(&dis, UD_SYN_INTEL); ud_set_mode(&dis, 32); ud_set_input_buffer(&dis, pCodeStart, MAX_SIG_FUNCTION_SIZE); int sig_size=0; int opcode_size=0; while ((opcode_size=ud_disassemble(&dis)) > 0) { unsigned char* baseAddr=pCodeStartBak+sig_size; //Save the bytes used for this instruction to our buffer for(int i=0; ipush_back(*pChar); } sig_size+=opcode_size; baseAddr=pCodeStartBak+sig_size; //If this instruction is CALL then step into the CALL'ed function if (dis.mnemonic == UD_Icall && stepIntoCalls) { int addr=0; memcpy(&addr, &dis.operand[0].lval, sizeof(int)); //printf("Start: %08X Call: %08X Base: %X Func: %08X\n", pCodeStart, addr, baseAddr, baseAddr+addr); GetCodeSigSize(baseAddr+addr, pCharBuffer, false); } //If this instruction is RET or we have reached the max signature size per function if (dis.mnemonic == UD_Iret || sig_size >= MAX_SIG_FUNCTION_SIZE) { break; } } return pCharBuffer->size(); } bool after=false; //Does the magic code checking! void __stdcall CallHook(unsigned char* packet, unsigned char* function) { unsigned int iHeader = *((unsigned short*)&packet[0]); //ignore the CIH packet if (iHeader == 0x0011) { return; } unsigned long te=0; memcpy(&te, packet+4, 4); /*if (te != 20) { if (after) { after=false; } else { return; } } else { SetConsoleColor(CC_BLUE, CC_BLACK); after=true; }*/ if (gMonitorMode==4) { if (!ExtraProcessing(iHeader, packet)) { return; } } //Get the signature of the function vector buf; int sig_size=GetCodeSigSize(function, &buf, true); //We are comparing signatures if (gMonitorMode == 2) { int selection=0; std::vector conflicts; //Check all signatures for this one std::list::iterator itr; for (itr=opcodes.begin(); itr!=opcodes.end(); itr++) { if (itr->sigSize == sig_size) { if (CompareSigs(&buf[0], itr->pSignature, sig_size)) { conflicts.push_back(*itr); } } } bool resolve=false; if (conflicts.size() > 0) { //Check for conflicting signatures if (conflicts.size() > 1) { //We must solve the conflict manually SetConsoleColor(CC_LIGHTRED, CC_BLACK); printf("------------- Code Signature Conflict! ----------\n"); int i=0; for (i=0; i> selection; printf("--------------- Conflict Resolved ---------------\n\n"); selection--; if (selection < conflicts.size()){resolve=true;} } //We have a valid packet selected if (selection < conflicts.size()) { if (!conflicts[selection].wroteChange) { //Process the one remaining match if (conflicts[selection].opcode != iHeader) { SetConsoleColor(CC_YELLOW, CC_BLACK); printf("%08X - Found %s(%04X) New(%04X)", function, conflicts[selection].packetName, REV(conflicts[selection].opcode), REV(iHeader)); SetConsoleColor(CC_LIGHTGRAY, CC_BLACK); } else { if (!conflicts[selection].wroteChange) { printf("%08X %04X - Match %s(%04X)", function, REV(iHeader), conflicts[selection].packetName, REV(conflicts[selection].opcode), REV(iHeader)); } } OpUpdate uo; strcpy(uo.packetName, conflicts[selection].packetName); uo.fileOffset=conflicts[selection].fileOffset; uo.opcode=iHeader; WriteUpdate(uo); printf(" ~ Updated %u/%u\n", gOpUpdates.size(), MaxCanUpdate()); } else { printf("%08X %04X - Match %s(%04X)\n", function, REV(iHeader), conflicts[selection].packetName, REV(conflicts[selection].opcode), REV(iHeader)); } } conflicts.clear(); } } //We are collecting signatures else if (gMonitorMode == 1 || gMonitorMode == 3) { bool found_opcode=false; //Find the packet for our opcode std::list::iterator itr; for (itr=opcodes.begin(); itr!=opcodes.end(); itr++) { if (itr->opcode == iHeader) { found_opcode=true; if (itr->pSignature == NULL) { if (!gSuppressWriteSigPrompt) { char choice=0; Log("\n%u byte signature for %s(%04X)\n", sig_size, itr->packetName, REV(iHeader)); if (gMonitorMode != 3) { printf("1. Yes\n"); printf("2. No\n"); printf("3. Always\n"); printf("Write signature? \n"); choice=getch(); if (choice=='2'){break;} else if(choice=='3'){gSuppressWriteSigPrompt=true;} } } if (gMonitorMode != 3) { //Write the signature file char file[150]; sprintf(file, "KnownCodes/CodeSigs/%s.cs", itr->packetName); itr->sigSize=sig_size; itr->pSignature=new unsigned char[sig_size]; memcpy(itr->pSignature, &buf[0], sig_size); std::ofstream fout(file, std::ios::binary); fout.write((char*)itr->pSignature, sig_size); fout.close(); SetConsoleColor(CC_GREEN, CC_BLACK); printf("%08X %04X - Wrote %u byte signature for %s(%04X)\n", function, REV(iHeader), sig_size, itr->packetName, REV(iHeader)); SetConsoleColor(CC_LIGHTGRAY, CC_BLACK); } break; } else if (gMonitorMode == 3) { Log("\n%u byte signature for %s(%04X)\n", sig_size, itr->packetName, REV(iHeader)); } } } if (!found_opcode) { Log("%08X %04X - Not found\n", function, REV(iHeader)); } } SetConsoleColor(CC_LIGHTGRAY, CC_BLACK); }