티스토리 뷰

아래 글은 DLL 파일 및 SYS 파일을 어떻게 Olly로 unpack 을 할 수 있는지에 대해서 노하우를 제시해 준다.

    People often ask me how to unpack DLLs and drivers. A common assumption is that it is necessary to use OllyDbg's LOADDLL for unpacking DLLs and that a ring-0 debugger such as SoftICE or WinDbg is necessary for unpacking drivers. With a little tweaking, we can use regular OllyDbg to unpack packed DLLs and even many packed drivers. 

    I don't know about you, but I've always had problems with LOADDLL. Even though it's well documented in OllyDbg's help file (the source is even included in the help file), I'd rather not use it if I don't have to. So how can we load a DLL into OllyDbg so that we can unpack it like we would a normal EXE? 

위에는 별로 중요치 않은 말이고, 아래부터가 Tip 이다.
PE 헤더에서 각 파일의 타입을 나타내주는 Flag 값을 변경함으로써 DLL, SYS 파일을 EXE 처럼 Olly 에서 Tracing 이 가능하다.
즉, EXE 냐 DLL 이냐를 구분하는 항목은 ' IMAGE_NT_HEADER - IMAGE_FILE_HEADER - Characteristic ' 에 지정되어 있는데, 이 Flag 값의 IMAGE_FILE_DLL 항목을 0 으로 설정하면 EXE 로 인식되도록 할 수 있다는 말이다.

    All that you need to do is set the IMAGE_FILE_DLL bit to zero in the Characteristics field of the PE's IMAGE_FILE_HEADER structure. You could use a hex editor to make this change, but it's easier with a PE editor like LordPE. Once this flag is zeroed out, you can load the "DLL" into OllyDbg and OllyDbg and the OS will interpret it as an EXE. You can then unpack it as you would an EXE (trace to the OEP, dump, fix the imports, etc.), and then set the IMAGE_FILE_DLL bit back to one in the unpacked file. 

아래는 DLL 파일을 EXE 로 인식해서 해당 DLL 의 OEP 로 EIP 가 왔을 때 fdwReason 값을 설정해줘야 한다고 지적한다.
DLL 의 동작 코드는 대부분 프로세스에 들러붙을 때 (DLL_PROCESS_ATTACH) 동작을 하게 된다. 그런데, EXE로 동작시키면 이 값이 설정이 제대로 안될테고, 결국 원하는 방향으로 동작이 안되게 되기 때문에 사전에 fdwReason 값을 설정하라는 말이다.

    The only catch is that many unpacking stubs check to see if [EBP+0x0C] == 1 (does the fdwReason argument to DllMain equal DLL_PROCESS_ATTACH), and if it doesn't equal 1 then it won't continue to unpack itself. You can fix this problem by looking for this comparison and forcing a jump/no-jump or by manually pushing three DWORDs onto the stack (before executing the first instruction at the EP), the second of which should be 1. 

위와 같이 PE 헤더를 변경하는 트릭은 SYS 파일에 대해서도 할 수 있다는 말이다. SYS 파일로 인식되는 헤더 값은 IMAGE_NT_HEADER - IMAGE_OPTIONAL_HEADER - Subsystem 항목이다. 이 항목에 IMAGE_SUBSYSTEM_NATIVE 로 설정되어 있는 것을 IMAGE_SUBSYSTEM_WINDOWS_GUI 로 변경하면 되는데, 드라이버의 경우 ring-0 에서 동작되도록 구현되어 있기 때문에 DLL 때 처럼 쉽게 언팩이 가능한 것은 아니나, 다수의 XOR 을 이용해 언팩 stub 을 구성해놓은 경우에는 가능하다는 말로 마치고 있다.

    We can use the same PE header patching trick for loading drivers into OllyDbg for unpacking purposes. By setting the Subsystem field to 2 (IMAGE_SUBSYSTEM_WINDOWS_GUI) in the PE's IMAGE_OPTIONAL_HEADER, OllyDbg and the OS will interpret the file as an EXE instead of as a driver. This allows us to trace through the unpacking stub until the code and data are unpacked, and we can dump the process when we find the OEP. Of course if the unpacking stub is trying to execute instructions/functions that need to be executed from ring-0 then we won't be able to unpack it like this. However, if the unpacking stub is just doing a lot of simple XORing to unpack the original code and data, then we should be able to use this trick to successfully unpack the driver with OllyDbg.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함