Skip to content

Commit

Permalink
feat: merge lab5
Browse files Browse the repository at this point in the history
  • Loading branch information
YingMuo committed Apr 18, 2024
1 parent a996b7c commit 6400b30
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/lab-autograding.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ jobs:
const files = await github.rest.pulls.listFiles({ owner, repo, pull_number: issue_number });
const changedFiles = files.data.map((file) => file.filename);
const allowedFileRegex = /^lab\d+\/main_test.js$/;
if (!changedFiles.every((file) => allowedFileRegex.test(file))) {
const specialChangedFiles = ["lab5/Answer.md", "lab5/antiasan.c"];
if (!changedFiles.every((file) => (allowedFileRegex.test(file) || specialChangedFiles.includes(file)))) {
core.setFailed('The PR contains changes to files other than the allowed files.');
}
return labNumber;
Expand Down
92 changes: 92 additions & 0 deletions lab5/Answer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Answer

Name:
ID:

## Test Valgrind and ASan
### Result
| | Valgrind | Asan |
| -------------------- | -------- | ---- |
| Heap out-of-bounds | | |
| Stack out-of-bounds | | |
| Global out-of-bounds | | |
| Use-after-free | | |
| Use-after-return | | |

### Heap out-of-bounds
#### Source code
```
```
#### Valgrind Report
```
```
### ASan Report
```
```

### Stack out-of-bounds
#### Source code
```
```
#### Valgrind Report
```
```
### ASan Report
```
```

### Global out-of-bounds
#### Source code
```
```
#### Valgrind Report
```
```
### ASan Report
```
```

### Use-after-free
#### Source code
```
```
#### Valgrind Report
```
```
### ASan Report
```
```

### Use-after-return
#### Source code
```
```
#### Valgrind Report
```
```
### ASan Report
```
```

## ASan Out-of-bound Write bypass Redzone
### Source code
```
```
### Why

17 changes: 17 additions & 0 deletions lab5/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.PHONY: all
all: uaf_asan

uaf_asan: uaf.c libantiasan.so
gcc -fsanitize=address -Og -g -o $@ $< -lantiasan -L.

libantiasan.so: antiasan.c
gcc -g -fPIC -c antiasan.c
gcc -shared antiasan.o -o libantiasan.so

.PHINY: run
run:
LD_LIBRARY_PATH=. ./uaf_asan

.PHONY: clean
clean:
rm uaf_asan antiasan.o libantiasan.so
29 changes: 29 additions & 0 deletions lab5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Lab5

## Introduction

In this lab, you will write a function antoasan to bypass detection of ASan in `antiasan.c` and answer questions of slide in `Answer.md`.

## Preparation (Important!!!)

1. Sync fork your branch (e.g., `SQLab:311XXXXXX`)
2. `git checkout -b lab5` (**NOT** your student ID !!!)

## Requirement

1. (50%) Test Valgrind and ASan to detect common memory corruption vulns, and then asnwer result, report of Valgrind/ASan and Vulnerable code in `Answer.md`.
2. (40%) Write a vulnerable code to bypass redzone between 2 int [8] arrays and asnwer reason and code in `Answer.md`.

3. (30%) write a function antoasan to bypass detection of ASan in `antiasan.c`.
You can run `validate.sh` in your local to test if you satisfy the requirements.

Please note that you must not alter files other than `antiasan.c` and `Answer.md`. You will get 0 points if

1. you modify other files to achieve requirements.
2. you can't pass all CI on your PR.

## Submission

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.

Moreover, please submit the URL of your PR to E3. Your submission will only be accepted when you present at both places.
3 changes: 3 additions & 0 deletions lab5/ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
LD_LIBRARY_PATH=. ./uaf_asan
s[0x10] = H
s[0x10] = H
5 changes: 5 additions & 0 deletions lab5/antiasan.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// TODO:
void antiasan(unsigned long addr)
{

}
6 changes: 6 additions & 0 deletions lab5/antiasan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef HIJACK_H
#define HIJACK_H

void antiasan(unsigned long);

#endif
15 changes: 15 additions & 0 deletions lab5/uaf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "antiasan.h"

int main(void)
{
char *s = (char *)malloc(0x18);
strcpy(s, "HAHAHAHAHAHAHAHAHAHAHAH");
printf("s[0x10] = %c\n", s[0x10]);
free(s);
antiasan((unsigned long)&s[0x10]);
printf("s[0x10] = %c\n", s[0x10]);
return 0;
}
43 changes: 43 additions & 0 deletions lab5/validate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Check for unwanted files
for file in *; do
if [[ $file != "uaf.c" && $file != "antiasan.c" && $file != "antiasan.h" && $file != "Makefile" && $file != "README.md" && $file != "Answer.md" && $file != "validate.sh" && $file != "ans" ]]; then
echo "[!] Unwanted file detected: $file."
exit 1
fi
done

test_path="${BASH_SOURCE[0]}"
solution_path="$(realpath .)"
tmp_dir=$(mktemp -d -t lab5-XXXXXXXXXX)
answer=""

cd $tmp_dir

rm -rf *
cp $solution_path/Makefile .
cp $solution_path/*.c .
cp $solution_path/*.h .
cp $solution_path/ans .

make
make run > out 2>&1
result=$(diff ans out)
if [[ -n $result ]]; then
echo "[!] Expected: "
cat ans
echo ""
echo "[!] Actual: "
cat out
echo ""
exit 1
else
echo "[V] Pass"
fi

rm -rf $tmp_dir

exit 0

# vim: set fenc=utf8 ff=unix et sw=2 ts=2 sts=2:

0 comments on commit 6400b30

Please sign in to comment.