Wednesday, April 23, 2014

EBFE (Debugging a remote thread)

Creating a remote thread is a common routine for malwares. A remote thread is like a normal thread, but it is running in another process' context. It is usually done by writing the thread routine to the target running process.

This makes debugging the remote thread a little bit tricky because you need to attach a debugger to the target process (this is the process that is being injected).

However, there are some instances where you cannot attach Ollydbg to the target process because it is suspended.
















To go around this, we can improvise a technique where we modify few bytes of the entry point's thread so that it will not execute. That is, we modify the thread so that it will be in an infinite loop before executing the thread.

We modify the first 2 bytes of the threads entry point with the bytes "EB FE". EB FE are the hex bytes equivalent to JMP TO SELF. In effect, when you resumed the thread, it will be in an infinite loop, an "improvised breakpoint". With this, once the thread is resumed, we can now attach Ollydbg to the target process without executing the whole thread. Before you modify, take note of the 2 bytes that you replaced because you will replace it back later. Refer to the diagram below.













Now, let us start debugging a real malware applying this concept.

We will be using the following sample:

MD5: 49e6ad4afa492b1a7e34b46a98298d5b


Let us skip to the part where it creates a suspended process and go right where it tries to write into it.
Take note of the code that it is trying to inject on the suspended process. Notice that it tries to write an executable image. With this, we can assume that the entry point of the thread will be the entry point of the executable image.



So, what we can do is to find the entry point of that executable image and replace the first 2 bytes with EBFE. To find the entry point,  we can dump the memory to file and find the offset of the entry point. In this sample, the entry point is at offset 0x168B.

We go to that offset and change the first 2 bytes. To do this:
Highlight the first 2 bytes to modify and Press Ctrl+E to edit.
 


Go ahead and change it to EB FE. Take note of the 2 bytes "55 8B" because you will restore it later. In most cases, this is 55 8B because it is usually the initial bytes of a function. The bytes will turn red indicating it is changed.




You can now execute until it resume the thread "kernell32.ResumeThread" or run it (F9). You will notice that the injected process will have almost 100% memory utilization because it is in an infinite loop. This also means that our "improvised breakpoint" is working. However, the good news is, since the process is not suspended, we can attach our debugger to it.


To attach, open new instance of OllyDbg and click File>Attach>(Choose the PID in hex of the process we want to attach). In our case PID is 2960 which is 0xB90.



When you attach, you will notice that it will be in some address not on the remote thread.




Just press F9 to run it then press F12 to go where our EIP is. We don't bother running it because it is in an infinite loop anyway.  After pressing F12, we expect our EIP to be at EBFE.





And hooray!! Our EBFE is hit. Now you can highlight the code and press Ctrl+E to restore the bytes "55 8B". After restoring the original bytes, we can now fully debug our remote thread.

Final Words

In some cases, there are remote threads where the target process is not suspended. For example, a malware will create a remote thread to a running explorer.exe. In this case, we can easily attach our debugger to it locate the address of the injected code and set breakpoint. Or we just easily attach to it, then "set breakpoint on new thread" and run it. As soon as the remote thread is resumed, Ollydbg will breakpoint at the thread's entry point.

Of course we can also use Windbg which I think is more powerful but we can reserve that tutorial later as I am more experienced with Ollydbg:)

Hope you learn something today. Cheers!




















Thursday, April 10, 2014

Unpacking using Ollydbg

For years, malware take advantage of packers to protect themselves against reverse analysis and AV detection. Third party packers such as UPX, PECompact, Aspack, etc. were being used by malware for years to somehow evade antivirus detection and make reversing difficult . However, due to the fact that these are available tools, AV companies and reversers where able to study them and thus malwares packed with such third party packers can be easily unpacked these days. Today however, malware using a so called hacker-packer is proliferating. Unlike packers such as UPX, hacker-packer tools are not readily available. They are sold and distributed usually underground.

In this blog, we will discuss a technique to unpack most malware using Ollydbg. To start let us first look on the following useful Ollydbg features. We will be using these features for unpacking a malware sample.

  • F9 - Run the program
  • F2 - Set breakpoint
  • ALT+F9 - Return to user code
  • CTRL+G - Go to an expression, e.g., specific address, address of API or addresses pointed by the registers.
  • Follow in Dump - This allows to view the contents of a memory in Ollydbg's memory address view.


We will be using a real malware that came from a spam. You can find the virustotal hits of this sample here. You also notice that it is not using a commercial packer.




Let us now unpack the sample.

1. Load the sample in Ollydbg.
2. Set breakpoint in VirtualAlloc. This is on the assumption that the sample uses VirtualAlloc api. In some cases it uses the native api, ZwAllocateVirtualMemory.  To do this, press CTRL+G and type VirtualAlloc (case sensitive) then Enter. This will point you to the address of VirtualAlloc. Press F2 to set breakpoint.



3. Press F9 to run the sample.
4. If the breakpoint is hit, press ALT+F9 to go to back  to user code.
5. Right click on the value of EAX on the registers section and click "Follow in Dump"



6. Highlight a few bytes on the Memory View and right click. Then select  Breakpoint>Hardware, write>Byte. Then Press F9 to run again the sample. We do this because, when a sample uses VirtualAlloc, that means it is preparing to write something on the allocated memory. We wanted to breakpoint on that as soon as it is being written.



7. If the breakpoint is hit, examine the data. Is it already a PE image? In our case, not yet. Maybe it is still the second layer encryption routine. For this case, press F9 again to run the sample and hope that the VirtualAlloc is hit again.



8. Redo the steps 4,5 and 6.
9. Now the breakpoint is hit and we now see the MZ header. However, it seems it is not yet fully decrypted. We examine the code and it appears to be in a loop. This appears to be the last layer of decryption. Just finish the loop.




10. We are now looking at the decrypted PE file.


11. We just have to dump the memory and we have now an unpacked file.




Additional Notes:

In some cases, the sample is not using VirtualAlloc api so you might be wondering why your breakpoint is not hit. Try ZwAllocateVirtual memory or HeapAlloc. Also, the Breakpoint>Memory, on access can be used in placed for the hardware breakpoint.

Hope you learn something today. Cheers:)