26.07.2018 Views

hacking-the-art-of-exploitation

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

strncpy(description, desc, MAX_DESC_LEN);<br />

strcpy(product_code, id);<br />

}<br />

printf("Updating product #%s with description \'%s\'\n", product_code, desc);<br />

// Update database<br />

int main(int argc, char *argv[], char *envp[])<br />

{<br />

int i;<br />

char *id, *desc;<br />

if(argc < 2)<br />

barf("Usage: %s \n", argv[0]);<br />

id = argv[1]; // id - Product code to update in DB<br />

desc = argv[2]; // desc - Item description to update<br />

if(strlen(id) > MAX_ID_LEN) // id must be less than MAX_ID_LEN bytes.<br />

barf("Fatal: id argument must be less than %u bytes\n", (void *)MAX_ID_LEN);<br />

for(i=0; i < strlen(desc)-1; i++) { // Only allow printable bytes in desc.<br />

if(!(isprint(desc[i])))<br />

barf("Fatal: description argument can only contain printable bytes\n", NULL);<br />

}<br />

// Clearing out <strong>the</strong> stack memory (security)<br />

// Clearing all arguments except <strong>the</strong> first and second<br />

memset(argv[0], 0, strlen(argv[0]));<br />

for(i=3; argv[i] != 0; i++)<br />

memset(argv[i], 0, strlen(argv[i]));<br />

// Clearing all environment variables<br />

for(i=0; envp[i] != 0; i++)<br />

memset(envp[i], 0, strlen(envp[i]));<br />

printf("[DEBUG]: desc is at %p\n", desc);<br />

}<br />

update_product_description(id, desc); // Update database.<br />

Despite <strong>the</strong> vulnerability, <strong>the</strong> code does make an attempt at security.<br />

The length <strong>of</strong> <strong>the</strong> product ID argument is restricted, and <strong>the</strong> contents <strong>of</strong> <strong>the</strong><br />

description argument are limited to printable characters. In addition, <strong>the</strong><br />

unused environment variables and program arguments are cleared out for<br />

security reasons. The first argument (id) is too small for shellcode, and since<br />

<strong>the</strong> rest <strong>of</strong> <strong>the</strong> stack memory is cleared out, <strong>the</strong>re’s only one place left.<br />

364 0x600

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

Saved successfully!

Ooh no, something went wrong!