26.07.2018 Views

hacking-the-art-of-exploitation

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

With <strong>the</strong> addresses <strong>of</strong> <strong>the</strong> variables displayed, it is apparent that <strong>the</strong><br />

static_var in main() is different than <strong>the</strong> one found in function(), since <strong>the</strong>y are<br />

located at different memory addresses (0x804968c and 0x8049688, respectively).<br />

You may have noticed that <strong>the</strong> addresses <strong>of</strong> <strong>the</strong> local variables all have very<br />

high addresses, like 0xbffff814, while <strong>the</strong> global and static variables all have<br />

very low memory addresses, like 0x0804968c and 0x8049688. That’s very astute<br />

<strong>of</strong> you—noticing details like this and asking why is one <strong>of</strong> <strong>the</strong> cornerstones <strong>of</strong><br />

<strong>hacking</strong>. Read on for your answers.<br />

0x270<br />

Memory Segmentation<br />

A compiled program’s memory is divided into five segments: text, data, bss,<br />

heap, and stack. Each segment represents a special portion <strong>of</strong> memory that is<br />

set aside for a certain purpose.<br />

The text segment is also sometimes called <strong>the</strong> code segment. This is where<br />

<strong>the</strong> assembled machine language instructions <strong>of</strong> <strong>the</strong> program are located.<br />

The execution <strong>of</strong> instructions in this segment is nonlinear, thanks to <strong>the</strong><br />

aforementioned high-level control structures and functions, which compile<br />

into branch, jump, and call instructions in assembly language. As a program<br />

executes, <strong>the</strong> EIP is set to <strong>the</strong> first instruction in <strong>the</strong> text segment. The<br />

processor <strong>the</strong>n follows an execution loop that does <strong>the</strong> following:<br />

1. Reads <strong>the</strong> instruction that EIP is pointing to<br />

2. Adds <strong>the</strong> byte length <strong>of</strong> <strong>the</strong> instruction to EIP<br />

3. Executes <strong>the</strong> instruction that was read in step 1<br />

4. Goes back to step 1<br />

Sometimes <strong>the</strong> instruction will be a jump or a call instruction, which<br />

changes <strong>the</strong> EIP to a different address <strong>of</strong> memory. The processor doesn’t<br />

care about <strong>the</strong> change, because it’s expecting <strong>the</strong> execution to be nonlinear<br />

anyway. If EIP is changed in step 3, <strong>the</strong> processor will just go back to step 1<br />

and read <strong>the</strong> instruction found at <strong>the</strong> address <strong>of</strong> whatever EIP was changed to.<br />

Write permission is disabled in <strong>the</strong> text segment, as it is not used to store<br />

variables, only code. This prevents people from actually modifying <strong>the</strong> program<br />

code; any attempt to write to this segment <strong>of</strong> memory will cause <strong>the</strong><br />

program to alert <strong>the</strong> user that something bad happened, and <strong>the</strong> program<br />

will be killed. Ano<strong>the</strong>r advantage <strong>of</strong> this segment being read-only is that it<br />

can be shared among different copies <strong>of</strong> <strong>the</strong> program, allowing multiple<br />

executions <strong>of</strong> <strong>the</strong> program at <strong>the</strong> same time without any problems. It should<br />

also be noted that this memory segment has a fixed size, since nothing ever<br />

changes in it.<br />

The data and bss segments are used to store global and static program<br />

variables. The data segment is filled with <strong>the</strong> initialized global and static variables,<br />

while <strong>the</strong> bss segment is filled with <strong>the</strong>ir uninitialized counterp<strong>art</strong>s. Although<br />

<strong>the</strong>se segments are writable, <strong>the</strong>y also have a fixed size. Remember that global<br />

variables persist, despite <strong>the</strong> functional context (like <strong>the</strong> variable j in <strong>the</strong><br />

previous examples). Both global and static variables are able to persist<br />

because <strong>the</strong>y are stored in <strong>the</strong>ir own memory segments.<br />

Programming 69

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!