Advertisement
Riremito

AirMemory.cpp

Jul 16th, 2015
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.09 KB | None | 0 0
  1. #include"AirMemory.h"
  2.  
  3. namespace Air{
  4.     DWORD ReadAob(char *Aob, BYTE *b, bool *mask);
  5.     int CreateMemory(BYTE *b, char *Code);
  6. }
  7.  
  8. DWORD Air::ReadAob(char *Aob, BYTE *b, bool *mask){
  9.     DWORD dwSize = 0;
  10.     int i, j;
  11.     BYTE temp;
  12.     BYTE rb;
  13.     i = 0;
  14.     j = 0;
  15.  
  16.     while(Aob[j]){
  17.         rb = (BYTE)Aob[j];
  18.         if(i%2 == 0){
  19.             temp = 4;//high
  20.         }
  21.         else{
  22.             temp = 0;//low
  23.         }
  24.  
  25.         if(rb >= '0' && rb <= '9'){
  26.             b[i/2] += (rb - '0') << temp;
  27.             i++;
  28.         }
  29.         else if(rb >= 'A' && rb <= 'F'){
  30.             b[i/2] += (rb - 'A' + 0x0A) << temp;
  31.             i++;
  32.         }
  33.         else if(rb >= 'a' && rb <= 'f'){
  34.             b[i/2] += (rb - 'a' + 0x0A) << temp;
  35.             i++;
  36.         }
  37.         else if(rb == '?'){
  38.             b[i/2] = 0xFF;
  39.             mask[i/2] = true;
  40.             i++;
  41.         }
  42.         j++;
  43.     }
  44.  
  45.     return (i%2 == 0) ? (i/2) : -1;
  46. }
  47.  
  48. int Air::CreateMemory(BYTE *b, char *Code){
  49.     BYTE wb, temp;
  50.     int i, j;
  51.     i = 0;
  52.     j = 0;
  53.     while(Code[j]){
  54.         wb = (BYTE)Code[j];
  55.         if(i%2 == 0){
  56.             temp = 4;//high
  57.         }
  58.         else{
  59.             temp = 0;//low
  60.         }
  61.  
  62.         if(wb >= '0' && wb <= '9'){
  63.             b[i/2] += (wb - '0') << temp;
  64.             i++;
  65.         }
  66.         else if(wb >= 'A' && wb <= 'F'){
  67.             b[i/2] += (wb - 'A' + 0x0A) << temp;
  68.             i++;
  69.         }
  70.         else if(wb >= 'a' && wb <= 'f'){
  71.             b[i/2] += (wb - 'a' + 0x0A) << temp;
  72.             i++;
  73.         }
  74.         j++;
  75.     }
  76.  
  77.     return (i%2 == 0) ? (i/2) : -1;
  78. }
  79.  
  80. //AirMemory
  81.  
  82. AirMemory::AirMemory(){
  83.     BaseAddress = NULL;
  84.     Memory_Start = NULL;
  85.     Memory_End = NULL;
  86.     MemoryDump = NULL;
  87.     IsDLL = false;
  88.     MemoryList.Next = NULL;
  89. }
  90.  
  91. void AirMemory::Init(char *ModuleName){
  92.     HMODULE hModule = NULL;
  93.     MODULEINFO ModInfo;
  94.     HANDLE hProcess = GetCurrentProcess();
  95.     IMAGE_NT_HEADERS *nth;
  96.  
  97.     if(ModuleName){//DLL
  98.         while(!(hModule = GetModuleHandleA(ModuleName))){
  99.             Sleep(100);
  100.         }
  101.         GetModuleInformation(hProcess, hModule, &ModInfo, sizeof(MODULEINFO));
  102.         Memory_Start = (DWORD)ModInfo.lpBaseOfDll;
  103.         Memory_End = (DWORD)ModInfo.lpBaseOfDll + (DWORD)ModInfo.SizeOfImage;
  104.         IsDLL = true;
  105.     }
  106.     else{//Client
  107.         hModule = GetModuleHandleA(NULL);
  108.         nth = (IMAGE_NT_HEADERS *)((DWORD)hModule + PIMAGE_DOS_HEADER(hModule)->e_lfanew);
  109.         Memory_Start = (DWORD)nth->OptionalHeader.ImageBase;
  110.         Memory_End = (DWORD)nth->OptionalHeader.ImageBase + (DWORD)nth->OptionalHeader.SizeOfImage;
  111.     }
  112.     BaseAddress = Memory_Start;
  113. }
  114.  
  115.  
  116. void AirMemory::CreateMemoryDump(){
  117.     BYTE *NewMemory = (BYTE *)malloc(Memory_End - Memory_Start);
  118.     RtlCopyMemory(NewMemory, (DWORD *)Memory_Start, Memory_End - Memory_Start);
  119.     MemoryDump = (DWORD)NewMemory;
  120. }
  121.  
  122. bool AirMemory::FullAccess(DWORD Address, DWORD Size){
  123.     if(VirtualProtect((DWORD *)Address, Size, PAGE_EXECUTE_READWRITE, &OldProtect)){
  124.         OldAddr = Address;
  125.         OldSize = Size;
  126.         return true;
  127.     }
  128.     return false;
  129. }
  130.  
  131. bool AirMemory::RestoreProtect(){
  132.     DWORD old;
  133.     return VirtualProtect((DWORD *)OldAddr, OldSize, OldProtect, &old);
  134. }
  135.  
  136. DWORD AirMemory::AobScan(char *Aob, int Result, DWORD StartAddress, DWORD EndAddress){
  137.     BYTE Memory[MaxSize] = {0};
  138.     bool mask[MaxSize] = {0};
  139.     DWORD Size = Air::ReadAob(Aob, Memory, mask);
  140.     DWORD i, j, ResultCount = 0;
  141.  
  142.     if(Size < 1){
  143.         return 0;
  144.     }
  145.    
  146.     StartAddress = StartAddress? StartAddress: Memory_Start;
  147.     EndAddress = EndAddress? EndAddress: Memory_End;
  148.  
  149.     __try{
  150.         for(i=StartAddress; i<EndAddress; i++){
  151.             for(j=0; j<Size; j++){
  152.                 if(mask[j]){
  153.                     continue;
  154.                 }
  155.                 if(Memory[j] != *(BYTE *)(i + j)){
  156.                     break;
  157.                 }
  158.             }
  159.             if(j == Size){
  160.                 ResultCount++;
  161.                 if(ResultCount >= Result){
  162.                     return i;
  163.                 }
  164.             }
  165.         }
  166.     }
  167.     __except(EXCEPTION_EXECUTE_HANDLER){
  168.         return 0;
  169.     }
  170.     return 0;
  171. }
  172.  
  173. DWORD AirMemory::GetAbsoluteAddress(DWORD Address){
  174.     if(IsDLL){
  175.         if(Address < BaseAddress){
  176.             return (Address + BaseAddress);
  177.         }
  178.     }
  179.     return Address;
  180. }
  181.  
  182. void AirMemory::AddBackupMemory(DWORD Address, void *Memory, DWORD Size){
  183.     BackupMemory *p = (BackupMemory *)malloc(sizeof(BackupMemory));
  184.  
  185.     p->Address = Address;
  186.     p->Size = Size;
  187.     p->Memory = malloc(Size);
  188.     MemoryCopy((DWORD)p->Memory, (void *)p->Address, p->Size);//Backup Memory
  189.     if(Memory){
  190.         MemoryCopy(p->Address, Memory, p->Size);//Write Memory
  191.     }
  192.  
  193.     if(!MemoryList.Next){
  194.         p->Next = NULL;
  195.         MemoryList.Next = p;
  196.     }
  197.     else{
  198.         p->Next = MemoryList.Next;
  199.         MemoryList.Next = p;
  200.     }
  201. }
  202.  
  203. bool AirMemory::DeleteBackupMemory(DWORD Address){
  204.     BackupMemory *p, *d, *pp;
  205.     pp = &MemoryList;
  206.     for(p=MemoryList.Next; p; p=p->Next){
  207.         if(p->Address == Address){
  208.             d = p;
  209.             MemoryCopy(d->Address, d->Memory, d->Size);//Restore Memory
  210.             pp->Next = p->Next;
  211.             free(d->Memory);
  212.             free(d);
  213.             return true;
  214.         }
  215.         pp = p;
  216.     }
  217.     return false;
  218. }
  219.  
  220. void AirMemory::MemoryCopy(DWORD Address, void *Memory, DWORD Size){
  221.     __try{
  222.         if(FullAccess(Address, Size)){
  223.             RtlCopyMemory((void *)Address, Memory, Size);
  224.             RestoreProtect();
  225.         }
  226.     }
  227.     __except(EXCEPTION_EXECUTE_HANDLER){
  228.     }
  229.     return;
  230. }
  231.  
  232. bool AirMemory::MemoryWriter(DWORD Address, char *Code){
  233.     BYTE Memory[MaxSize] = {0};
  234.     DWORD Size = Air::CreateMemory(Memory, Code);
  235.  
  236.     if(Size < 1){
  237.         return false;
  238.     }
  239.  
  240.     AddBackupMemory(GetAbsoluteAddress(Address), Memory, Size);
  241.     return true;
  242. }
  243.  
  244.  
  245. bool AirMemory::RestoreMemory(DWORD Address){
  246.     return DeleteBackupMemory(GetAbsoluteAddress(Address));
  247. }
  248.  
  249. bool AirMemory::WriteHook(DWORD Address, WORD OpCode, void *Function, DWORD *RetAddr, DWORD AddNop){
  250.     DWORD Size;
  251.     if(OpCode < 0x100){
  252.         Size = 5;
  253.     }
  254.     else{
  255.         Size = 6;
  256.     }
  257.  
  258.     Address = GetAbsoluteAddress(Address);
  259.  
  260.     FullAccess(Address, Size + AddNop);
  261.     AddBackupMemory(Address, NULL, Size + AddNop);
  262.     __try{
  263.         if(OpCode < 0x100){
  264.             *(BYTE *)(Address) = OpCode;
  265.             *(DWORD *)(Address + 1) = (DWORD)Function - Address - Size;
  266.         }
  267.         else{
  268.             *(WORD *)(Address) = OpCode;
  269.             *(DWORD *)(Address + 2) = (DWORD)Function - Address - Size;
  270.         }
  271.         for(int i = 0; i<AddNop; i++){
  272.             *(BYTE *)(Address + Size + i) = 0x90;
  273.         }
  274.     }
  275.     __except(EXCEPTION_EXECUTE_HANDLER){
  276.         return false;
  277.     }
  278.     RestoreProtect();
  279.  
  280.     if(RetAddr){
  281.         *RetAddr = Address + Size + AddNop;
  282.     }
  283.     return true;
  284. }
  285.  
  286. void AirMemory::GetDumpInfo(DWORD *MS, DWORD *ME, DWORD *MD){
  287.     *MS = Memory_Start;
  288.     *ME = Memory_End;
  289.     *MD = MemoryDump;
  290. }
  291.  
  292. DWORD AirMemory::GetBaseAddress(){
  293.     return BaseAddress;
  294. }
  295.  
  296. void AirMemory::PointerHook(DWORD Pointer, void *Function, DWORD *OldFunction){
  297.     Pointer = GetAbsoluteAddress(Pointer);
  298.  
  299.     FullAccess(Pointer, 4);//v1.01
  300.     AddBackupMemory(Pointer, NULL, 4);
  301.     *OldFunction = *(DWORD *)Pointer;
  302.     *(DWORD *)Pointer = (DWORD)Function;
  303.     RestoreProtect();//v1.01
  304. }
  305.  
  306. DWORD AirMemory::AutoVMHook(DWORD Address, void *Function, DWORD *RetAddr, DWORD MinAddr){
  307.     DWORD i, VMSection, VMSection_Ret;
  308.  
  309.     Address = GetAbsoluteAddress(Address);
  310.  
  311.     for(i=Address; ;i++){
  312.         if(*(BYTE *)i == JMP){
  313.             VMSection = *(DWORD *)(i + 1) + i + 5;
  314.             if((VMSection > Memory_Start) && (VMSection < Memory_End)){
  315.                 break;
  316.             }
  317.         }
  318.     }
  319.  
  320.     for(i=VMSection; ;i++){
  321.         switch(*(BYTE *)i){
  322.         case JMP:
  323.             VMSection_Ret =  *(DWORD *)(i + 1) + i + 5;
  324.             if((VMSection_Ret > Memory_Start) && (VMSection_Ret < Memory_End)){
  325.                 if(MinAddr && i < MinAddr){
  326.                     i = VMSection_Ret;
  327.                     break;
  328.                 }
  329.                 WriteHook(i, JMP, Function);
  330.                 *RetAddr = VMSection_Ret;
  331.                 return i;
  332.             }
  333.             break;
  334.  
  335.         case CALL:
  336.             VMSection_Ret =  *(DWORD *)(i + 1) + i + 5;
  337.             if((VMSection_Ret > Memory_Start) && (VMSection_Ret < Memory_End)){
  338.                 if(MinAddr && i < MinAddr){
  339.                     i = VMSection_Ret;
  340.                     break;
  341.                 }
  342.                 WriteHook(i, CALL, Function);
  343.                 *RetAddr = VMSection_Ret;
  344.                 return i;
  345.             }
  346.             break;
  347.  
  348.         default:
  349.             break;
  350.         }
  351.     }
  352. }
  353.  
  354. void WriteJMP(DWORD Prev, DWORD Next){
  355.     *(BYTE *)Prev = 0xE9;
  356.     *(DWORD *)(Prev + 1) = Next - Prev - 5;
  357. }
  358.  
  359. void WriteCALL(DWORD Prev, DWORD Next){
  360.     *(BYTE *)Prev = 0xE8;
  361.     *(DWORD *)(Prev + 1) = Next - Prev - 5;
  362. }
  363.  
  364. bool AirMemory::FunctionHook(char *ProcName, void *Function, DWORD *RetAddr){
  365.     DWORD ExportFunction, i;
  366.     void *NewMem = malloc(7);
  367.  
  368.     if(!IsDLL){
  369.         return false;
  370.     }
  371.  
  372.     ExportFunction = (DWORD)GetProcAddress((HMODULE)BaseAddress, ProcName);
  373.  
  374.     if(!ExportFunction){
  375.         return false;
  376.     }
  377.  
  378.     FullAccess((DWORD)NewMem, 7);
  379.  
  380.     if(FullAccess(ExportFunction - 5, 7)){
  381.         if(*(WORD *)ExportFunction == 0xFF8B || *(BYTE *)ExportFunction == 0x6A){
  382.             for(i=(ExportFunction - 5); i<ExportFunction; i++){
  383.                 if(*(BYTE *)i != 0x90 && *(BYTE *)i != 0xCC){
  384.                     RestoreProtect();
  385.                     return false;
  386.                 }
  387.             }
  388.             *RetAddr = (DWORD)NewMem;
  389.             *(WORD *)((DWORD)NewMem) = *(WORD *)ExportFunction;
  390.             WriteJMP((DWORD)NewMem + 2, ExportFunction + 2);
  391.             WriteJMP(ExportFunction - 5, (DWORD)Function);
  392.             *(WORD *)(ExportFunction) = 0xF9EB;
  393.             RestoreProtect();
  394.             return true;
  395.         }
  396.     }
  397.  
  398.     return false;
  399. }
  400.  
  401. void AirMemory::ArgumentsHook(DWORD Address, DWORD OverWrite, void *HookFunction){
  402.     DWORD NewMem = (DWORD)malloc(128);
  403.     Address = GetAbsoluteAddress(Address);
  404.     FullAccess((DWORD)NewMem, 128);
  405.     *(WORD *)(NewMem) = 0xC48B;//mov eax,esp
  406.     *(BYTE *)(NewMem + 2) = 0x60;//pushad
  407.     *(BYTE *)(NewMem + 3) = 0x50;//push eax
  408.     WriteCALL(NewMem + 4, (DWORD)HookFunction);//call
  409.     *(BYTE *)(NewMem + 9) = 0x61;//popad
  410.     MemoryCopy(NewMem + 10, (void *)Address, OverWrite);
  411.     WriteJMP(NewMem + 10 + OverWrite, Address + OverWrite);//jmp
  412.     WriteHook(Address, JMP, (void *)NewMem);
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement