home *** CD-ROM | disk | FTP | other *** search
/ The Elite Hackers Toolkit / TheEliteHackersToolkitVolume1_1998.rar / HACKERS.BIN / appcraks / UCFPE113.ZIP / STNPEE.ASM < prev    next >
Assembly Source File  |  1998-01-18  |  25KB  |  865 lines

  1. Comment % Stone's PE-ExeEncrypter v1.13
  2.       This program is dedicated to the person that thought me the concept
  3.       of happiness and the those few who makes the meaningness of life
  4.       seem unimportant to me!
  5.  
  6.  
  7.       Contacting: I can be emailed at: stone@one.se
  8.       
  9.       Credits:
  10.       This could not have been made without the efforts of:
  11.       Random/UCF, you are the PeKing..
  12.       Acp/UCF, Who is my mentor in Windows assembly coding
  13.       Patriarch/PWA, My landlord and friend! Thanks for it all!
  14.       Rose, My betatester (blame him for any bugs :) nahhh)
  15.       Marquis De Soiree/UCF, for the support and betatesting
  16.       DjHD/UCF, thanks for telling it didn't run under NT :)
  17.       Stefan Esser, you greet me - I greet  you :)
  18.       United Cracking Force & Xtremist INC, my "sponsors" and benefactors
  19.  
  20.       Numbers.inc is written by Patriarch/PWA, modified by Acp/UCF to fit
  21.       the windows console and finally custumized by me.
  22.  
  23.       Console.inc is written by Acp/UCF
  24.  
  25.       All bugs and errors are of my doing (those that doesn't belong to
  26.       microsoft for inventing this weird format) the above people only
  27.       contributed in making this better
  28.  
  29.       Usage: stnplee.exe <PEFILE>
  30.  
  31.     %
  32.  
  33.  
  34. subvalue EQU 1            ; 0 = encryption off, anything else=on (debug variable)
  35. version EQU "v1.13"        ; First release candidate with DLL support
  36.  
  37. .386P                ; 386 Priveledged mode... 
  38. Locals
  39. jumps
  40.  
  41. .Model Flat ,StdCall
  42.  
  43.  
  44. ;Define the needed external functions and constants here.
  45. Extrn           ExitProcess    : PROC  
  46. Extrn           CreateFileA    : PROC                
  47. Extrn        SetFilePointer    : PROC
  48. Extrn        MessageBeep    : PROC
  49. Extrn        CloseHandle    : PROC
  50. Extrn        ReadFile    : PROC
  51. Extrn        WriteFile    : PROC
  52. Extrn        GetCommandLineA : PROC
  53. Extrn        GetLastError    : PROC
  54.  
  55. .Data                                        
  56. Title1          db "Stone's  PeExeEncrypter",0        ; Tittle of Program
  57. FileAttr    dd 0            ; Standard File Attributes = none
  58. FileHandle    dd 0             ; Store the FileHandle Here
  59. BytesRead    dd 0             ; We read this many bytes
  60. ObjTblOff    dd 0            ; Object table located here (displacement from PEheader)
  61. PeHeaderOff     dd 0            ; File offset of PEHEADER
  62. CurrentRVA    dd 0             ; Current RVA while encrypting
  63. ITRVA        dd 0            ; RVA of IT
  64. ITENDRVA    dd 0            ; END of IT
  65. ObjectDone    db 0            ; Are we finished with this object?
  66. ;────────────────────Decryption routine Object Table ─────────────────────────
  67.  
  68. ObjectForm:                           ; the new object (size / total)
  69. oname         db ".Stone",0,0        ; Name of the object   (8 / 8)
  70. VirtualSize    dd 0            ; Virtual Size of object (4 / 12)
  71. RVA        dd 0            ; Relative Virtual Adress of obj (4 /16)
  72. PhysicalSize    dd 0            ; Physical Size (4 / 20)
  73. PhysicalOffset    dd 0            ; (4 / 24)
  74. Reserved    dd 0,0,0        ; (12 / 36)
  75. objectflags    db 40h,0,0,0c0h        ; Initialized data object, (4 / 40)
  76.                     ; nada, nada
  77.                     ; 
  78.                     ; 80 = W, 40 =R, 20 = E (I think - see PE.TXT for more INFO)
  79.  
  80. ;─────────────────────────────The PE-HEader───────────────────────────────────
  81. PePrimSize     EQU 88            ; Bytes in PEHeader /wo ObjTbl n'stuff
  82.  
  83. PEHeader:                        ; essential part of the pe header
  84. signature     dd 0            ;+0
  85. cputype     dw 0            ;+4    
  86. NumObj         dw 0            ;+6
  87.         db 3*4 dup (0)        ;+8  crap
  88. NtHeaderSize     dw 0            ;+20=1eh
  89. Flags         dw 0            ;+22
  90.         db 4*4 dup (0)        ;+24 crap
  91. entrypointRVA     dd 0            ;+40 - entry point relative to IB
  92.         db 2*4 dup (0)        ;+44 Crap
  93. ImageBase    dd 0                    ;+52 - every RVA is relative to this
  94. ObjAlign     dd 0            ;+56
  95. FileAlign     dd 0            ;+60
  96.         db 4*4 dup (0)        ;+64 Crap
  97. ImageSize     dd 0            ;+80
  98. HeaderSize     dd 0            ;+84
  99. vend:                    ;+88 = 58h
  100. RestOfHead     db 3000 dup (0)        ; Rest of the header (plenty space)
  101. ;──────────────Encryption data────────────────────────────────────────────────
  102. EncryptBuffer    db 16 dup(0)        ; Buffer
  103. CurrentPos    dd 0            ; Current Position in PE-EXE
  104. CurrDisp    dd 0            ; Displacement of Objectdata in Appedix
  105. OutOff        dd 0            ; offset of something to go out
  106. ;───────────────────────Messages ─────────────────────────────────────────────
  107. MsgIntro        db "-[X]- Stone's PE-EXE Encrypter ",version," -[X]-",13,10
  108.             DB "──────────────|   STONE   |──────────────",10,13
  109.             DB "  ▄▄▄   ▄▄▄    ▄▄▄▄▄▄▄▄▄    ▄▄▄▄▄▄▄▄▄    ",10,13
  110.             DB "  ███   ███    ███          ███▄▄▄       ",10,13
  111.             DB "  ███▄  ███    ███▄         ███          ",10,13
  112.             DB "  ▀▀▀▀▀▀▀▀▀    ▀▀▀▀▀▀▀▀▀    ▀▀▀          ",10,13
  113.             DB "─────────────────────────────────────────",10,13
  114.             DB "u N I T E D  c R Æ C K I N G  f O R C E  ",10,13
  115.             DB "[wIN95/NT]─────────────────────[Jan 1998]",10,13
  116.         db "Legend: [i] = INFO, [X] = Status, [!] = Error",10,13
  117. LenIntro    EQU $-MsgIntro
  118.  
  119. MsgNoCmdLine    db " [i] Usage: StnPee.exe <INPUT FILE>",10,13
  120. LenNoCmdLine    EQU $-MsgNoCmdLine
  121. MsgOpenErr    db " [!] An error has occured opening the file error code: "
  122. LenOpenErr    EQU $ - MsgOpenErr
  123.  
  124. MsgOpenOk    db " [X] File Opened Successfully",10,13
  125. LenOpenOk    EQU $ - MsgOpenOk
  126.  
  127. MsgPEOk        db " [X] Header Loaded Successfully ",10,13
  128. LenPEOk        EQU $-MsgPEOk
  129.  
  130. MsgNotPE     db " [!] This Appears not to be a Valid PE file! :(",10,13
  131. LenNotPE     EQU $-MsgNotPE
  132.  
  133.  
  134. MsgErrRead     db " [!] An error has occured reading the file, error code: "
  135. LenErrRead     EQU $-MsgErrRead
  136.  
  137. MsgAppendSize    db " [i] Appending Bytes            : "
  138. LenAppendSize    EQU $-MsgAppendSize
  139.  
  140. MsgPeHOff     db " [i] Offset of PeHeader is      : "
  141. LenPeHOff     EQU $-MsgPeHOff
  142.  
  143. MsgNumObj     db " [i] Original Number of objects : "
  144. LenNumObj     EQU $-MsgNumObj
  145.  
  146. MsgObjAlg    db " [i] Object Alignment           : "
  147. LenObjAlg    EQU $-MsgObjAlg
  148.  
  149. MsgHSize    db " [i] HeaderSize                 : "
  150. LenHSize    EQU $-MsgHSize
  151.  
  152. MsgImageSize    db " [i] ImageSize                  : "
  153. LenImageSize    EQU $-MsgImageSize
  154.  
  155. MsgEPRVA    db " [i] Original EntryPoint RVA    : "
  156. LenEPRVA    EQU $-MsgEPRVA
  157.  
  158. MsgIB        db " [i] ImageBase                  : "
  159. LenIB        EQU $-MsgIB
  160.  
  161. MsgH         db "h",10,13
  162. LenH         EQU $-MsgH
  163.  
  164. MsgH2        db "h  "
  165. LenH2        EQU $-MsgH2
  166.  
  167. MsgSpace    db "      "
  168. LenSpace    EQU $-MsgSpace
  169.  
  170.  
  171. MsgObjLine    db "Objectname      VirSize        RVA   PhysSize    PhysPos      Flags  Encrypted"
  172. LenObjLine    EQU $-MsgObjLine
  173.  
  174. MsgLfCf        db 10,13
  175. LenLfCf        EQU $-MsgLfCf
  176.  
  177. MsgYup        db "Yup!"
  178. LenYup        EQU $-MsgYup
  179.  
  180. MsgOhfuck    db 10,13," [i] It appears that this file was linked with an old linker - this means that it is liable to chrash your computer when running it!",10,13        ; Debug-bug message! :)
  181. LenOhfuck    EQU $-MsgOhfuck
  182.  
  183. MsgNoObjSpace    db " [!] Sorry - No space in the Object table! - aborting",10,13
  184. LenNoObjSpace    EQU $-MsgNoObjSpace
  185.  
  186. ;──────────────────────────────────────────────────────────────────────────────
  187.  
  188. .Code                                  
  189. Include Console.inc
  190. Include Numbers.inc
  191.  
  192. Main:
  193.     CALL    Init_Console        ; Init Console for output
  194.  
  195.     Write_Console <offset MsgIntro> <LenIntro> ; Write intro text
  196.  
  197.     call    GetCommandLineA        ; get commandline
  198.     mov     edi,eax            ; => EDI -> Commandline
  199.     mov     ecx, -1
  200.     mov     al,0
  201.     push     edi
  202.     CLD
  203.     repnz     scasb
  204.     pop     edi
  205.     not     ecx            ; => ECX = length of commandline
  206.     mov     al,20h
  207.     repnz     scasb            ; the the space before the parameter
  208.     repz     scasb            ; remove superflousious spaces
  209.     test    ecx,ecx            ; test if anything at all was written
  210.     jz    NoCommandLine
  211.     dec     edi    
  212.     cmp    byte ptr [edi], '/'    ; Did the user try /H or something?
  213.     jz    NoCommandLine
  214.  
  215.     CALL    OpenRW             ; Open Read Write
  216.     CMP    EAX, -1    
  217.     JNZ    NoOpenErr
  218.  
  219.     Call    GetLastError
  220.     Write_Console <offset MsgOpenErr> <LenOpenErr>        
  221.     call     Hex2Dec
  222.     Write_Console <offset MsgH> <LenH>
  223.  
  224.     JMP    Exit_Proc
  225.  
  226. NoOpenErr:
  227.     MOV    FileHandle, EAX
  228.     Write_Console <offset MsgOpenOk>  <LenOpenOk>
  229.  
  230.     PUSH    0
  231.     PUSH    0
  232.     PUSH    3ch            ; Set File Pointer to 3ch 
  233.                     ; Dos relocation table
  234.                     ; on this place PEfiles store
  235.                     ; the offset of the PEHeader!
  236.     PUSH    DWord ptr [FileHandle]
  237.     CALL    SetFilePointer
  238.  
  239. ;------- Read Offset of PE-header
  240.     PUSH    0            ; Something
  241.     PUSH    offset BytesRead    ; Bytes read
  242.     PUSH     4            ; Bytes to read
  243.     PUSH     offset PeHeaderOff    ; Read to here 
  244.     PUSH     DWord ptr [FileHandle]    ; From here
  245.     CALL     ReadFile
  246.     CMP    EAX,-1
  247.     JZ    Error_Read
  248.  
  249. ;--------    Set file pointer to PE header
  250.     PUSH     0            ; Method 0 (From begining of file)
  251.     PUSH     0
  252.     PUSH     DWord ptr [PeHeaderOff]    ; To this offset
  253.     PUSH     DWord ptr [FileHandle]    ; on this file
  254.     CALL     SetFilePointer
  255.  
  256. ;-------- read PE-header (preliminary)
  257.     PUSH     0
  258.     PUSH     offset BytesRead    ; This many bytes read
  259.     PUSH     PePrimSize        ; read this many bytes (88=size of
  260.                     ; "main" PEheader)
  261.     PUSH     offset PEHeader        ; To here
  262.     PUSH     DWord ptr [FileHandle]    ; from this file
  263.     CALL     ReadFile
  264.  
  265. ;------ Check PE-header signature
  266.     CMP    DWord ptr [PEHeader], 00004550h    ; 0,0,E,P (PE Signature)
  267.     JNZ    Not_PE                ; If not terminate /W errormsg
  268.     Write_Console <Offset MsgPEOk> <LenPEOk>
  269.  
  270. ;------ Read the rest of the PE header
  271.     PUSH     0                ; 0
  272.     PUSH     offset BytesRead        ; bytes read
  273.     MOV     EAX, HeaderSize            ; To read
  274.     SUB     EAX, PePrimSize            ; except what we already read
  275.     SUB    EAX, [PeHeaderOff]
  276.     PUSH     EAX                ; on stack
  277.     PUSH     offset RestOfHead        ; To rest of the header
  278.     PUSH     DWord ptr [FileHandle]        ; From
  279.     CALL     ReadFile            ; Read IT! :)
  280.     CMP     EAX, -1
  281.     JZ     Error_Read
  282.  
  283. ;------ Write Stuff
  284.     CALL    WriteInfo            ; Write some stuff!
  285.  
  286.  
  287. ;------ Locate Object table
  288.     movzx     EAX, word ptr [NtHeaderSize]    ; EAX=0,0,NtHeaderSize
  289.     ADD     EAX, 24                ; Flags=+24
  290.     MOV     [ObjTblOff], EAX        ; (DISPLACEMENT FROM PEHeader)
  291.  
  292. ;----- Is there space in the object table for another object?
  293.     pushad                    
  294.     movzx    eax, [NumObj]            ; Number of objects
  295.     inc    eax                ; + the on we'll add
  296.     mov     ecx, 40d            
  297.     mul     ecx                ; times 40 (size of a object record)
  298.     add    eax, [ObjTblOff]        ; add displacement from PeHeader
  299.     add    eax, [PeHeaderOff]        ; Add the PEHeader's File offset
  300.     cmp    eax, [HeaderSize]        ; compare it to the headers's size
  301.     popad
  302.     jg    NoObjSpace
  303.  
  304.  
  305.     
  306. ;------    Encrypt objects!!
  307.     CALL     Encrypt                ; encrypt everything :)
  308.     MOV     ESI, [ImageBase]
  309.  
  310.  
  311.     CMP    ESI, 10000h            ; Issue old linker warning!
  312.     JNZ    NotOldPe            ; should I keep this mesg?
  313.     Write_Console <Offset MsgOhfuck> <LenOhfuck>
  314. NotOldPe:
  315.  
  316. ;----- Locate Last object in Object table
  317.     LEA     ESI, [PEHeader]            ; OFfset PEheader
  318.     ADD     ESI,EAX                ; Adjust ESI=real Offset(ObjTable)
  319.  
  320.     movzx     EAX, [NumObj]            ; Ax= Number of objects
  321.     MOV     ECX, 40                ; 40d bytes each (see ObjectForm)
  322.     XOR     EDX,EDX                ; EDX=0 (cLEAn division)
  323.     MUL     ECX                ; => EAX= Number of bytes
  324.                         ; In objecttable
  325.     
  326.  
  327.     ADD     ESI, EAX            ; => ESI= offset of unADDed
  328.     
  329.     
  330.  
  331. ;------ ADD another object!
  332.     INC     word ptr [NumObj]        ; That's the number of them :)
  333.  
  334.     LEA     EDI,[ObjectForm]
  335.     XCHG     EDI,ESI                ; ESI = NewObject (form)
  336.                         ; EDI = NewObject (in table)
  337. ;------ Calculate new Object's RVA
  338.     MOV     EAX,[EDI-40+8]            ; Previous object VirtualSize
  339.     ADD     EAX,[EDI-5*8+12]        ; Previous object's RVA
  340.     MOV     ECX, DWord ptr [ObjAlign]       ; Alignment
  341.     XOR     EDX,EDX                ; CLEAr EDX for  32 bit DIVision
  342.     DIV     ECX                ; How many Aligned "pages" did 
  343.     TEST    EDX,EDX                ; Was the previous page aligned?
  344.     JZ    PrevAligned
  345.     INC     EAX                ; it use - ADD another "page"
  346. PrevAligned:
  347.     MUL     ECX                ; back to bytes
  348.     MOV     DWord ptr [RVA],EAX        ; (This smells.. )
  349.     MOV    [AppVirOff], EAX
  350.  
  351. ;------ Calculate New objects Phys Size
  352.     MOV     ECX, DWord ptr [FileAlign]    ; File "page" size
  353.     MOV     EAX, AppendEnd-AppendStart    ; Size of appention code
  354.     MOV     DWord ptr [PhysicalSize],EAX
  355.  
  356. ;------ Calculate the Virtual Size of the new object
  357.     MOV     ECX,DWord ptr [ObjAlign]
  358.     MOV     EAX,AppendEnd - AppendStart    ; Size of Appendix
  359.     XOR     EDX,EDX
  360.     DIV     ECX
  361.     INC     EAX
  362.     MUL     ECX
  363.     MOV     DWord ptr [VirtualSize],EAX
  364.  
  365. ;------ calculate the physical offset of the new object
  366.     MOV     EAX, [EDI-40+20]        ; Previous Phys size
  367.     ADD     EAX,[EDI-40+16]            ; Previous phys offset
  368.     MOV     ECX,DWord ptr [FileAlign]    ; File "pages"
  369.     XOR    EDX,EDX
  370.     DIV    ECX
  371.     INC    EAX                ; No need for test edx,edx
  372.     MUL     ECX                ; since prev. obj must be aligned!
  373.     MOV     DWord ptr [PhysicalOffset],EAX
  374.  
  375. ;------ update the image size (the size in memory) of the file
  376.     mov    eax, [VirtualSize]
  377.     add    [ImageSize],eax
  378.  
  379. ;------ Copy it the new object!!!!
  380.     CLD                    ; Forward copy
  381.     MOV     ECX, 40                ; 40 bytes = size of objects
  382.     REP     MOVSB                ; copy
  383.  
  384.  
  385. ;------ Calculate the new entrypoint RVA
  386.     MOV     EAX,DWord ptr [RVA]        ; RVA of new Object (our code)
  387.  
  388.     MOV     EBX,DWord ptr [entrypointRVA]    ; Save old RVA entry
  389.     MOV     DWord ptr [entrypointRVA],EAX    ; Put in new one in PE header!
  390.  
  391. ;----- Set the value needed to return to the host
  392.     MOV    [AppEntryRVA], EBX        ; store EntryRVA in Appendix
  393.  
  394.  
  395.     MOV     BytesRead, 0
  396. ;----- Set FilePoint to PEheader!
  397.     PUSH     0
  398.     PUSH     0
  399.     PUSH     DWord ptr [PeHeaderOff]
  400.     PUSH     DWord ptr [FileHandle]
  401.     CALL    SetFilePointer
  402.  
  403. ;----- Write the pe header and object table to the file
  404.     PUSH     0
  405.     PUSH     offset BytesRead
  406.     MOV     EAX, [HeaderSize]
  407.     SUB     EAX, [PeHeaderOff]
  408.     PUSH     EAX
  409.     PUSH     offset PEHeader
  410.     PUSH     DWord ptr [FileHandle]
  411.     CALL     WriteFile
  412.  
  413. ;----- MOVe FilePointer to the physical offset of the new object
  414. ;----- I'm not too happy about this moving the file pointer beyound the
  415. ;    EOF instead of actually zeropadding the last object before adding our
  416. ;    own... theoretically you should zero pad!!!!
  417.     PUSH     0
  418.     PUSH     0
  419.     PUSH     DWord ptr [PhysicalOffset]
  420.     PUSH     DWord ptr [FileHandle]
  421.     CALL     SetFilePointer
  422.  
  423. ;----- append decryption code
  424.     MOV     BytesRead,0
  425.     PUSH     0
  426.     PUSH     offset BytesRead        ; This many bytes appended 
  427.     MOV     EAX, AppendEnd-AppendStart    ; This many bytes dESIred to be appended
  428.     PUSH     EAX
  429.     PUSH     offset AppendStart
  430.     PUSH     DWord ptr [FileHandle]
  431.     CALL     WriteFile
  432.  
  433. terminate:
  434.     CALL     CloseFile            ; Close the damn file
  435. Exit_Proc:                    ; Exit Process
  436.     PUSH     LARGE-1
  437.     CALL     ExitProcess
  438.  
  439. Not_PE:
  440.     Write_Console <Offset MsgNotPE> <LenNotPE>
  441.     JMP     terminate
  442.     
  443. Error_Read: 
  444.     Call GetLastError
  445.     Write_Console <Offset MsgErrRead> <LenErrRead>
  446.     Call Hex2Dec
  447.     Write_Console <Offset MsgH> <LenH>
  448.     JMP terminate
  449.  
  450. NoObjSpace:
  451.     Write_Console <offset MsgNoObjSpace> <LenNoObjSpace>
  452.     jmp terminate
  453.  
  454. NoCommandLine:
  455.     Write_Console <offset MsgNoCmdLine> <LenNoCmdLine>
  456.     JMP terminate
  457.  
  458.  
  459. Encrypt PROC
  460.     PUSHAD
  461.     movzx     ECX, [NumObj]            ; ECX = Number of objects!
  462.  
  463.                         ; For when we encrypt all objects
  464.     Write_Console <offset MsgObjLine> <LenObjLine>
  465.  
  466.     MOV     ESI, offset PEHeader
  467.  
  468. ;----- Preserve Import INFO stored out of .idata section
  469.     MOV    EAX, dword ptr [ESI+128]    ;  RVA
  470.     MOV    [ITRVA], EAX
  471.     MOV    [ITENDRVA], EAX            ; IT RVA (import table)
  472.     MOV    EAX, [ESI+132]
  473.     ADD    [ITENDRVA], EAX            ; Size of IT! (import table)
  474.  
  475.     MOV    EAX, dword ptr [esi+0d8h]    ; RVA of IAT
  476.     MOV    [AppIAT], EAX
  477.     add    eax, dword ptr [esi+0d8h+4]    ; Size of IAT
  478.     mov    [AppEndIAT],EAX
  479.  
  480.     mov    dword ptr [esi+0d0h],0        ; gestapo method :)
  481.     mov    dword ptr [esi+0d4h],0        ; eradicate information on which DLL's TO load
  482.                         ; NT will instead use the IT
  483. ;----- go on with encryption
  484.     MOV     EAX, [ObjTblOff]
  485.     ADD     ESI,EAX                ; ESI= First object item table
  486.     XOR     EDI, EDI
  487. nextobject:
  488.     call Enc_Object
  489.     add esi, 40                ; => next object in objtable
  490.     loopd nextobject
  491.  
  492.  
  493.     POPAD
  494.     RET
  495.  
  496. Encrypt ENDP
  497.  
  498. Enc_Object PROC                    ; Encrypts an object
  499.                         ; ESI should => Objtable
  500.                         ; of object
  501.     pushad
  502.  
  503.     Write_Console <offset MsgLfCf> <LenLfCf>
  504.  
  505.     call ObjectInfo                ; Lame screen output
  506.  
  507.     mov    [ObjectDone],0
  508.  
  509.     cmp dword ptr [ESI], 'adr.'        ; stay out of .rdata
  510.     jz noencrypt                ; .rdata contains all kinda
  511.                         ; fucked up debug info'n'stuff
  512.  
  513.     cmp dWord ptr [ESI], 'ler.'        ; Stay out of relocations
  514.     jz noencrypt
  515. ;******* Unencrypted relocations is not a problem given:
  516. ; add [byte in image] + encryption [byte in image] 
  517. ; = encryption [byte in image] + add [byte in image] 
  518. ; and that encryption [byte in image] | encryption [byte-1 in image]
  519. ; = encryption [byte in image] - that encryption is independant of previous 
  520. ; bytes.. this basically means you can't use ROL/ROR or XOR in your encryption
  521. ; scheme unless you specifically deal with relocation or sacrifice general
  522. ; compatability (while this at first glance seems to be a major problem
  523. ; in regards to getting a strong polymorphic encryption that is not the case!!!)
  524. ; Also relocations is only used *IF* win95 cannot map the same virtual address
  525. ; as the PE file was linked under. (almost never) Encrypting Relocations can be handled
  526. ; and will be shit annoying to unpack!
  527.  
  528.  
  529.     cmp DWord ptr [ESI], 'adi.'        ; Stay out of Import data
  530.     jz noencrypt
  531. ; .idata *usually* :( contains the IAT which means trouble if we encrypt it.
  532. ; lemme think this "issue" over a couple of days!
  533.  
  534.     cmp dword ptr [ESI], 'ade.'        ; Stay out of export data
  535.     jz noencrypt                ; can I safely encrypt it?
  536.  
  537.     cmp dword ptr [ESI], 'rsr.'        ; Stay out of resource code
  538.     jz noencrypt                ; 
  539.  
  540.     cmp dword ptr [ESI], 'oci.'        ; Don't wanna encrypt the icon ;)
  541.     jz noencrypt
  542.  
  543.     cmp dword ptr [esi+16], 0        ; If size of object=0 then 
  544.     jz noencrypt                ; do not encrypt (e.g. .bss)
  545.  
  546.     Write_Console <offset MsgYup> <LenYup>
  547.     
  548.     MOV    EDI, dword ptr [ESI+12]
  549.     MOV    [CurrentRVA], EDI
  550.         
  551.         MOV     bl, byte ptr [ESI+39]        ; Force Write Axx. to obj.
  552.     OR     bl, 80h                ; if we don't we get a page
  553.     MOV     byte ptr [ESI+39],bl        ; fault OR a protection fault!
  554.  
  555.     MOV    EDI, [CurrDisp]
  556.     MOV     EAX, [ESI+12]
  557.     MOV     [AppObjectRVA+EDI], EAX
  558.  
  559.     MOV     EAX, [ESI+16]              ; EAX= phys size of Obj!
  560.     MOV     [AppObjectSize+EDI], EAX    
  561.     Add     [CurrDisp], 8            ; Update Current Displacement of RVA
  562.     INC    Byte ptr [AppNumObj]        ; Update
  563.  
  564.  
  565.     XOR     EDX,EDX
  566.     MOV     EDI,16                ; 10h
  567.     DIV     EDI
  568.     MOV     EDI,EAX                ; EDI = number of 10h byte blocks
  569.  
  570.  
  571.     MOV     EAX, [ESI+20]            ; Current Position to EAX
  572.     MOV     [CurrentPos], EAX
  573.  
  574. ;----- MOVe FilePointer to the physical offset of the new object
  575.     PUSH     0                ; Method 0 - from start of file
  576.     PUSH    0
  577.     PUSH     DWORd ptr [ESI+20]
  578.     PUSH    DWORd ptr [FileHandle]
  579.     CALL     SetFilePointer
  580.  
  581. NextBlock:
  582.     PUSH     0                ; 0
  583.     PUSH     offset BytesRead        ; bytes read
  584.     MOV     EAX, 16
  585.     PUSH     EAX
  586.     PUSH     offset EncryptBuffer        ; To EncryptBuffer
  587.     PUSH     DWORd ptr [FileHandle]        ; From our file
  588.     CALL     ReadFile            ; Read IT! :)
  589.     
  590.     MOV     ECX,16
  591.     MOV     EBX, offset EncryptBuffer
  592. encryptloop:
  593.     mov    dl, [ObjectDone]        ; Have we encountered the IT
  594.     cmp    dl, 1                ; in this object?
  595.     jz    IT                ; if so - don't encrypt any more
  596.  
  597.     mov    EDX, [CurrentRVA]        ; are we in the IT?
  598.     CMP    edx, [ITENDRVA]
  599.     jg    NotIT
  600.     cmp    EDX, [ITRVA]
  601.     jl    NotIT
  602.  
  603.     MOV    EDX, [CurrDisp]            ; Yes - this is the IT
  604.     sub     edx, 8
  605.     push    eax
  606.     MOV     eax, [CurrentRVA]
  607.     sub     eax, [ESI+12]
  608.     dec    eax
  609.     MOV    [AppObjectSize+EDX], EAX    ; Decrypt this many bytes instead
  610.     POP     EAX
  611.  
  612.     mov    [ObjectDone],1
  613.  
  614.     jmp    IT
  615. NotIT:
  616.     ADD     byte ptr [EBX],subvalue        ; simple encryption scheme
  617. IT:
  618.     INC     EBX
  619.     INC    dword ptr [CurrentRVA]
  620.     LOOPD     encryptloop
  621.         
  622.     PUSH     0h                    
  623.     PUSH    0h
  624.     PUSH     DWORd ptr [CurrentPos]
  625.     PUSH     DWORd ptr [FileHandle]
  626.     CALL     SetFilePointer
  627.  
  628.     MOV     [BytesRead],0
  629.     PUSH     0
  630.     PUSH     offset BytesRead        ; Bytes Read
  631.     MOV     EAX, 16
  632.     PUSH     EAX                ; number of bytes
  633.     PUSH     offset EncryptBuffer        ; EncryptBuffer
  634.     PUSH     DWORd ptr [FileHandle]        ; to filehandle
  635.     CALL     WriteFile
  636.  
  637.     ADD     [CurrentPos],16            
  638.  
  639.  
  640.     DEC     EDI
  641.     JNZ     NextBlock
  642. noencrypt:
  643.  
  644.     popad
  645.     ret
  646. Enc_Object ENDP
  647.  
  648.  
  649. OpenRW     PROC
  650.     PUSH     0
  651.     PUSH     offset FileAttr          ; FILE_ATTRIBUTE_NORMAL
  652.     PUSH     3                 ; OPEN_EXISTING
  653.     PUSH     0
  654.     PUSH     0
  655.     PUSH     80000000h + 40000000h         ; GENERIC_READ + GENERIC_WRITE
  656.     PUSH    edi ;offset FileName
  657.     CALL     CreateFileA                 ; open file in read/write mode
  658.     RET
  659. OpenRW ENDP
  660.  
  661. CloseFile PROC
  662.     PUSH     DWord Ptr [FileHandle]
  663.     CALL     CloseHandle
  664.      RET
  665. CloseFile EndP
  666.  
  667. WriteInfo PROC                    ; Just lame screen output
  668.     PUSHAD
  669.     
  670.     Write_Console <offset MsgPeHOff> <LenPeHOff>
  671.     MOV     EAX, [PeHeaderOff]
  672.     CALL     Hex2Dec
  673.     Write_Console <offset MsgH> <LenH>
  674.  
  675.  
  676.     Write_Console <Offset MsgHSize> <LenHSize>
  677.     MOV    EAX, [HeaderSize]
  678.     Call     Hex2Dec
  679.     Write_Console <offset MsgH> <LenH>
  680.  
  681.     Write_Console <Offset MsgImageSize> <LenImageSize>
  682.     MOV    EAX, [ImageSize]
  683.     Call    Hex2Dec
  684.     Write_Console <offset MsgH> <LenH>
  685.  
  686.     Write_Console <offset MsgNumObj> <LenNumObj>
  687.     MOVZX     EAX, [NumObj]
  688.     CALL     Hex2Dec
  689.     Write_Console <offset MsgH> <LenH>
  690.  
  691.     Write_Console <offset MsgObjAlg> <LenObjAlg>
  692.     MOV    EAX, [ObjAlign]
  693.     call    Hex2Dec
  694.     Write_Console <offset MsgH> <LenH>
  695.  
  696.     Write_Console <offset MsgIB> <LenIB>
  697.     MOV     EAX, [ImageBase]
  698.     CALL    Hex2Dec
  699.     Write_Console <offset MsgH> <LenH>
  700.  
  701.  
  702.     Write_Console <offset MsgEPRVA> <LenEPRVA>
  703.     MOV EAX, [entrypointRVA]
  704.     CALL Hex2Dec
  705.     Write_Console <offset MsgH> <LenH>
  706.  
  707.     Write_Console <offset MsgAppendSize> <LenAppendSize>
  708.     MOV EAX, AppendEnd-AppendStart
  709.     call Hex2Dec
  710.     Write_Console <offset MsgH> <LenH>
  711.  
  712.     POPAD
  713.     RET
  714. WriteInfo ENDP
  715.  
  716. ObjectInfo PROC                    ; Write Info on objecttable at
  717.     PUSHAD                    ; ESI
  718.     
  719.     mov ecx, 8                ; Object Name
  720.     call Dump2Screen
  721.  
  722.     Write_Console <offset MsgSpace> <LenSpace> ; "      "
  723.     
  724.     Add esi, 8                ; VirSize
  725.     mov eax, [ESI]
  726.     call Hex2Dec
  727.     Write_Console <offset MsgH2> <LenH2>
  728.  
  729.  
  730.     add esi, 4                ; Object RVA
  731.     mov EAX, [ESI]
  732.     call Hex2Dec
  733.  
  734.     Write_Console <offset MsgH2> <LenH2>    ; "h    "
  735.  
  736.     add esi, 4                ; Physical Size
  737.     mov eax, [ESI]
  738.     call Hex2Dec
  739.  
  740.     Write_Console <offset MsgH2> <LenH2>    ; "h     "    
  741.  
  742.     add esi, 4                ; PhysPos
  743.     mov eax, [esi]
  744.     call Hex2Dec                
  745.  
  746.     Write_Console <offset MsgH2> <LenH2>    ; "h     "
  747.     
  748.     add esi, 16                ; Flags
  749.     mov eax, [esi]
  750.     call Hex2Dec
  751.     
  752.     Write_Console <offset MsgH2> <LenH2>    ; "h",10,13
  753.     
  754.     POPAD
  755.     RET
  756. ObjectInfo ENDP
  757.  
  758.  
  759. Dump2Screen PROC            ; Writes ECX bytes from ESI on console
  760.     PushA
  761.     PushF 
  762.     Push 0                       ; lpvReserved
  763.     Push offset cchWritten       ; lpcchWritten
  764.     Push ECX              ; cchToWrite
  765.     Push ESI                      ; lpvBuffer
  766.     Push [ConHandle]             ; hConsoleOutput
  767.     Call WriteConsoleA           ; i will let u think about it :)
  768.     PopF
  769.     PopA
  770.     RET
  771. Dump2Screen ENDP
  772.  
  773. ;------ Everything below here is append to the PE file :)
  774.  
  775. AppendStart:
  776.  
  777.     PUSH    EBP EDI ESI EDX ECX EBX ; No point in saving EAX / EAX =
  778.                     ; ENTRYPOINT ADDRESS - So we make
  779.                     ; sure it still is so!
  780. ;    int 3h                ; <<<<<<<<< Breakpoint for the almighty
  781.  
  782.     CALL     next            ; calculate delta offset!
  783. next:
  784.     POP     EBP
  785.     MOV     EDX, EBP        ; Save EIP
  786.  
  787.     SUB     EBP, offset next    ; Calculate delta offset
  788.  
  789.     SUB    EDX, [AppVirOff+EBP]    ; Subtract the virtual offset
  790.     SUB    EDX, offset next- offset AppendStart ; Subtract the push in the start
  791.                     ; => EDX= reallocated IB
  792.                     ; Sometimes you're pretty clever, Stone :)
  793.  
  794.     MOV    [AppImageBase+EBP],EDX
  795.  
  796.     ADD    [AppIAT+ebp], EDX    ; from RVA => VA
  797.     ADD    [AppEndIAT+ebp],EDX
  798.  
  799.     
  800.     cmp     [AppStatus+ebp],0    ; envoke decryption only if first time
  801.     jnz    ReturnToHost        ; dll will envoke code on entrypoint
  802.     mov    [AppStatus+ebp],1    ; more than once - but always once
  803.                     ; when loaded!
  804.  
  805.     Lea    esi, [AppNumObj+ebp]
  806.     movzx    esi, Byte ptr [esi]
  807.     MOV     EDI, EBP
  808.  
  809. Nextobj:
  810.     LEA     EBX, [AppImageBase+EBP]        ; Fetch Imagebase
  811.     MOV     EBX, [EBX]
  812.     LEA     EAX, [AppObjectRVA+EDI]        ; fetch the RVA of the object
  813.     MOV     EAX, [EAX]
  814.     ADD     EBX, EAX            ; Imagebase+RVA
  815.     LEA     ECX, [AppObjectSize+EDI]    ; Fetch number of bytes to DECrypt
  816.     MOV     ECX, [ECX]
  817.  
  818. DeCryptloop:                    ; DECrypt it
  819.     cmp    ebx, dword ptr [AppEndIAT+ebp]
  820.     jg    NotIAT
  821.     cmp    ebx, dword ptr [AppIAT+EBP]
  822.     jl    NotIAT
  823.     jmp     IAT
  824. NotIAT:
  825.     SUB     byte ptr [EBX],subvalue        ; Wooohoo.. powerful encryption
  826. IAT:
  827.     INC     EBX
  828.     LOOPD     DeCryptloop
  829.  
  830.     ADD    EDI, 8                ; Next Object! (8=1dword of RVA, 1dword of size)
  831.     dec    esi
  832.     jnz     Nextobj
  833.  
  834.  
  835. ReturnToHost:
  836. ;------- Return to Host            
  837.     MOV     EAX, [AppEntryRVA+EBP]        ; EAX = new entrypoint RVA
  838.     MOV     EBX, [AppImageBase+EBP]        ; EBX = ImageBase
  839.     ADD    EAX, EBX            ; EAX = New Entrypoint
  840.  
  841.     POP      EBX ECX EDX ESI EDI EBP        ; restore registers
  842.     JMP     EAX
  843. breakpoint:
  844.  
  845. ;------- Appended data
  846. AppIAT        dd 0            ; IAT starts here 
  847. AppEndIAT    dd 0            ; IAT ends here
  848. AppStatus    db 0            ; decrypted already marker
  849. AppVirOff    dd 0            ; Virtual offset (RVA) of Appended code
  850. AppEntryRVA    dd 0        
  851. AppNumObj       db 0             ; Number of objects to decrypt
  852. AppImageBase     dd 0            ; ImageBase in appeded Code!
  853. AppObjectRVA    dd 0            ; Encrypted Objects RVA
  854. AppObjectSize   dd 0             ; Size of Object
  855. AppMoreObjects  db 40 dup (0)        ; 5 more encrypted objects
  856.                     ; should I add more space?
  857.                     ; or just let it use the next string?
  858.  
  859. db     "Encrypted by Stone/UCF - PowerLame PE-ExeEnCrypter! :) "
  860. db      "2nd&mi" ; Personal tag - not used!
  861. AppendEnd:
  862. ;──────────────────────────────────────────────────────────────────────────────
  863. End Main
  864.  
  865.