Lecture4
Lecture4
and C++
Pointer Subterfuge
Lecture 4
Jan 22, 2013
Acknowledgement: These slides are based on author Seacord’s original
presentation
Pointer Subterfuge
A pointer is a variable that contains the address of a
function, array element, or other data structure.
The default binary format on Linux, Solaris 2.x, and SVR4 is called
the executable and linking format (ELF).
The location of the GOT entry for a function can be found using the
objdump
The .ctors and .dtors sections are mapped into the process
address space and are writable by default.
Constructors have not been used in exploits because they are
called before the main program.
The focus is on destructors and the .dtors section.
The contents of the .dtors section in the executable image can
be examined with the objdump command
The .dtors Section - 2
An attacker can transfer control to arbitrary code by overwriting the
address of the function pointer in the .dtors section.
The .dtors section consists of the head and tail tag with no function
addresses between.
A pointer my_b to the base class
19. int _tmain(int argc, _TCHAR* argv[]) {
20. a *my_b = new b(); is declared in main() but
21. my_b->f(); assigned to an object of the
22. my_b->g();
23. return derived classb.
Virtual Pointers - Example
Program- 1
19. int _tmain(int argc, _TCHAR* argv[]) {
20. a *my_b = new b();
21. my_b->f(); A pointer my_b to the base class
22. my_b->g(); is declared in main() but
23. return assigned to an object of the
derived class b.
b object b vtable
my_b g()
other
virtual
function
The atexit() and on_exit()
Functions - 1
The atexit() function is a general utility function
defined in C99.
The atexit() function registers a function to be called
without arguments at normal program termination.
C99 requires that the implementation support the
registration of at least 32 functions.
The on_exit() function from SunOS performs a
similar function.
This function is also present in libc4, libc5, and glibc
The atexit() and on_exit() –
Example Program
1. char *glob;
2. void test(void) {
3. printf("%s", glob);
4. }
5. void main(void) {
6. atexit(test);
7. glob = "Exiting.\n";
8. }
The atexit() and on_exit()
Functions - 2
The atexit() function works by adding a specified
function to an array of existing functions to be called
on exit.
6. void f(void) {
7. setjmp(buf);
8. g(n);
9. }