Unpack packed executable file

1. Create an executable file

(1) Create a C Program

Create a new folder called “test” in C:/, and create a text document called “1.cpp” in the folder.
Like this:
新建文件
Right click the file and edit it with Notepad++. Input the codes as follows.
在这里插入图片描述

#include<stdio.h>
int main (){
    
    
	printf("Hello World!\n");
	return 0;
}

Save the file.

(2) Compile the C program

Open the Developer’s command prompt of Visual Studio 2017 in the start menu.
在这里插入图片描述
Switch to the file location C:\test using “cd C:\test”
在这里插入图片描述
Then compile 1.cpp using “cl 1.cpp”
在这里插入图片描述

We can run it using command “1.exe”
在这里插入图片描述
Success.

2.Pack the program

(1) Download UPX

UPX is a free, portable, extendable, high-performance executable packer for several executable formats.

We will use UPX to pack the executable file created above
Download upx from https://github.com/upx/upx/releases/download/v3.96/upx-3.96-win32.zip
Extract the zip file

(2) Pack 1.exe

  1. Go to the root directory of upx in Explorer, input “cmd” in path and press Enter to enter cmd in the path of upx.
    在这里插入图片描述
    In that way, we don’t need to input the path of upx manually.
    在这里插入图片描述
  2. Use command “upx.exe C:\test\1.exe -o C:\test\1_packed.exe” to pack it.
    在这里插入图片描述
  3. 1_packed.exe is a packed file with the same function of 1.exe but the internal structure is different from the later. We can open it with IDA. The procedure and imports are as follows.
    structure overview of 1_packed.exe
    Imports of 1_packed.exe
    However, the procedure and imports of 1.exe are as follows.
    在这里插入图片描述
    在这里插入图片描述

3. Unpack the program

(1) Find Original Entry Point with x32dbg

  1. Open x32dbg
  2. Drug 1_packed.exe into x32dbg
  3. Press F9 to run.
  4. Find the last jmp command
    The last jmp command is the selected command in the picture.
    在这里插入图片描述
  5. Press F2 to set breakpoint for that command. Then Press F9 to run it.
    在这里插入图片描述
  6. Press F8, then we see the program which looks like C program. It begins with a “call” and a “jmp” command. In C programs the “call” command is always the entry point of the program.
    在这里插入图片描述

(3) Use PE Tools to save dump file

  1. Keep the state of x32dbg. Open PETools and find the process by its PID.
    在这里插入图片描述
  2. Right click on it and select “Dump full” to save the file.
    在这里插入图片描述
  3. We can find Dumped.exe cannot be run. Open it with IDA. Remeber to Press “yes” here.
    (Click here and look at the first part of the article to know how to open a executable file with IDA)
    在这里插入图片描述
  4. We will find the Imports of it is empty. And there is no “strat” in function name table.
    在这里插入图片描述
    在这里插入图片描述

(4) Restore entry point

  1. Open PETools. Tools -> PE Editor. Open Dumped.exe
    在这里插入图片描述
  2. Click “Optional Header”.
    在这里插入图片描述
  3. The address of entry point is “010812E1” .
    在这里插入图片描述
  4. The address of entry point is image base plus entry point. “01080000” is image base. So the entry point is 010812E1-01080000=12E1. Change the entry point inOptional Header to 12E1.
    在这里插入图片描述
  5. Press Ok and press Ok.
  6. Open Dumped.exe in IDA. Then we can see “start” in function name table. That means the entry point has been changed successfully.
    在这里插入图片描述

(5) Get the Imports of the program

  1. Close IDA. Run Scylla in x32dbg.
    在这里插入图片描述
  2. “EIP” must point to the actual entry point of the program.
    在这里插入图片描述
  3. Then press “IAT Autosearch” to automatically get the address of Import Address Table. Import Addre Table is the list of addresses of extral functions for this file.
    在这里插入图片描述
  4. Copy VA. Click to dump press Ctrl+G and input VA. Press OK.
    在这里插入图片描述
  5. Right click to show address.
    在这里插入图片描述
  6. We can see that the fisrt line is empty. So the address of “VA” needs to change to 1094000.
    在这里插入图片描述
  7. Scroll down, then we can see the address of import table ends at 1094104.
    在这里插入图片描述
  8. So the size of import address table need to change to 104+4=108.
    在这里插入图片描述
  9. Then press “Get Imports”. Then the functions will be imported.
    在这里插入图片描述
  10. Click “Fix Dump” and select Dump.exe. Then we got Dump_SCY.exe
    在这里插入图片描述
  11. Open Dump-SCY.exe with IDA and we can see the imports.

在这里插入图片描述
12. Run it in cmd. We can see it cannot run properly.
在这里插入图片描述

(6) Prevent address randomization

  1. Make x32dbg the default debugger of the system. Run x32dbg as administrator. Options -> Preferences -> Misc -> “Set x32dbg as Just in Time debugger.” -> Save.
    在这里插入图片描述

  2. Run Dumped_SCY.exe again, and select Debug the program.
    在这里插入图片描述

  3. Press F8 until we see the exception.
    在这里插入图片描述

  4. Press in dump, press Ctrl+G, and input 109B018. We can see that is an invalid address.
    在这里插入图片描述

  5. Because when a process starts its base address will be chosen randomly by the operation system. We need to prevent address randomization.

  6. Open PETools -> Tools -> PE Editor. Open Dumped_SCY.exe. Go to File Header.
    在这里插入图片描述

  7. Go to characteristics.
    在这里插入图片描述

  8. Enable Reloaction stripped.
    在这里插入图片描述

  9. Press OK and OK and OK. Run it again. Success!
    在这里插入图片描述

Finish unpacking!

猜你喜欢

转载自blog.csdn.net/weixin_43529394/article/details/113487876