[SOURCE][RET Exe Corruption] Corrompe cualquier Ejecutable

Iniciado por Karcrack, 7 Abril 2009, 19:01 PM

0 Miembros y 1 Visitante están viendo este tema.

Karcrack

Codigo relativo a este post:
Citar[RET Exe Corruption] Corrompe cualquier Ejecutable




Código (vb) [Seleccionar]
'---------------------------------------------------------------------------------------
' Modulo    : mPatchExe
' Autor     : Karcrack
' Fecha-Hora: 07/04/2009  18:43
' Finalidad : Deshabilita cualquier ejecutable
'---------------------------------------------------------------------------------------

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal L As Long)

Private Enum ImageSignatureTypes
    IMAGE_DOS_SIGNATURE = &H5A4D     ''\\ MZ
    IMAGE_OS2_SIGNATURE = &H454E     ''\\ NE
    IMAGE_OS2_SIGNATURE_LE = &H454C  ''\\ LE
    IMAGE_VXD_SIGNATURE = &H454C     ''\\ LE
    IMAGE_NT_SIGNATURE = &H4550      ''\\ PE\0\0
End Enum

Private Type IMAGE_DOS_HEADER
    e_magic As Integer        ' Magic number
    e_cblp As Integer         ' Bytes on last page of file
    e_cp As Integer           ' Pages in file
    e_crlc As Integer         ' Relocations
    e_cparhdr As Integer      ' Size of header in paragraphs
    e_minalloc As Integer     ' Minimum extra paragraphs needed
    e_maxalloc As Integer     ' Maximum extra paragraphs needed
    e_ss As Integer           ' Initial (relative) SS value
    e_sp As Integer           ' Initial SP value
    e_csum As Integer         ' Checksum
    e_ip As Integer           ' Initial IP value
    e_cs As Integer           ' Initial (relative) CS value
    e_lfarlc As Integer       ' File address of relocation table
    e_ovno As Integer         ' Overlay number
    e_res(0 To 3) As Integer  ' Reserved words
    e_oemid As Integer        ' OEM identifier (for e_oeminfo)
    e_oeminfo As Integer      ' OEM information; e_oemid specific
    e_res2(0 To 9) As Integer ' Reserved words
    e_lfanew As Long          ' File address of new exe header
End Type

' MSDOS File header
Private Type IMAGE_FILE_HEADER
    Machine As Integer
    NumberOfSections As Integer
    TimeDateStamp As Long
    PointerToSymbolTable As Long
    NumberOfSymbols As Long
    SizeOfOptionalHeader As Integer
    characteristics As Integer
End Type

' Directory format.
Private Type IMAGE_DATA_DIRECTORY
    VirtualAddress As Long
    Size As Long
End Type

' Optional header format.
Const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16

Private Type IMAGE_OPTIONAL_HEADER
    ' Standard fields.
    Magic As Integer
    MajorLinkerVersion As Byte
    MinorLinkerVersion As Byte
    SizeOfCode As Long
    SizeOfInitializedData As Long
    SizeOfUnitializedData As Long
    AddressOfEntryPoint As Long
    BaseOfCode As Long
    BaseOfData As Long
    ' NT additional fields.
    ImageBase As Long
    SectionAlignment As Long
    FileAlignment As Long
    MajorOperatingSystemVersion As Integer
    MinorOperatingSystemVersion As Integer
    MajorImageVersion As Integer
    MinorImageVersion As Integer
    MajorSubsystemVersion As Integer
    MinorSubsystemVersion As Integer
    W32VersionValue As Long
    SizeOfImage As Long
    SizeOfHeaders As Long
    CheckSum As Long
    SubSystem As Integer
    DllCharacteristics As Integer
    SizeOfStackReserve As Long
    SizeOfStackCommit As Long
    SizeOfHeapReserve As Long
    SizeOfHeapCommit As Long
    LoaderFlags As Long
    NumberOfRvaAndSizes As Long
    DataDirectory(0 To IMAGE_NUMBEROF_DIRECTORY_ENTRIES - 1) As IMAGE_DATA_DIRECTORY
End Type

Private Type IMAGE_NT_HEADERS
    Signature As Long
    FileHeader As IMAGE_FILE_HEADER
    OptionalHeader As IMAGE_OPTIONAL_HEADER
End Type

' Section header
Const IMAGE_SIZEOF_SHORT_NAME = 8

