Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: Karcrack en 23 Abril 2011, 15:34 pm



Título: [VB6][SNIPPET] mAPIPatchByID - Carga APIs dinamicamente (Late binding)
Publicado por: Karcrack en 23 Abril 2011, 15:34 pm
Código
  1. '---------------------------------------------------------------------------------------
  2. ' Module    : mAPIPatchByID
  3. ' Author    : Karcrack
  4. ' Now       : 23/04/2011 14:13
  5. ' Purpose   : Patch API functions by ID
  6. ' History   : 23/04/2011 First cut .........................................................
  7. '---------------------------------------------------------------------------------------
  8.  
  9. Option Explicit
  10.  
  11. 'KERNEL32
  12. Private Declare Function NtWriteVirtualMemory Lib "NTDLL" (ByVal hProcess As Long, ByRef lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesWritten As Long) As Long
  13.  
  14. Public Sub PatchAPIAddr(ByVal lID As Long, ByVal lAddr As Long)
  15.    Dim hInstance       As Long
  16.    Dim lExtTablePtr    As Long
  17.  
  18.    hInstance = App.hInstance
  19.    lExtTablePtr = GetDWORD(GetDWORD((hInstance + GetDWORD(hInstance + GetDWORD(hInstance + &H3C) + &H28)) + &H1) + &H30) + &H234
  20.    If GetDWORD(lExtTablePtr + &H4) >= lID Then
  21.        Call PutDWORD(GetDWORD(GetDWORD(GetDWORD(lExtTablePtr) + (8 * lID) + 4) + &H19), lAddr)
  22.    End If
  23. End Sub
  24.  
  25. Private Sub PutDWORD(ByVal lAddr As Long, ByVal lDWORD As Long)
  26.    Call NtWriteVirtualMemory(-1, ByVal lAddr, lDWORD, 4, ByVal 0&)
  27. End Sub
  28.  
  29. Private Function GetDWORD(ByVal lAddr As Long) As Long
  30.    Call NtWriteVirtualMemory(-1, GetDWORD, ByVal lAddr, 4, ByVal 0&)
  31. End Function

Para que sirve? Para cargar APIs dinamicamente :D

Un ejemplo:
Código
  1. Option Explicit
  2.  
  3. 'USER32
  4. Private Declare Function MessageBox Lib "nadaesloqueparece" Alias "Karcrack" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
  5. 'KERNEL32
  6. Private Declare Function GetProcAddress Lib "KERNEL32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
  7. Private Declare Function LoadLibrary Lib "KERNEL32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
  8.  
  9. Sub Main()
  10.    Call PatchAPIAddr(2, GetProcAddress(LoadLibrary("USER32"), "MessageBoxA"))
  11.    Call MessageBox(0, "Te has fijado en la declaracion del API 'MessageBox'?", "Hola :)", 0)
  12. End Sub
  13.  
Otro un poco mas enrevesado:
Código
  1. Option Explicit
  2.  
  3. 'USER32
  4. Private Declare Function fnc1& Lib "whatever" (ByVal a&, ByVal b&, ByVal c&, ByVal d&)
  5. Private Declare Function fnc2& Lib "whatever" (ByVal a&, ByVal b&)
  6. 'KERNEL32
  7. Private Declare Function GetProcAddress Lib "KERNEL32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
  8. Private Declare Function LoadLibrary Lib "KERNEL32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
  9.  
  10. Private Const u32$ = "ZXJW87"
  11. Private Const msgbx$ = "RjxxfljGt}\"
  12. Private Const ktmr$ = "PnqqYnrjw"
  13. Private Const stmr$ = "XjyYnrjw"
  14. Private x&
  15. Private bo&
  16.  
  17. Sub Main()
  18.    Dim p&
  19.  
  20.    p = GetProcAddress(LoadLibrary(d(u32)), d(stmr))
  21.    Call PatchAPIAddr(3, p)
  22.    x = fnc1(0, 0, 2 * 1000, AddressOf tproc)
  23.    p = GetProcAddress(LoadLibrary(d(u32)), d(msgbx))
  24.    Call PatchAPIAddr(3, p)
  25.    p = GetProcAddress(LoadLibrary(d(u32)), d(ktmr))
  26.    Call PatchAPIAddr(2, p)
  27.    bo = 1
  28.    While bo
  29.        DoEvents
  30.    Wend
  31. End Sub
  32.  
  33. Private Function d$(s$)
  34.    Dim i&
  35.    d = s
  36.    For i = 1 To Len(d)
  37.        Mid$(d, i, 1) = Chr$(Asc(Mid$(d, i, 1)) - 5)
  38.    Next i
  39. End Function
  40.  
  41. Private Function tproc&(ByVal a&, ByVal b&, ByVal c&, ByVal d&)
  42.    If fnc1(0, StrPtr("Seguimos?"), StrPtr(":)"), vbYesNo) = vbNo Then
  43.        bo = 0
  44.        Call fnc2(0, x)
  45.    End If
  46. End Function

