Skip to content

Commit d143784

Browse files
committed
labs/lab-10: Add checker infrastructure
- Added for each task a directory named tests - Added for each task a specified checker in directory tests - Added for each task an exploit.py file with some TODOs - Updated the hardcoded part and got rid of the fixed payload - Added instructions in the README about completing exploit.py TODOs and running make check in tests for showing the results - Added some detailed explanations in README files - Deleted the exploit.py files from stack_buffer directory - Added more detailed tests for stack_buffer exercise Fixes #43 Signed-off-by: Alexandru Braslasu <[email protected]>
1 parent 7dc238e commit d143784

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1312
-2
lines changed

labs/lab-10/tasks/overflow-for-binary/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ If you're having difficulties solving this exercise, go through [this](../../rea
2121

2222
> **WARNING** If you try using a payload generated with python and it doesn't work, try simply copying its content in the terminal
2323
24+
## Checking Your Solution
25+
26+
In order to verify your exploit, please complete the `exploit.py` TODOs.
27+
Afterwards, navigate to the `tests` directory and run:
28+
29+
```Bash
30+
make check
31+
test........................................passed
32+
Total: 100/100
33+
```
34+
35+
If your solution is correct, you will receive a `100/100` result.
36+
2437
## Resources
2538

2639
If you found the laboratory interesting in a positive way, you can learn more about this type of attack, as well as cybersecurity in general, on this [channel](https://www.youtube.com/c/LiveOverflow).
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
import subprocess
3+
4+
5+
def run_executable():
6+
argument = 32 * "A" + "\x50\x52\x30\x4e"
7+
subprocess.run(["./overflow_in_binary", argument])
8+
9+
10+
if __name__ == "__main__":
11+
run_executable()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
import subprocess
3+
4+
5+
def run_executable():
6+
argument = "" # TODO: Put here the payload you have discovered
7+
subprocess.run(["../support/overflow_in_binary", argument])
8+
9+
10+
if __name__ == "__main__":
11+
run_executable()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CC = gcc
2+
CFLAGS = -Wall -Wextra -O2
3+
TARGET = test_overflow_for_binary
4+
5+
SRCS = test_overflow_for_binary.c graded_test.c
6+
7+
OBJS = $(SRCS:.c=.o)
8+
9+
all: $(TARGET)
10+
11+
$(TARGET): $(OBJS)
12+
$(CC) $(CFLAGS) $(OBJS) -o $@
13+
14+
%.o: %.c
15+
$(CC) $(CFLAGS) -c $< -o $@
16+
17+
clean:
18+
rm -f $(TARGET) $(OBJS)
19+
20+
check: all
21+
./run_all_tests.sh
22+
$(MAKE) clean
23+
24+
.PHONY: all clean test
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
3+
#include <string.h>
4+
#include <stdlib.h>
5+
#include <unistd.h>
6+
#include <sys/param.h>
7+
8+
#include "./graded_test.h"
9+
10+
/*
11+
* Print test result. Printed message should fit in 72 characters.
12+
*
13+
* Print format is:
14+
*
15+
* description ...................... passed ... NNN
16+
* description ...................... failed ... NNN
17+
* 32 chars 24 chars 6 3 3
18+
*/
19+
20+
static void print_test(const char *description, int result)
21+
{
22+
/* Make these global linkage, so it's only allocated once. */
23+
static char print_buffer[74];
24+
static const char failed[] = "failed";
25+
static const char passed[] = "passed";
26+
size_t i;
27+
size_t len;
28+
29+
/* Collect description in print_buffer. */
30+
len = MIN(strlen(description), 32);
31+
for (i = 0; i < len; i++)
32+
print_buffer[i] = description[i];
33+
34+
/* Collect dots in print_buffer. */
35+
for (i = 0; i < 40; i++)
36+
print_buffer[12+i] = '.';
37+
38+
/* Collect passed / failed. */
39+
for (i = 0; i < 6; i++) {
40+
if (result == 1)
41+
print_buffer[52+i] = passed[i];
42+
else
43+
print_buffer[52+i] = failed[i];
44+
}
45+
46+
/* Collect newline. */
47+
print_buffer[59] = '\n';
48+
49+
int ret = write(1, print_buffer, 58);
50+
51+
if (ret == -1)
52+
return;
53+
}
54+
55+
void run_test(struct graded_test *test)
56+
{
57+
int res;
58+
59+
res = test->function();
60+
print_test(test->description, res);
61+
#ifdef EXIT_IF_FAIL
62+
exit(EXIT_FAILURE);
63+
#endif
64+
}
65+
66+
void run_tests(struct graded_test *tests, size_t count)
67+
{
68+
size_t i;
69+
70+
for (i = 0; i < count; i++)
71+
run_test(&tests[i]);
72+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
3+
#include <stddef.h>
4+
5+
#ifndef GRADED_TEST_H_
6+
#define GRADED_TEST_H_ 1
7+
8+
/* test function prototype */
9+
typedef int (*test_f)(void);
10+
11+
struct graded_test {
12+
test_f function; /* test/evaluation function */
13+
char *description; /* test description */
14+
size_t points; /* points for each test */
15+
};
16+
17+
void run_test(struct graded_test *test);
18+
void run_tests(struct graded_test *tests, size_t count);
19+
20+
#endif /* GRADED_TEST_H_ */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
4+
./test_overflow_for_binary | tee results.txt
5+
6+
total=$(if tail -n 1 results.txt | grep -q 'passed$'; then echo 100; else echo 0; fi)
7+
echo ""
8+
echo -n "Total: "
9+
echo -n " "
10+
LC_ALL=C printf "%3d/100\n" "$total"
11+
12+
rm results.txt
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include <stdlib.h>
6+
#include <assert.h>
7+
#include <sys/wait.h>
8+
9+
#include "graded_test.h"
10+
11+
static int test(void)
12+
{
13+
FILE *pipe = popen("python3 ../support/exploit.py", "r");
14+
15+
if (!pipe)
16+
return 0;
17+
18+
char buffer[512];
19+
int found = 0;
20+
21+
while (fgets(buffer, sizeof(buffer), pipe))
22+
if (strstr(buffer, "Great success!") != NULL)
23+
found = 1;
24+
25+
return found;
26+
}
27+
28+
static struct graded_test all_tests[] = {
29+
{ test, "test", 100},
30+
};
31+
32+
int main(void)
33+
{
34+
run_tests(all_tests, sizeof(all_tests) / sizeof(all_tests[0]));
35+
return 0;
36+
}

labs/lab-10/tasks/overflow-in-c/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,17 @@ Not quite there. Try again!
9696
Aborted (core dumped)
9797
```
9898

99+
## Checking Your Solution
100+
101+
In order to verify your exploit, please complete the `exploit.py` TODOs.
102+
Afterwards, navigate to the `tests` directory and run:
103+
104+
```Bash
105+
make check
106+
test........................................passed
107+
Total: 100/100
108+
```
109+
110+
If your solution is correct, you will receive a `100/100` result.
111+
99112
If you're having difficulties solving this exercise, go through [this](../../reading/overflow-vuln.md) reading material.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
import subprocess
3+
4+
5+
def run_executable():
6+
subprocess.run(["make"], check=True, cwd="../support")
7+
payload = 73 * "A" + "\x4d\x49\x41\x55"
8+
subprocess.run(["../support/do_overflow"], input=payload, universal_newlines=True)
9+
subprocess.run(["make", "clean"], check=True, cwd="../support")
10+
11+
12+
if __name__ == "__main__":
13+
run_executable()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
import subprocess
3+
4+
5+
def run_executable():
6+
subprocess.run(["make"], check=True, cwd="../support")
7+
payload = "" # TODO: Put here the payload you have discovered
8+
subprocess.run(["../support/do_overflow"], input=payload, universal_newlines=True)
9+
subprocess.run(["make", "clean"], check=True, cwd="../support")
10+
11+
12+
if __name__ == "__main__":
13+
run_executable()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CC = gcc
2+
CFLAGS = -Wall -Wextra -O2
3+
TARGET = test_overflow_in_C
4+
5+
SRCS = test_overflow_in_C.c graded_test.c
6+
7+
OBJS = $(SRCS:.c=.o)
8+
9+
all: $(TARGET)
10+
11+
$(TARGET): $(OBJS)
12+
$(CC) $(CFLAGS) $(OBJS) -o $@
13+
14+
%.o: %.c
15+
$(CC) $(CFLAGS) -c $< -o $@
16+
17+
clean:
18+
rm -f $(TARGET) $(OBJS)
19+
20+
check: all
21+
./run_all_tests.sh
22+
$(MAKE) clean
23+
24+
.PHONY: all clean test
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
3+
#include <string.h>
4+
#include <stdlib.h>
5+
#include <unistd.h>
6+
#include <sys/param.h>
7+
8+
#include "./graded_test.h"
9+
10+
/*
11+
* Print test result. Printed message should fit in 72 characters.
12+
*
13+
* Print format is:
14+
*
15+
* description ...................... passed ... NNN
16+
* description ...................... failed ... NNN
17+
* 32 chars 24 chars 6 3 3
18+
*/
19+
20+
static void print_test(const char *description, int result)
21+
{
22+
/* Make these global linkage, so it's only allocated once. */
23+
static char print_buffer[74];
24+
static const char failed[] = "failed";
25+
static const char passed[] = "passed";
26+
size_t i;
27+
size_t len;
28+
29+
/* Collect description in print_buffer. */
30+
len = MIN(strlen(description), 32);
31+
for (i = 0; i < len; i++)
32+
print_buffer[i] = description[i];
33+
34+
/* Collect dots in print_buffer. */
35+
for (i = 0; i < 40; i++)
36+
print_buffer[12+i] = '.';
37+
38+
/* Collect passed / failed. */
39+
for (i = 0; i < 6; i++) {
40+
if (result == 1)
41+
print_buffer[52+i] = passed[i];
42+
else
43+
print_buffer[52+i] = failed[i];
44+
}
45+
46+
/* Collect newline. */
47+
print_buffer[59] = '\n';
48+
49+
int ret = write(1, print_buffer, 58);
50+
51+
if (ret == -1)
52+
return;
53+
}
54+
55+
void run_test(struct graded_test *test)
56+
{
57+
int res;
58+
59+
res = test->function();
60+
print_test(test->description, res);
61+
#ifdef EXIT_IF_FAIL
62+
exit(EXIT_FAILURE);
63+
#endif
64+
}
65+
66+
void run_tests(struct graded_test *tests, size_t count)
67+
{
68+
size_t i;
69+
70+
for (i = 0; i < count; i++)
71+
run_test(&tests[i]);
72+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
3+
#include <stddef.h>
4+
5+
#ifndef GRADED_TEST_H_
6+
#define GRADED_TEST_H_ 1
7+
8+
/* test function prototype */
9+
typedef int (*test_f)(void);
10+
11+
struct graded_test {
12+
test_f function; /* test/evaluation function */
13+
char *description; /* test description */
14+
size_t points; /* points for each test */
15+
};
16+
17+
void run_test(struct graded_test *test);
18+
void run_tests(struct graded_test *tests, size_t count);
19+
20+
#endif /* GRADED_TEST_H_ */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
4+
./test_overflow_in_C | tee results.txt
5+
6+
total=$(if tail -n 1 results.txt | grep -q 'passed$'; then echo 100; else echo 0; fi)
7+
echo ""
8+
echo -n "Total: "
9+
echo -n " "
10+
LC_ALL=C printf "%3d/100\n" "$total"
11+
12+
rm results.txt

0 commit comments

Comments
 (0)