Private Type IMAGE_SECTION_HEADER
   SecName As String * IMAGE_SIZEOF_SHORT_NAME
   VirtualSize As Long
   VirtualAddress  As Long
   SizeOfRawData As Long
   PointerToRawData As Long
   PointerToRelocations As Long
   PointerToLinenumbers As Long
   NumberOfRelocations As Integer
   NumberOfLinenumbers As Integer
   characteristics  As Long
End Type

'---------------------------------------------------------------------------------------
' Procedimiento : PatchExe
' Autor         : Karcrack
' Fecha         : 07/04/2009
' Parametro(s)  : sPath -> La ruta del fichero
' Return        : True si todo fue bien
'---------------------------------------------------------------------------------------

Public Function PatchExe(ByVal sPath As String) As Boolean
    On Error GoTo Fallo
    Dim IDH             As IMAGE_DOS_HEADER
    Dim INH             As IMAGE_NT_HEADERS
    Dim ISH()           As IMAGE_SECTION_HEADER
   
    Dim bvCode()        As Byte
    Dim PE              As Long
    Dim i               As Long
    Dim Section         As Long
   
    bvCode = ReadFile(sPath)                                                        'Leemos el fichero
   
    Call CopyMemory(IDH, bvCode(0), Len(IDH))                                       'Leemos la info del PE
    Call CopyMemory(INH, bvCode(IDH.e_lfanew), Len(INH))                            'Leemos la info del PE
   
    For i = 0 To INH.FileHeader.NumberOfSections - 1
        ReDim Preserve ISH(0 To i)
        Call CopyMemory(ISH(i), bvCode(IDH.e_lfanew + Len(INH) + Len(ISH(i)) * i), Len(ISH(i)))
        If (INH.OptionalHeader.AddressOfEntryPoint => ISH(i).VirtualAddress) And (INH.OptionalHeader.AddressOfEntryPoint =< ISH(i).VirtualAddress + ISH(i).VirtualSize) Then
            Section = i
            Exit For
        End If
    Next i
   
    bvCode(INH.OptionalHeader.AddressOfEntryPoint - ISH(i).VirtualAddress + ISH(i).PointerToRawData) = &HC3 'Parcheamos el fichero (C3=RET)
   
    Call SaveFile(bvCode, sPath)
   
    PatchExe = True                                                                 'Todo funciono
    Exit Function                                                                   'Salimos
Fallo:
    PatchExe = False                                                                'Algo ha ido mal :S
End Function


'---------------------------------------------------------------------------------------
' Procedimiento : ReadFile
' Autor         : Karcrack
' Fecha         : 07/04/2009
' Parametro(s)  : sPath -> La ruta del fichero
' Return        : Devuelve un Byte array con los bytes del fichero
'---------------------------------------------------------------------------------------

Private Function ReadFile(ByVal sPath As String) As Byte()
    Dim bvTmp()         As Byte
   
    Open sPath For Binary As #1
        ReDim bvTmp(0 To LOF(1) - 1)
        Get #1, , bvTmp
    Close #1
   
    ReadFile = bvTmp
End Function


'---------------------------------------------------------------------------------------
' Procedimiento : SaveFile
' Autor         : Karcrack
' Fecha         : 07/04/2009
' Parametro(s)  : bvData() -> Array de datos
'                 sPath    -> Ruta de guardado
'---------------------------------------------------------------------------------------

Private Sub SaveFile(ByRef bvData() As Byte, ByVal sPath As String)
    Open sPath For Binary As #1
        Put #1, , bvData
    Close #1
End Sub

el_c0c0

muy interesante, esta seria la forma logica, aunque reemplazando 2 o 3 kb del principio ya lo cagas.
saludos
'-     coco
"Te voy a romper el orto"- Las hemorroides

Karcrack

Cita de: el_c0c0 en  8 Abril 2009, 03:27 AM
muy interesante, esta seria la forma logica, aunque reemplazando 2 o 3 kb del principio ya lo cagas.
saludos
Si, solo con reemplazar el primer byte (M) ya no funciona... pero muestra una fea pantalla negra y se pierde el icono...

Dessa

#3

Está bueno el code, se mantiene todo tal cual, icono y propiededes del ejecutable, me sirve para seguridad.

Saludos
Adrian Desanti

YST

Tambien modificando el SizeOfImage a un valor malo te da que no es una aplicación valida , aunque muy util el metodo para desabilitar programas como el regedit ;) .


Yo le enseñe a Kayser a usar objetos en ASM