LAB 9.3 Disassembling C on Windows
LAB 9.3 Disassembling C on Windows
+ 10 extra credit)
What You Need
A Windows machine, real or virtual. I used Windows 7.
Visual Studio Express, which you installed in a previous project.
IDA Pro Free, which you installed in a previous project.
Purpose
You will write a small C programs, compile it, and examine it in the IDA Pro disassembler to learn what it looks like in assembly language.
In the "New Project" window, on the left, expand the "Visual C++" container.
Click Win32.
In the center pane, accept the default selection of "Win32 Console Application".
At the bottom of the "New Project" window, type a Name of YOURNAME-8a, replacing "YOURNAME" with your own name. Do not use any spaces in the
name.
In the "Location" line, notice the location files will be saved in--it's a subfolder of your Documents folder.
Click Next. In the next screen, accept the default settings and click Finish.
Modify this program to match the code shown in text and the image below.
Do not use the literal string "YOURNAME"--replace it with your own name.
#include "stdafx.h"
You should see the message "Build: 1 succeeded" at the bottom of the window. If you see errors, you need to correct them and re-compile the program.
A Command Prompt window opens, showing the output of "1 2", as shown below:
In the "Select PE Executable to disassemble" box, navigate to the folder you used to save your program. The default location is in your Documents folder, in a
subfolder named "visual studio 2013\Projects".
A box appears, saying this file was linked with debug information, as shown below. This is a luxury you won't often have with malware, but it's nice for this
project.
Click Yes
IDA Pro loads the file. Unfortunately, the graph mode isn't much use, as shown below.
However, we can still find the code. Expand the Strings window and find "YOURNAME-8a %d %d\n", as shown below.
This is in the .rdata section of the file, which contains data but not executable instructions.
To the right of "YOURNAME-8a" there is a "DATA XREF" comment. Hover over the address to the right of "DATA XREF", which was "wmain+32" when I did
it.
The instructions that use this string appear in a yellow pop-up box, as shown below.
Double-click "wmain+32".
Now the assembly code that performs the task you wrote in C appears, as shown below.
int j=2;
printf("YOURNAME-8a: %d %d\n", i, j);
as shown above. The offset value may be different, but it should contain push and YOURNAME.
j is a local variable, so it is simply stored on the stack at the location ebp+var_8. It's temporary, only available to the function it's defined in.
i is a global variable, and in this case IDA was able to refer to it by name in the "mov ecx, i" instruction.
A yellow box pops up showing where it is stored. When I did it, it was stored at location 418000, as shown below.
Two local variables as shown in the top green box in the figure below: two mov instructions referencing stack locations such as [ebp+var_14], each
followed by a push instruction.
Two global variables as shown in the lower green box in the figure below: two mov instructions referencing named variables such as x, each followed by
a push instruction.