hash演算法搜尋獲得api函數地址的實現

來源:互聯網
上載者:User

標籤:blank   include   hash   wine   cad   xor   字元   data   學習   

  我們一般要獲得一個函數的地址,通常採用的是明文,例如定義一個api函數字串"MessageBoxA",然後在GetProcAddress函數中一個位元組一個位元組進行比較。這樣弊端很多,例如如果我們定義一個殺毒軟體比較敏感的api函數字串,那麼可能就會增加殺毒軟體對我們的程式的判定值,而且定義這些字串還有一個弊端是佔用的位元組數較大。我們想想如何我們的api函數字串通過演算法將它定義成一個4位元組的值,然後在GetProcAddress中把AddressOfNames表中的每個地址指向的api字串通過我們的演算法壓縮成4位元組值後,與我們之前定義的4位元組值進行判斷,如果匹配成功則讀取函數地址。

  我們來看一種rol 3移位演算法,這個演算法是每次將目的地址迴圈向左移動3位,然後將低位元組與源字串的每個位元組進行異或。演算法很精巧方便。還有很多方便小巧的演算法,例如ROR 13等演算法,其實它和我們上面的基本一樣,只不過它將函數名表的字串通過我們的演算法過程獲得hash值後與我們之前定義的hash值進行匹配,匹配成功則獲得對應函數的地址。

  下面的代碼通過hash搜尋WinExec函數來運行Clac:

 .386      .model flat, stdcall      option casemap:none        include windows.inc  include user32.inc  include kernel32.inc  includelib user32.lib  includelib kernel32.lib        .const  szCalc      db ‘calc.exe‘, 0        .code        _GetKrnl32 proc  ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>; 擷取kernel32.dll的地址,因為xp和win7不一樣; 故採用比較名稱的方式擷取地址,具體見Kernel32基地址獲得學習; http://blog.csdn.net/programmingring/article/details/11357393;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>    assume fs:nothing      mov eax, fs:[30h]      mov eax, [eax + 0ch]      mov eax, [eax + 1ch]        ; 第一個LDR_MODULE    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>; 壓入kernel32.dll的unicode字串;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>      push dword ptr 006ch      push dword ptr 6c0064h      push dword ptr 2e0032h      push dword ptr 33006ch      push dword ptr 65006eh      push dword ptr 720065h      push word ptr 006bh      mov ebx, esp              ; ebx指向字串    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>; 開始比較;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>      _Search:      cmp eax, 0      jz _NotFound      cmp eax, 0ffffffffh      jz _NotFound      lea esi, [eax + 1ch]          ; esi指向UNICODE_STRING結構    xor ecx, ecx                  mov cx, 13              ; 比較的長度    mov esi, [esi + 4]          ; 獲得UNICODE_STRING結構的Buffer成員    mov edi, ebx              ; edi指向kernel32unicode字串    repz cmpsw      or ecx, ecx              ; ecx減完說明相等    jz _Found      mov eax, [eax]              ; 擷取下一個LDR_MODULE    jmp _Search        _NotFound:      mov eax, 0ffffffffh      jmp _Over        _Found:      mov eax, [eax + 08h]          ; 獲得地址      _Over:      add esp, 26      ret        _GetKrnl32 endp    _GetRolHash proc _lpApiString            mov eax, _lpApiString     push esi      xor edx, edx      xchg eax, esi              ; esi = _lpApiString    cld                  ; 遞增      _Next:          lodsb                  ; 從esi指向的地址中取一個字元    test al, al              ; 比較是否為0    jz _Ret      rol edx, 3              ; 左迴圈移位3位    xor dl, al              ; 低位元組和字元異或    jmp _Next        _Ret:      xchg eax, edx              ; eax = 處理後的_lpApiString的hash    pop esi      ret        _GetRolHash endp    _GetApi proc _hDllHandle, _iHashApi            push ebp      mov eax, _hDllHandle      mov ecx, _iHashApi      mov ebx, eax      mov edi, ecx            mov eax, [ebx + 3ch]      mov esi, [ebx + eax + 78h]          ; Get Export RVA      lea esi, [esi + ebx + IMAGE_EXPORT_DIRECTORY.NumberOfNames]      lodsd      xchg eax, edx                   ; edx = NumberOfNames      lodsd      push eax                        ; [esp] = AddressOfFunctions      lodsd      xchg eax, ebp                      ; ebp = AddressOfNames      lodsd      xchg eax, ebp                   ; ebp = AddressOfNameOrdinals, eax = AddressOfNames      add eax, ebx      xchg eax, esi                   ; esi = AddressOfNames        _LoopScas:      dec edx      js _Ret      lodsd                  ; eax = api名稱的rva    add eax, ebx              ; eax = api名稱的實際地址    push edx      invoke _GetRolHash, eax         ; 計算hash字串            pop edx      cmp eax, edi              ; 比較和傳入的hash參數是否相等    jz _GetAddr            add ebp, 2              ; 遞增,和esi相對應    jmp _LoopScas        _GetAddr:      movzx eax, word ptr [ebp + ebx]      ; 取得序號值    shl eax, 2              ; 乘4    add eax, [esp]              ; 在AddressOfFunctions取得相應序號值位置的值    mov eax, [ebx + eax]          ; 取得函數地址    add eax, ebx        _Ret:      pop ecx      pop ebp      ret        _GetApi endp    _WinMain proc        invoke _GetKrnl32      invoke _GetApi, eax, 016ef74bh      ; "WinExec"的hash      push SW_SHOW      lea ebx, szCalc      push ebx      call eax      ret        _WinMain endp  start:      call _WinMain      invoke ExitProcess, 0            end start  

 

hash演算法搜尋獲得api函數地址的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.