#include "PacketId.h" std::list opcodes; std::list gOpUpdates; unsigned char* CallHookStart = NULL; unsigned char* CallHookReturn = NULL; unsigned char CallHookCode[] = { 0x8B, 0x46, 0x18, 0x8B, 0x55, 0xF4, 0x8D, 0x8E, 0x58, 0x08, 0x00, 0x00, 0xFF, 0x50, 0x08 }; unsigned char** PacketCodeData; int gMonitorMode=0; int gSuppressWriteSigPrompt=false; bool LoadPacketOpcodes(char* pFilename); void __stdcall CallHook(unsigned char* packet, unsigned char* function); std::ofstream gLogFile; //Does the magic void _declspec(naked) ASMCallHook() { _asm { //Code from GW i overwrote.. MOV EAX,DWORD PTR DS:[ESI+0x18] MOV EDX,DWORD PTR SS:[EBP-0xC] LEA ECX,DWORD PTR DS:[ESI+0x858] //Save Registers PUSH EAX PUSH ECX PUSH EDX PUSH ESP MOV EDX, DWORD PTR DS:[EAX+0x8] PUSH EDX //Function Pointer PUSH ECX //Packet Pointer //Call my function that checks teh Code CALL CallHook //Restore Registers POP ESP POP EDX POP ECX POP EAX JMP CallHookReturn } } //Finds the code in memory to insert leet asm hax void FindCallHookOffset() { unsigned char* start = (unsigned char*)0x00401000; unsigned char* end = (unsigned char*)0x00600000; size_t CallHookSize = sizeof(CallHookCode); while(start != end) { if(!memcmp(start, CallHookCode, CallHookSize)) { CallHookStart = start; CallHookReturn = start+0xC; return; } start++; } } //Writes the jump to memory (MOV EAX, MahFunction CALL EAX) void WriteJMP( unsigned char* location, unsigned char* newFunction ) { DWORD dwOldProtection; VirtualProtect(location, 7, PAGE_EXECUTE_READWRITE, &dwOldProtection); location[0] = 0xB8; *((unsigned int*)(location + 1)) = (unsigned int)newFunction; location[5] = 0xFF; location[6] = 0xE0; VirtualProtect(location, 7, dwOldProtection, &dwOldProtection); } void WriteUpdate(OpUpdate pU) { fstream fout; fout.open("KnownCodes/OutCodes.ini", ios::out | ios::in); fout.seekp(pU.fileOffset); char opcode[20]; sprintf(opcode, "0x%04X", REV(pU.opcode)); fout.write(opcode, strlen(opcode)); fout.close(); SetWrittenByName(pU.packetName); gOpUpdates.push_back(pU); } void SetWrittenByName(char* pName) { std::list::iterator itr; for (itr=opcodes.begin(); itr!=opcodes.end(); itr++) { if (strcmp(pName, itr->packetName) == 0) { itr->wroteChange=true; } } } int GetOffsetByName(char* pName) { std::list::iterator itr; for (itr=opcodes.begin(); itr!=opcodes.end(); itr++) { if (strcmp(pName, itr->packetName) == 0) { return itr->fileOffset; } } return 0; } int MaxCanUpdate() { int remaining=0; std::list::iterator itr; for (itr=opcodes.begin(); itr!=opcodes.end(); itr++) { if (itr->pSignature != NULL) { remaining++; } } return remaining; } void Log(char* pString, ...) { va_list args; va_start(args, pString); char string[9000]; vsprintf(string, pString, args); va_end(args); printf(string); gLogFile << string; } BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) { switch(dwReason) { case DLL_PROCESS_ATTACH: gLogFile.open("KnownCodes/log.txt"); gLogFile << "------------------------------------------------------" << '\n'; FILE* fh; FILE* fi; //Create meh console screen AllocConsole(); SetConsoleTitle( "GWInject Packet Identifier by ExJam & l3j. Close GW to exit." ); freopen_s( &fh, "CONOUT$", "wb", stdout); //Redirect the stdin to the console if (true) { long lhandle=(long)GetStdHandle(STD_INPUT_HANDLE); int hConHandle = _open_osfhandle(lhandle, _O_TEXT); fi = _fdopen( hConHandle, "r" ); *stdin = *fi; setvbuf( stdin, NULL, _IONBF, 0 ); } //Loads packet headers, dirty :D LoadPacketOpcodes("KnownCodes/KnownCodes.ini"); //Load all the updates to the packet opcodes if (true) { ifstream fin("KnownCodes/update.dat", std::ios::binary); if (!fin.fail()) { int begin=fin.tellg(); fin.seekg(0, std::ios::end); int end=fin.tellg(); int size=end-begin; fin.seekg(0, std::ios::beg);//*/ //Load all the opcode updates for (int i=0; i::iterator itr; for (itr=opcodes.begin(); itr!=opcodes.end(); itr++) { if (itr->pSignature == NULL){count++;} } } //Ask what mode this should run in char choice=0; printf("Stats: Updated: %u/%u No Sig: %u\n", gOpUpdates.size(), MaxCanUpdate(), count); printf("---------------------------------\n"); printf("1. Collect packet signatures.\n"); printf("2. Find updated packet opcodes.\n"); printf("3. View not-updated packets.\n"); printf("4. View packets without a signature.\n"); printf("5. No-write collect packet signatures.\n"); printf("6. Analysis mode.\n"); printf("q. Run GW normally\n"); printf("---------------------------------\n"); printf("Please press to select.\n"); choice=getch(); switch (choice) { case '1': printf("Collecting packet code signatures...\n"); gMonitorMode=1; break; case '2': printf("Finding updated packet opcodes...\n"); gMonitorMode=2; break; case '3': if (true) { system("cls"); std::list::iterator itr; for (itr=opcodes.begin(); itr!=opcodes.end(); itr++) { if (itr->pSignature != NULL && !itr->wroteChange) { printf("'%s'\n", itr->packetName); } } printf("%u/%u packets are updated!\n\n", gOpUpdates.size(), MaxCanUpdate()); printf("Press any key to continue.\n"); getch(); system("cls"); goto ask; } break; case '4': if (true) { system("cls"); std::list::iterator itr; for (itr=opcodes.begin(); itr!=opcodes.end(); itr++) { if (itr->pSignature == NULL) { printf("'%s'\n", itr->packetName); } } printf("%u packets lack a code signature!\n\n", count); printf("Press any key to continue.\n"); getch(); system("cls"); goto ask; } break; case '5': printf("No-write Collecting packet code signatures...\n"); gMonitorMode=3; break; case '6': gMonitorMode=4; break; case 'q': if (true) { std::list::iterator itr; for (itr=opcodes.begin(); itr!=opcodes.end(); itr++) { if (itr->pSignature != NULL) { delete [] itr->pSignature; } } FreeConsole(); ExitThread(0); } break; } } printf("----------------------------------------------\n\n\n"); break; case DLL_PROCESS_DETACH: FreeConsole(); std::list::iterator itr; for (itr=opcodes.begin(); itr!=opcodes.end(); itr++) { if (itr->pSignature != NULL) { delete [] itr->pSignature; } } ofstream fout("KnownCodes/update.dat", std::ios::binary); //Save all the opcode updates std::list::iterator tr; for (tr=gOpUpdates.begin(); tr!=gOpUpdates.end(); tr++) { OpUpdate* pUo=&(*tr); fout.write((char*)pUo, sizeof(OpUpdate)); } fout.close(); gLogFile.close(); break; } return true; }