Skip to main content

My notes from Gray Hat Python book

I've picked up the book Gray Hat Python by Justin Seitz and although it's not exactly an introductory book, I think even python or security noob can find some some pretty cool stuff that would make it worth the price. I've decided to use this blog as a note book for a quick reference later in the next more advanced chapters if I need to. Much of the stuff included in these notes below, I learned before in Assembly language class years ago, but I haven't played with assembly in a while, and I wish I had taken easily accessible notes to jog my memory.

The following is just basic CPU architecture taken from chapter 2:


In the x86 instruction set, a CPU uses eight general-purpose registers: EAX, EDX, ECX, ESI, EDI, EBP, ESP, and EBX. The registers are small pieces of storage designed for for quick access by CPU. The eight general-purpose registers have specific roles and are designed to enable to CPU to efficiently process instructions.

The EAX register:
This register is also called the accumulator register, is used for performing calculations as well as storing return values from function calls. Many optimized instructions in the x86 instruction set are designed to move data into and out of the EAX register and perform calculations on that data. Most basic operations like add, subtract, and compare are optimized to use the EAX register. As well, more specialized operations like multiplication or division can occur only within the EAX register. Because return values from function calls are stored in EAX, we can easily determine if a function call has failed or succeeded based on the value stored in EAX, in addition, we can determine the actual value of what the function is returning.

The EDX register:
This is the data register. It is basically an extension of the EAX register, and it assists in storing extra data for more complex calculations like multiplication and division. It can also be used for general-purpose storage, but it is most commonly used in conjunction with calculations performed with the EAX register.

The ECX register:
This is also called the count register, and is used for looping operations. The repeated operations could be storing a string or counting numbers. An important point to understand is that ECX counts downward, not upward. Take the following snippet in Python, for example:

counter = 0

while counter < 10:
print "Loop number: %d" % counter
counter += 1

If you were to translate this code to assembly, ECX would equal 10 on the first loop, 9 on the second loop, and so on. This is a bit confusing, as it is the reverse of what is shown in Python, but just remember that it's always a downward count, and you'll be fine.


The ESI and EDI registers:
In x86 assembly, loops that process data rely on the ESI and EDI registers for efficient data manipulation. The ESI register is the source index for the data operation and holds the location of the input data stream. The EDI register points to the location where the result of a data operation is stored, or the destination index. An easy way to remember this is that ESI is used for reading and EDI is used for writing. Using the source and destination index registers for data operation greatly improves the performance of the running program.

The ESP and EBP registers:
These are the stack pointer and the base pointer, respectively. These registers are used for managing function calls and stack operations. When a function is called, the arguments to the function are pushed onto the stack and are followed by the return address. The ESP register points to the very top of the stack, and so it will point to the return address. The EBP register is used to point to the bottom of the call stack. In some circumstances a compiler may use optimizations to remove the EBP register as a stack frame pointer; in these cases the EBP register is freed up to be used like any other general-purpose register.

The EBX register is the only register that was not designed for anything specific. It can be used for extra storage.

One extra register that should be mentioned is the EIP register. This register points to the current instruction that is being executed. As the CPU moves through the binary executing code, EIP is updated to reflect the location where the execution is occurring.

A debugger must be able to easily read and modify the contents of these registers. Each operating system provides an interface for the debugger to interact with the CPU and retrieve or modify these values. We'll cover the individual interfaces in the operating system—specific chapters.

Comments

Popular posts from this blog

OpenDPI

I stumbled on an open source project that looks very interesting. I am posting this to remind me to come back to it later when I have some more time to check out the code, and what it's all about. The source code is hosted on google here: http://code.google.com/p/opendpi/ This the project's website: http://opendpi.org/ I might write more about it later.

Gray Hat Python...the Stack

The Stack The stack is a very important structure to understand when developing a debugger. The stack stores information about how a function is called, the parameters it takes, and how it should return after it is finished executing. The stack is a First In, Last Out (FILO) structure, where arguments are pushed onto the stack for a function call and popped off the stack when the function is finished. The ESP register is used to track the very top of the stack frame, and the EBP register is used to track the bottom of the stack frame. The stack grows from high memory addresses to low memory addresses. Let's use our previously covered function my_socks() as a simplified example of how the stack works. Function Call in C int my_socks(color_one, color_two, color_three); Function Call in x86 Assembly push color_three push color_two push color_one call my_socks To see what the stack frame would look like, refer to Figure 2-1. Image from book Figure 2-1: Stack frame for the my_socks...