Foro de elhacker.net

Seguridad Informática => Análisis y Diseño de Malware => Mensaje iniciado por: fary en 5 Febrero 2015, 16:45 pm



Título: Hook API Calculando cantidad opcodes.
Publicado por: fary en 5 Febrero 2015, 16:45 pm
Bueno este código lo tenía por ahí de hace un par de años, y al final lo voy a perder así que aquí os lo dejo por a alguien le sirve.

Código
  1. ; Programado por mDrinky o Juan fary.
  2. ; drinky.94@hotmail.com
  3.  
  4. include 'win32ax.inc'
  5.  
  6. ; Instrucciones a las que es capaz de sacar la longitud.
  7.  
  8. ; mov esp,ebp                          = 0x8B  2 bytes
  9. ; push ebp                             = 0x55  1 byte
  10. ; mov Reg,DWORD                        = 0xB8 + Registro   5 bytes
  11. ; ret                                  = 0xC3 1 byte
  12. ; ret Numero                           = 0xC2 2 bytes
  13. ; mov dword[Direccion],Numero          = 0x83 7 bytes
  14.  
  15. .data
  16.    DLL  db 'USER32.DLL',0
  17.    API  db 'MessageBoxA',0
  18.    MsgHook  db 'Api Hookeada',0
  19.  
  20.    ApiBuena  dd ?
  21.    CanSalto  dd ?
  22.    oPer  dd ?
  23.    Salto  dd ?
  24.  
  25. .code
  26. start:
  27.    invoke LoadLibraryA,DLL
  28.    invoke GetProcAddress,eax,API
  29.    mov [ApiBuena],eax
  30.  
  31.    push MiFuncion
  32.    Call Hookear
  33.  
  34.    push 0
  35.    push 0
  36.    push 0
  37.    push 0
  38.  
  39.    call [MessageBoxA]
  40.  
  41.    push 0
  42.    call [ExitProcess]
  43.  
  44.  
  45. MiFuncion:
  46.    pop ecx
  47.  
  48.    add esp,0xC
  49.  
  50.    push 0
  51.    push 0
  52.    push MsgHook
  53.    push 0
  54.  
  55.    push ecx
  56.  
  57.    ApiVerdadera:
  58.    db 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90
  59.  
  60.    push eax
  61.  
  62.    mov eax,[ApiBuena]
  63.    add eax,[CanSalto]
  64.    mov [Salto],eax
  65.  
  66.    pop eax
  67.  
  68.    jmp [Salto]
  69.  
  70.  
  71. proc Hookear,FunHook
  72.  
  73.    mov ecx,0
  74.  
  75.    NumOpcode:
  76.  
  77.    cmp byte[eax],0x8B
  78.    jne NoOp1
  79.  
  80.    add ecx,2
  81.    add eax,2
  82.  
  83.    NoOp1:
  84.  
  85.    cmp byte[eax],0x55
  86.    jne NoOp2
  87.  
  88.    add ecx,1
  89.    add eax,1
  90.  
  91.    NoOp2:
  92.  
  93.    cmp byte[eax],0xB8
  94.    jb NoOp3
  95.    cmp byte[eax],0xBD
  96.    ja NoOp3
  97.  
  98.    add ecx,5
  99.    add eax,5
  100.  
  101.    NoOp3:
  102.  
  103.    cmp byte[eax],0xC3
  104.    jne NoOp4
  105.  
  106.    add ecx,1
  107.    add eax,1
  108.  
  109.    NoOp4:
  110.  
  111.    cmp byte[eax],0xC2
  112.    jne NoOp5
  113.  
  114.    add ecx,2
  115.    add eax,2
  116.  
  117.    NoOp5:
  118.  
  119.    cmp byte[eax],0x83
  120.    jne NoOp6
  121.  
  122.    add ecx,7
  123.    add eax,7
  124.  
  125.    NoOp6:
  126.  
  127.    cmp ecx,5
  128.    jb NumOpcode
  129.  
  130.    mov [CanSalto],ecx
  131.  
  132.    invoke VirtualProtect,[ApiBuena],20,PAGE_EXECUTE_READWRITE,addr oPer
  133.    invoke VirtualProtect,ApiVerdadera,20,PAGE_EXECUTE_READWRITE,addr oPer
  134.  
  135.    mov eax,[ApiBuena]
  136.    mov edx,ApiVerdadera
  137.  
  138.    mov ecx,0
  139.  
  140.    GuardarBytes:
  141.    mov bl,byte[eax+ecx]
  142.    mov byte[edx+ecx],bl
  143.  
  144.    inc ecx
  145.  
  146.    cmp ecx,[CanSalto]
  147.    jne GuardarBytes
  148.  
  149.    mov eax,[ApiBuena]
  150.  
  151.    mov byte[eax],0x68
  152.    inc eax
  153.  
  154.    mov ebx,dword[FunHook]
  155.    mov dword[eax],ebx
  156.  
  157.    add eax,4
  158.  
  159.    mov byte[eax],0xC3
  160.  
  161.    ret
  162. endp
  163.  
  164. .end start

No recalcula todas las API pero si la gran mayoría.

saludos.