Los ejemplos han de ir en un modulo aparte puesto después del modulo 'mAPIPatchByID' para que los IDs se correspondiesen... en caso contrario hay que calcular los IDs usando por ejemplo el OllyDbg :P

Cualquier duda preguntad!


Título: Re: [VB6][SNIPPET] mAPIPatchByID - Carga APIs dinamicamente (Late binding)
Publicado por: Abronsius en 24 Abril 2011, 16:04 pm
Excellent !!!! bye bye CallAPI


Título: Re: [VB6][SNIPPET] mAPIPatchByID - Carga APIs dinamicamente (Late binding)
Publicado por: CAR3S? en 24 Abril 2011, 16:35 pm
Diria que esta bueno.... pero no entendi para que sirve  >:D


Título: Re: [VB6][SNIPPET] mAPIPatchByID - Carga APIs dinamicamente (Late binding)
Publicado por: Swellow en 24 Abril 2011, 19:22 pm
ÔMG! You are the best Karcrack. It seems pretty hard to use, can you give some more explanations please?


Título: Re: [VB6][SNIPPET] mAPIPatchByID - Carga APIs dinamicamente (Late binding)
Publicado por: raul338 en 25 Abril 2011, 01:00 am
Los ejemplos han de ir en un modulo aparte puesto después del modulo 'mAPIPatchByID' para que los IDs se correspondiesen... en caso contrario hay que calcular los IDs usando por ejemplo el OllyDbg :P

Interesantisimo, pero ... no entiendo como sacar los ids :P


Título: Re: [VB6][SNIPPET] mAPIPatchByID - Carga APIs dinamicamente (Late binding)
Publicado por: LeandroA en 25 Abril 2011, 02:09 am
Hola, me da error 53 no se ha encontrado el archivo nadaesloqueparece, estoy en windows 7 32 bits
el id seria el ordinal del MessageBoxA ? , no testie si es el 2 quizas ese sea el problema.

Saludos.


Título: Re: [VB6][SNIPPET] mAPIPatchByID - Carga APIs dinamicamente (Late binding)
Publicado por: Abronsius en 25 Abril 2011, 02:42 am
Hola, me da error 53 no se ha encontrado el archivo nadaesloqueparece, estoy en windows 7 32 bits
el id seria el ordinal del MessageBoxA ? , no testie si es el 2 quizas ese sea el problema.

Saludos.

Compile, Open OllyDbg, Watch Api Order, set proper ID in source, recompile :)


Título: Re: [VB6][SNIPPET] mAPIPatchByID - Carga APIs dinamicamente (Late binding)
Publicado por: Karcrack en 26 Abril 2011, 16:27 pm
@LeandroA: No se usan los ordinales de las funciones, estos varian en cada version de W$, se utiliza el orden en el que estan declaradas las funciones en las estructuras del VB6 :D

You are right @Abronsius :)

He hecho una version que lee los primeros cuatro bytes del nombre de la funcion... mas sencilla de usar, no hay que revisar el orden:
Código
  1. Private Sub PatchAPIAddr(ByVal lHash As Long, ByVal lAddr As Long)
  2.    Dim lExtTablePtr    As Long
  3.    Dim lPtr            As Long
  4.    Dim i               As Long
  5.    lExtTablePtr = GetDWORD(GetDWORD((App.hInstance + GetDWORD(App.hInstance + GetDWORD(App.hInstance + &H3C) + &H28)) + &H1) + &H30) + &H234
  6.  
  7.    For i = 0 To GetDWORD(lExtTablePtr + &H4)
  8.        lPtr = GetDWORD(GetDWORD(lExtTablePtr) + (8 * i) + 4)
  9.        If GetDWORD(GetDWORD(lPtr + 4)) = lHash Then
  10.            Call PutDWORD(GetDWORD(lPtr + &H19), lAddr)
  11.            Exit For
  12.        End If
  13.    Next i
  14. End Sub

Ejemplo:
Código:
Private Declare Function MessageBoxA Lib "" Alias "01" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long

sub Main()
  PatchAPIAddr(&H3130,pPunteroDeMessageBoxA)
  MessageBoxA(0, "HOOLA", ":D",0)
end

Saludos :D