Skip to content

Commit 4eaa244

Browse files
committed
feat: lab8
1 parent c65c3d2 commit 4eaa244

File tree

8 files changed

+129
-7
lines changed

8 files changed

+129
-7
lines changed

.github/workflows/lab-autograding.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
const files = await github.rest.pulls.listFiles({ owner, repo, pull_number: issue_number });
4646
const changedFiles = files.data.map((file) => file.filename);
4747
const allowedFileRegex = /^lab\d+\/main_test.js$/;
48-
const specialChangedFiles = ["lab0/lab0.js", "lab5/antiasan.c", "lab6/llvm-pass.so.cc"];
48+
const specialChangedFiles = ["lab0/lab0.js", "lab5/antiasan.c", "lab6/llvm-pass.so.cc", "lab8/solve.py"];
4949
if (!changedFiles.every((file) => (allowedFileRegex.test(file) || specialChangedFiles.includes(file)))) {
5050
core.setFailed('The PR contains changes to files other than the allowed files.');
5151
}

lab5/antiasan.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
#include <sanitizer/asan_interface.h>
1+
#include <string.h>
22

3-
//TODO:
43
void antiasan(unsigned long addr)
54
{
6-
// __asan_unpoison_memory_region((void *)addr, 64);
7-
// __asan_unpoison_memory_region((void *)addr, 128);
8-
__asan_unpoison_memory_region((void *)addr, 256);
9-
}
5+
6+
}

lab8/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
all:
2+
gcc -o chal -no-pie chal.c
3+
4+
run:
5+
./solve.py | ./chal
6+
7+
clean:
8+
rm chal

lab8/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Lab8
2+
3+
## Introduction
4+
5+
In this lab, you will write a angr script in `solve.py` to crack secret key in `chal.c` and get flag.
6+
7+
## Preparation (Important!!!)
8+
9+
1. Sync fork your branch (e.g., `SQLab:311XXXXXX`)
10+
2. `git checkout -b lab8` (**NOT** your student ID !!!)
11+
12+
## Requirement
13+
14+
(100%) Write a angr script and satisfy following requirements.
15+
1. Explore flag is in stdout or not by angr to solve secret key.
16+
2. Write secret key to stdout, and `validate.sh` pass it to `./chal` to check secret key.
17+
You can run `validate.sh` in your local to test if you satisfy the requirements.
18+
19+
Please note that you must not alter files other than `solve.py`. You will get 0 points if
20+
21+
1. you modify other files to achieve requirements.
22+
2. you can't pass all CI on your PR.
23+
24+
## Submission
25+
26+
You need to open a pull request to your branch (e.g. 311XXXXXX, your student number) and contain the code that satisfies the abovementioned requirements.
27+
28+
Moreover, please submit the URL of your PR to E3. Your submission will only be accepted when you present at both places.

lab8/ans

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
./solve.py | ./chal
2+
Enter the secret key: Correct! The flag is: CTF{symbolic_execution_for_the_win}

lab8/chal.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
5+
void gate(const char *input)
6+
{
7+
if (strlen(input) != 8) {
8+
return;
9+
}
10+
11+
if ((input[0] ^ input[1]) != 0x55) return;
12+
if ((input[2] + input[3]) != 200) return;
13+
if ((input[4] * 3) != input[5]) return;
14+
if ((input[6] - input[7]) != 1) return;
15+
16+
if ((input[1] + input[2] - input[3]) != 50) return;
17+
if ((input[5] ^ input[6]) != 0x2A) return;
18+
19+
puts("Correct! The flag is: CTF{symbolic_execution_for_the_win}");
20+
exit(0);
21+
}
22+
23+
int main()
24+
{
25+
char input[0x10] = {0};
26+
printf("Enter the secret key: ");
27+
fgets(input, sizeof(input), stdin);
28+
input[strcspn(input, "\n")] = 0; // Strip newline
29+
gate(input);
30+
puts("Wrong key!");
31+
return 0;
32+
}
33+

lab8/solve.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python3
2+
3+
import angr,sys
4+
5+
def main():
6+
secret_key = b""
7+
sys.stdout.buffer.write(secret_key)
8+
9+
10+
if __name__ == '__main__':
11+
main()

lab8/validate.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
# Check for unwanted files
4+
for file in *; do
5+
if [[ $file != "solve.py" && $file != "chal.c" && $file != "Makefile" && $file != "README.md" && $file != "validate.sh" && $file != "ans" ]]; then
6+
echo "[!] Unwanted file detected: $file."
7+
exit 1
8+
fi
9+
done
10+
11+
test_path="${BASH_SOURCE[0]}"
12+
solution_path="$(realpath .)"
13+
tmp_dir=$(mktemp -d -t lab8-XXXXXXXXXX)
14+
answer=""
15+
16+
cd $tmp_dir
17+
18+
rm -rf *
19+
cp $solution_path/Makefile .
20+
cp $solution_path/ans .
21+
cp $solution_path/*.c .
22+
cp $solution_path/*.py .
23+
24+
make
25+
make run > out
26+
result=$(diff --strip-trailing-cr ans out)
27+
if [[ -n $result ]]; then
28+
echo "[!] Expected: "
29+
cat ans
30+
echo ""
31+
echo "[!] Actual: "
32+
cat out
33+
echo ""
34+
exit 1
35+
else
36+
echo "[V] Pass"
37+
fi
38+
39+
rm -rf $tmp_dir
40+
41+
exit 0
42+
43+
# vim: set fenc=utf8 ff=unix et sw=2 ts=2 sts=2:

0 commit comments

Comments
 (0)