home *** CD-ROM | disk | FTP | other *** search
/ Hacker 2 / HACKER2.mdf / virus / 40hex_9.004 < prev    next >
Text File  |  1995-01-03  |  15KB  |  335 lines

  1. 40Hex Number 9 Volume 2 Issue 5                                       File 004
  2.  
  3. I picked this up in a collection of clips from the Fidonet 80xxx echo,
  4. figured it might interest someone.
  5.                                         --Hawkmoon
  6.  
  7. ===============================================================================
  8.  
  9.                             Anti Debugging Tricks
  10.                                      By:
  11.                                   Inbar Raz
  12.  
  13.                                Release number 2
  14.  
  15.   Today's anti debugging tricks devide into two categories:
  16.  
  17.   1. Preventive actions;
  18.   2. Self-modifying code.
  19.  
  20.   Most debugging tricks, as for today, are used within viruses, in order to
  21. avoid dis-assembly of the virus, as it will be exampled later in this file.
  22. Another big part of anti debugging tricks is found with software protection
  23. programs, what use them in order to make the cracking of the protection
  24. harder.
  25.  
  26. 1. Preventive actions:
  27. ----------------------
  28.  
  29.   Preventive actions are, basically, actions that the program takes in order
  30. to make the user unable to dis-assemble the code or trace it while running.
  31.  
  32. 1.1. Interrupt disable:
  33.  
  34.        Interrupt disable is probably the most common form of anti-debugging
  35.      trick. It can be done in several ways:
  36.  
  37.    1.1.1. Hardware masking of interrupt:
  38.  
  39.             In order to avoid tracing of a code, one usually disables the
  40.           interrupt via the 8259 Interrupt Controller, addressed by read/write
  41.           actions to port 21h. The 8259 Interrupt Controller controls the IRQ
  42.           lines. This means that any IRQ between 0 and 7 may be disabled by
  43.           this action. Bit 0 is IRQ0, bit 1 is IRQ1 etc. Since IRQ1 is the
  44.           keyboard interrupt, you may disable the keyboard without the
  45.           debugger being able to bypass it.
  46.  
  47.           Example:
  48.  
  49.           CS:0100 E421           IN     AL,21
  50.           CS:0102 0C02           OR     AL,02
  51.           CS:0104 E621           OUT    21,AL
  52.  
  53.             Just as a side notice, the keyboard may be also disabled by
  54.           commanding the Programmable Perepheral Interface (PPI), port 61h.
  55.  
  56.           Example:
  57.  
  58.           CS:0100 E461           IN     AL,61
  59.           CS:0102 0C80           OR     AL,80
  60.           CS:0104 E661           OUT    61,AL
  61.  
  62.    1.1.2. Software masking of interrupt:
  63.  
  64.             This is quite an easy form of anti-debugging trick. All you have
  65.           to do is simply replace the vectors of interrupts debuggers use/any
  66.           other interrupt you will not be using or expecting to happen. Do not
  67.           forget to restore the original vectors when you are finished.
  68.           It is adviseable to use manual change of vector, as shown below,
  69.           rather than to change it using interrupt 21h service 25h, because
  70.           any debugger that has gained control of interrupt 21h may replace
  71.           your vector with the debugger's. The example shows an interception
  72.           of interrupt 03h - the breakpoint interrupt.
  73.  
  74.           Example:
  75.  
  76.           CS:0100 EB04           JMP    0106
  77.           CS:0102 0000           ADD    [BX+SI],AL
  78.           CS:0104 0000           ADD    [BX+SI],AL
  79.           CS:0106 31C0           XOR    AX,AX
  80.           CS:0108 8EC0           MOV    ES,AX
  81.           CS:010A 268B1E0C00     MOV    BX,ES:[000C]
  82.           CS:010F 891E0201       MOV    [0102],BX
  83.           CS:0113 268B1E0E00     MOV    BX,ES:[000E]
  84.           CS:0118 891E0401       MOV    [0104],BX
  85.           CS:011C 26C7064C000000 MOV    Word Ptr ES:[000C],0000
  86.           CS:0123 26C7064E000000 MOV    Word Ptr ES:[000E],0000
  87.  
  88.    1.1.3. Vector manipulation
  89.  
  90.             This method involves manipulations of the interrupt vectors,
  91.           mainly for proper activation of the algorithm. Such action, as
  92.           exampled, may be used to decrypt a code (see also 2.1), using data
  93.           stored ON the vectors. Ofcourse, during normal operation of the
  94.           program, vectors 01h and 03h are not used, so unless you are trying
  95.           to debug such a program, it works fine.
  96.  
  97.           Example:
  98.  
  99.           CS:0100 31C0           XOR    AX,AX
  100.           CS:0102 8ED0           MOV    SS,AX
  101.           CS:0104 BC0600         MOV    SP,0006
  102.           CS:0107 8B0E0211       MOV    CX,[1102]
  103.           CS:010B 50             PUSH   AX
  104.           CS:010C 21C8           AND    AX,CX
  105.           CS:010E 01C5           ADD    BP,AX
  106.           CS:0110 58             POP    AX
  107.           CS:0111 E2F8           LOOP   010B
  108.  
  109.    1.1.4. Interrupt replacement
  110.  
  111.             This is a really nasty trick, and it should be used ONLY if you
  112.           are ABSOLUTELY sure that your programs needs no more debugging. What
  113.           it does is simply copy the vectors of some interrupts you will be
  114.           using, say 16h and 21h, onto the vectors of interrupt 01h and 03h,
  115.           that do not occure during normal operation of the program. If the
  116.           user wants to debug the program, he would have to search for every
  117.           occurance of INT 01, and replace it with the appropriate INT
  118.           instruction.
  119.  
  120.           Example:
  121.  
  122.           CS:0100 FA             CLI
  123.           CS:0101 31C0           XOR    AX,AX
  124.           CS:0103 8EC0           MOV    ES,AX
  125.           CS:0105 26A18400       MOV    AX,ES:[0084]
  126.           CS:0109 26A30400       MOV    ES:[0004],AX
  127.           CS:010D 26A18600       MOV    AX,ES:[0086]
  128.           CS:0111 26A30600       MOV    ES:[0006],AX
  129.           CS:0115 B44C           MOV    AH,4C
  130.           CS:0117 CD01           INT    01
  131.  
  132. 1.2. Time watch:
  133.  
  134.        This may be a less common method, but it is usefull against debuggers
  135.      that disable all interrupts except for the time that the program is
  136.      executed, such as Borland's Turbo Debugger. This method simply retains
  137.      the value of the clock counter, updated by interrupt 08h, and waits in an
  138.      infinite loop until the value changes. Another example is when you mask
  139.      the timer interrupt by ORing the value INed from port 21h with 01h and
  140.      then OUTing it back, thus disabling the IRQ0 - Timer interrupt. Note that
  141.      this method is usefull only against RUN actions, not TRACE/PROCEED ones.
  142.  
  143.      Example:
  144.  
  145.      CS:0100 2BC0           SUB    AX,AX
  146.      CS:0102 FB             STI
  147.      CS:0103 8ED8           MOV    DS,AX
  148.      CS:0105 8A266C04       MOV    AH,[046C]
  149.      CS:0109 A06C04         MOV    AL,[046C]
  150.      CS:010C 3AC4           CMP    AL,AH
  151.      CS:010E 74F9           JZ     0109
  152.  
  153. 1.3. Fool the debugger:
  154.  
  155.        This is a very nice technique, that works especially and only on those
  156.      who use Turbo Debugger or its kind. What you do is init a jump to a
  157.      middle of an instruction, whereas the real address actually contains
  158.      another opcode. If you work with a normal step debugger such as Debug or
  159.      SymDeb, it won't work since the debugger jumps to the exact address of
  160.      the jump, and not to the beginning of an instruction at the closest
  161.      address, like Turbo Debugger.
  162.  
  163.      Example:
  164.  
  165.      CS:0100 E421           IN     AL,21
  166.      CS:0102 B0FF           MOV    AL,FF
  167.      CS:0104 EB02           JMP    0108
  168.      CS:0106 C606E62100     MOV    Byte Ptr [21E6],00
  169.      CS:010B CD20           INT    20
  170.  
  171.      Watch this:
  172.  
  173.      CS:0108 E621           OUT    21,AL
  174.  
  175. 1.4. Cause debugger to stop execution:
  176.  
  177.        This is a technique that causes a debugger to stop the execution of a
  178.      certain program. What you need to do is to put some INT 3 instructions
  179.      over the code, at random places, and any debugger trying to run will stop
  180.      there. Since this techniqu causes the CPU to stop executing the program,
  181.      and therefore clear the Prefetch Instruction Queue, it is adviseable to
  182.      use this techinque in conjunction with the PIQ trick, 2.2.2. Note that
  183.      the example shows how to use these two tricks together.
  184.  
  185.      Example:
  186.  
  187.      CS:0100 B97502         MOV    CX,0275
  188.      CS:0103 BE9001         MOV    SI,0190
  189.      CS:0106 89F7           MOV    DI,SI
  190.      CS:0108 AC             LODSB
  191.      CS:0109 C70610013473   MOV    Word Ptr [0110],7334
  192.      CS:010F CC             INT    3
  193.      CS:0110 2406           AND    AL,06
  194.      CS:0112 AA             STOSB
  195.      CS:0113 C70610012406   MOV    Word Ptr [0110],0624
  196.      CS:0119 E2ED           LOOP   0108
  197.  
  198. 1.5. Halt TD386 V8086 mode:
  199.  
  200.        This is a nice way to fool Turbo Debugger's V8086 module (TD386). It is
  201.      baed on the fact that TD386 does not use INT 00h to detect division by
  202.      zero (or register overrun after division, which is treated by the
  203.      processor in the same way as in case of division by zero). When TD386
  204.      detects a division fault it aborts, reporting about the faulty
  205.      division. In real mode (even under a regular debugger), a faulty DIV
  206.      instruction will cause INT 00h to be called. Therefore, pointing INT 00h
  207.      to the next instruction, will recover from the faulty DIV.
  208.  
  209.      Note: It is very important to restore INT 00h's vector. Otherwise, the
  210.      next call to INT 00h will cause the machine to hang.
  211.  
  212.      Example:
  213.  
  214.      CS:0100 31C0          XOR     AX,AX
  215.      CS:0102 8ED8          MOV     DS,AX
  216.      CS:0104 C70600001201  MOV     WORD PTR [0000],0112
  217.      CS:010A 8C0E0200      MOV     [0002],CS
  218.      CS:010E B400          MOV     AH,00
  219.      CS:0110 F6F4          DIV     AH
  220.      CS:0112 B8004C        MOV     AX,4C00
  221.      CS:0115 CD21          INT     21
  222.  
  223. 1.6. Halt any V8086 process:
  224.  
  225.        Another way of messing TD386 is fooling it into an exception.
  226.      Unfortunately, this exception will also be generated under any other
  227.      program, running at V8086 mode. The exception is exception #13, and its
  228.      issued interrupt is INT 0Dh - 13d. The idea is very similar to the
  229.      divide by zero trick: Causing an exception, when the exception interrupt
  230.      points to somewhere in the program's code. It will always work when the
  231.      machine is running in real mode, but never under the V8086 mode.
  232.  
  233.      Note: It is very important to restore the original interrupt vectors.
  234.      Otherwise, the next exception will hang the machine.
  235.  
  236.      Example:
  237.  
  238.      CS:0100 31C0          XOR     AX,AX
  239.      CS:0102 8ED8          MOV     DS,AX
  240.      CS:0104 C70634001301  MOV     WORD PTR [0034],0113
  241.      CS:010A 8C0E3600      MOV     [0036],CS
  242.      CS:010E 833EFFFF00    CMP     WORD PTR [FFFF],+00
  243.      CS:0113 B8004C        MOV     AX,4C00
  244.      CS:0116 CD21          INT     21
  245.  
  246. 2. Self-modifying code:
  247. -----------------------
  248.  
  249. 2.1. Encryptive/decryptive algorithm:
  250.  
  251.        The first category is simply a code, that has been encrypted, and has
  252.      been added with a decryption routine. The trick here is that when a
  253.      debugger sets up a breakpoint, it simply places the opcode CCh (INT 03h)
  254.      in the desired address, and once that interrupt is executed, the debugger
  255.      regains control of things. If you try to set a breakpoint AFTER the
  256.      decryption algorithm, what is usually needed, you will end up putting an
  257.      opcode CCh in a place where decryption action is taken, therefore losing
  258.      your original CCh in favour of whatever the decryption algorithm makes.
  259.      The following example was extracted from the Haifa virus. If you try to
  260.      set a breakpoint at address CS:0110, you will never reach that address,
  261.      since there is no way to know what will result from the change. Note that
  262.      if you want to make the tracing even harder, you should start the
  263.      decryption of the code from its END, so it takes the whole operation
  264.      until the opcode following the decryption routine is decrypted.
  265.  
  266.      Example:
  267.  
  268.      CS:0100 BB7109         MOV    BX,0971
  269.      CS:0103 BE1001         MOV    DI,0110
  270.      CS:0106 91             XCHG   AX,CX
  271.      CS:0107 91             XCHG   AX,CX
  272.      CS:0108 2E803597       XOR    Byte Ptr CS:[DI],97
  273.      CS:010C 47             INC    DI
  274.      CS:010D 4B             DEC    BX
  275.      CS:010E 75F6           JNZ    0106
  276.      CS:0110 07             POP    ES
  277.      CS:0111 07             POP    ES
  278.  
  279. 2.2. Self-modifying code:
  280.  
  281.    2.2.1. Simple self-modification:
  282.  
  283.             This method implements the same principle as the encryption
  284.           method: Change the opcode before using it. In the following example,
  285.           we change the insruction following the call, and therefore, if you
  286.           try to trace the entire call ('P'/Debug or F8/Turbo Debugger), you
  287.           will not succeed, since the debugger will put its CCh on offset 104h,
  288.           but when the routine runs, it overwrites location 104h.
  289.  
  290.           Example:
  291.  
  292.           CS:0100 E80400         CALL   0107
  293.           CS:0103 CD20           INT    20
  294.           CS:0105 CD21           INT    21
  295.           CS:0107 C7060301B44C   MOV    Word Ptr [0103],4CB4
  296.           CS:010D C3             RET
  297.  
  298.           Watch this:
  299.  
  300.           CS:0103 B44C           MOV    AH,4C
  301.  
  302.    2.2.2. Prefetch Instruction Queue (PIQ) manipulation:
  303.  
  304.             This method is a bit similar to (1.3), but it fools ANY debugger,
  305.           or any other process that executes one operation at a time. The PIQ
  306.           is an area within the CPU, that pre-fethces, ie. takes in advance,
  307.           instructions from memory, so when they need to be executed, it
  308.           would take less time to get them, since they are already in the CPU.
  309.           The PIQ length ranges from 6 or 4 in old computers, up to as high as
  310.           25 in new ones. What the trick does is change the FOLLOWING opcode
  311.           to something meaningless. If you are debugging, then the change will
  312.           take place BEFORE the instructions is executed or fetched. If you
  313.           run the program NORMALLY, by the time you change the opcode, it will
  314.           have already been fetched.
  315.  
  316.           Example:
  317.  
  318.           CS:0100 B97502         MOV    CX,0275
  319.           CS:0103 BE9001         MOV    SI,0190
  320.           CS:0106 89F7           MOV    DI,SI
  321.           CS:0108 AC             LODSB
  322.           CS:0109 C7060F012406   MOV    Word Ptr [010F],0624
  323.           CS:010F 3473           XOR    AL,73
  324.           CS:0111 AA             STOSB
  325.           CS:0112 C7060F012406   MOV    Word Ptr [010F],0624
  326.           CS:0118 E2EE           LOOP   0108
  327.  
  328.           Watch this:
  329.  
  330.           CS:010F 2406           AND    AL,06
  331.  
  332. ===============================================================================
  333.  
  334. Downloaded From P-80 International Information Systems 304-744-2253
  335.