Buffer Overflow

Program Memory

Memory segment allocations
1
2
3
4
5
sudo apt-get update
sudo apt-get install -y gcc-multilib g++-multilib
gcc -m32 -W -c mem_layout.c
gcc -m32 -o mem_layout mem_layout.o
size mem_layout mem_layout.o
1
2
gcc -m32 -o mem_layout_print mem_layout_print.o
./mem_layout_print
Mapping process memory to memory layout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
## Stack Memory


- When a function is called, a block of memory called stack frame will 
be pushed onto the top of stack. 
- A stack frame contains four regions:
  - Arguments that are passed to the function (if they don't fit on the 
  general purpose registers)
  - Return address (the address of the instructions right after 
  the function call
  - Previous frame pointer
  - Local variables of the function
- When the program first starts, the stack contains only one frame, 
that of the main function.





<figure
  
>
  <picture>
    <!-- Auto scaling with imagemagick -->
    <!--
      See https://www.debugbear.com/blog/responsive-images#w-descriptors-and-the-sizes-attribute and
      https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images for info on defining 'sizes' for responsive images
    -->
    
      
        <source
          class="responsive-img-srcset"
          
            srcset="/assets/img/courses/csc302/buffer-overflow/stack_mem_layout-480.webp 480w,/assets/img/courses/csc302/buffer-overflow/stack_mem_layout-800.webp 800w,/assets/img/courses/csc302/buffer-overflow/stack_mem_layout-1400.webp 1400w,"
            type="image/webp"
          
          
            sizes="95vw"
          
        >
      
    
    <img
      src="/assets/img/courses/csc302/buffer-overflow/stack_mem_layout.png"
      
      
        width="50%"
      
      
        height="auto"
      
      
      
        alt="Stack memory layout"
      
      
      
        data-zoomable
      
      
        loading="lazy"
      
      onerror="this.onerror=null; $('.responsive-img-srcset').remove();"
    >
  </picture>

  
</figure>




- We can call a function from inside a function. 
- Any time we enter a function, a stack from is allocated on top of the stack. 
  - When the function returns, the allocated space is released. 





<figure
  
>
  <picture>
    <!-- Auto scaling with imagemagick -->
    <!--
      See https://www.debugbear.com/blog/responsive-images#w-descriptors-and-the-sizes-attribute and
      https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images for info on defining 'sizes' for responsive images
    -->
    
      
        <source
          class="responsive-img-srcset"
          
            srcset="/assets/img/courses/csc302/buffer-overflow/call_chain-480.webp 480w,/assets/img/courses/csc302/buffer-overflow/call_chain-800.webp 800w,/assets/img/courses/csc302/buffer-overflow/call_chain-1400.webp 1400w,"
            type="image/webp"
          
          
            sizes="95vw"
          
        >
      
    
    <img
      src="/assets/img/courses/csc302/buffer-overflow/call_chain.png"
      
      
        width="50%"
      
      
        height="auto"
      
      
      
        alt="Chain of function calls"
      
      
      
        data-zoomable
      
      
        loading="lazy"
      
      onerror="this.onerror=null; $('.responsive-img-srcset').remove();"
    >
  </picture>

  
</figure>


Buffer overflow attack

1
2
gcc -m32 -o strcpy_overflow strcpy_overflow.c
./strcpy_overflow
1
2
3
4
git clone https://github.com/longld/peda.git
echo "source $HOME/peda/peda.py" > $HOME/.gdbinit
gcc -m32 -g -o gdb_strcpy_overflow strcpy_overflow.c
gdb gdb_strcpy_overflow
Design a buffer overflow exploit
1
2
3
./mem_layout_print
./mem_layout_print
./mem_layout_print
1
echo 0 | sudo tee /proc/sys/kernel/randomize_va_space
1
2
3
./mem_layout_print
./mem_layout_print
./mem_layout_print
1
2
3
4
5
6
7
gcc -m32 -o stack -z execstack -fno-stack-protector stack.c
sudo chown root stack
sudo chmod 4755 stack
echo "aaaa" > badfile
./stack
echo "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" > badfile
./stack
1
2
3
4
gcc -m32 -g -o gdb_stack -z execstack -fno-stack-protector stack.c
rm badfile
touch badfile
gdb gdb_stack
1
2
3
4
5
6
break foo
run
print $ebp
print &buffer
print <hex address of ebp> - <hex address of buffer>
quit
Why 108
NOP to improve attach chance
NOP to improve attach chance

```