Skip to content

Instantly share code, notes, and snippets.

@k1R4
Last active December 19, 2021 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k1R4/ca1211bed4bd7d58ce335672ed96a1aa to your computer and use it in GitHub Desktop.
Save k1R4/ca1211bed4bd7d58ce335672ed96a1aa to your computer and use it in GitHub Desktop.
overwrite_simulator - InCTF Nationals 2021

Overwrite Simulator

tl;dr - Overwrite global variable to get libc leak and overwrite GOT(getchar) with one_gadget

Description

you can't overwrite me if you don't have a leak.

Challenge author: k1R4

handout: overwrite_simulator.zip

Write-up

Initial Analysis

We have a menu driven program where we have 2 options: Overwriting 8 bytes at an address of choice, Printing out "Hello there!" stored in a global variable. Partial overwrites aren't possible here because of memcpy, so we need a leak.

void overwrite(){
	int addr;
	char data[9];

	printf("Enter address to overwrite: ");
	scanf("%d",&addr);
	getchar();
	printf("Enter data to be written: ");
	fgets(data,9,stdin);
	memcpy(addr,&data,8);
}

Exploitation

PIE and Full RELRO are disabled which means we can overwrite the GOT but before that we need a leak. The second option in the menu can be used to leak since it uses printf to print the string in the global variable. Hence overwriting the global variable with an appropriate format string will give us a libc leak from the stack.

Now that we have a leak we can overwrite a specific GOT entry with a one_gadget. We have this useful one_gadget:

execve("/bin/sh", rsi, rdx)
 constraints:
   [rsi] == NULL || rsi == NULL
   [rdx] == NULL || rdx == NULL

The constraints are met when getchar is called after scanf in main. Thus overwriting GOT(getchar) with this one_gadget will give us a shell.

In summary,

  • overwrite global variable with format string to get libc leak
  • ovrwrite GOT(getchar) with one_gadget to get shell

Conclusion

Final exploit can be found here

flag: inctf{0v3rwriting_th3_G07_is_e4sy!}

Hope you enjoyed the challenge as much as I did making it :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment