generated from C4T-BuT-S4D/ad-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample_lib.py
283 lines (233 loc) · 9.1 KB
/
example_lib.py
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import requests
from checklib import *
from uuid import uuid4
from dataclasses import dataclass
from collections import defaultdict
from enum import Enum, auto, unique
from random import randint, choices
from base64 import b64encode
import re
uuid_regex = re.compile(
r"^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$")
@unique
class OPCODE(Enum):
def _generate_next_value_(name, start, count, last_values):
return name
def __str__(self):
return self.name
OP_PUSH = auto()
OP_POP = auto()
OP_DUP = auto()
OP_SWAP = auto()
OP_HIDE = auto()
OP_CALL = auto()
OP_INVOKE = auto()
OP_RESET = auto()
OP_JMP = auto()
OP_JMPIF = auto()
OP_JMPNIF = auto()
OP_REPORT = auto()
OP_ADD = auto()
OP_SUB = auto()
OP_HLTCHK = auto()
OP_HLTNCHK = auto()
class VM:
def __init__(self):
self.opcodes = []
def push(self, *opcode):
self.opcodes.append(list(opcode))
def serialize(self) -> list:
return [[str(opcode[0])] + opcode[1:] for opcode in self.opcodes]
@dataclass
class ExecutionResult:
vm_id: str
context: defaultdict(int)
class CheckMachine:
@property
def url(self):
return f'http://{self.c.host}:{self.port}/api'
def __init__(self, checker: BaseChecker):
self.c = checker
self.port = 5678
def get_access_key(self) -> str:
return uuid4()
def execute(self, session: requests.Session, opcodes: list, access_key: str, report: str, status: Status) -> ExecutionResult:
response = session.post(f'{self.url}/execute', params={
"accessKey": access_key
}, json={
"opcodes": opcodes,
"report": report
})
data = self.c.get_json(
response, "Invalid response on /execute", status)
self.c.assert_eq(type(data), dict,
"Invalid response type on /execute", status)
self.c.assert_in("ok", data, "No ok field on /execute", status)
self.c.assert_eq(data["ok"], True, "Not ok on /execute", status)
self.c.assert_in("result", data, "No result field on /execute", status)
self.c.assert_eq(type(data["result"]), dict,
"Invalid result type on /execute", status)
self.c.assert_in("vmId", data["result"],
"No vmId field on /execute", status)
self.c.assert_eq(type(data["result"]["vmId"]),
str, "Invalid vmId type on /execute", status)
self.c.assert_in(
"context", data["result"], "No context field on /execute", status)
self.c.assert_eq(type(data["result"]["context"]),
dict, "Invalid context type on /execute", status)
self.c.assert_in(
"CALLS", data["result"]["context"], "No CALLS field on /execute", status)
self.c.assert_eq(type(data["result"]["context"]["CALLS"]),
dict, "Invalid CALLS type on /execute", status)
for fn in data["result"]["context"]["CALLS"]:
self.c.assert_eq(
type(fn), str, "Invalid CALLS key type on /execute", status)
self.c.assert_eq(type(
data["result"]["context"]["CALLS"][fn]), int, "Invalid CALLS value on /execute", status)
self.c.assert_eq(bool(uuid_regex.fullmatch(
data["result"]["vmId"])), True, "Invalid vmId field on /execute", status)
return ExecutionResult(data["result"]["vmId"], data["result"]["context"])
def get_report(self, session: requests.Session, access_key: str, vm_id: str, status: Status) -> str:
response = session.get(f'{self.url}/getReport', params={
"accessKey": access_key,
"vmId": vm_id
})
data = self.c.get_json(
response, "Invalid response on /getReport", status)
self.c.assert_eq(type(data), dict,
"Invalid response type on /getReport", status)
self.c.assert_in("ok", data, "No ok field on /getReport", status)
self.c.assert_eq(data["ok"], True, "Not ok on /getReport", status)
self.c.assert_in(
"result", data, "No result field on /getReport", status)
self.c.assert_eq(type(data["result"]), str,
"Invalid result type on /getReport", status)
self.c.assert_eq(len(data["result"]) <= 1024,
True, "invalid result length on /getReport")
return data["result"]
def generate_random_vm(self) -> (dict, dict, str):
r = randint(0, 2)
if r == 0:
ops = choices([self.random_op0, self.random_op1, self.random_op2,
self.random_op3, self.random_op4], k=randint(20, 40))
vm = VM()
s = ""
context = defaultdict(int)
vm.push(OPCODE.OP_RESET)
vm.push(OPCODE.OP_PUSH, "")
for op in ops:
ctx, ss = op(vm)
s += ss
for k in ctx:
context[k] += ctx[k]
vm.push(OPCODE.OP_SWAP)
vm.push(OPCODE.OP_ADD)
vm.push(OPCODE.OP_REPORT)
return vm.serialize(), {'http.request': 2, 'JSON.stringify': 1, **context}, s
elif r == 1:
vm = VM()
s = rnd_string(100)
vm.push(OPCODE.OP_PUSH, s)
vm.push(OPCODE.OP_PUSH, 0)
vm.push(OPCODE.OP_DUP)
vm.push(OPCODE.OP_HIDE)
vm.push(OPCODE.OP_HLTNCHK)
vm.push(OPCODE.OP_SWAP)
vm.push(OPCODE.OP_PUSH, 1)
vm.push(OPCODE.OP_ADD)
vm.push(OPCODE.OP_JMP, 6)
return vm.serialize(), {'http.request': 2, 'JSON.stringify': 1}, s
else:
vm = VM()
s = rnd_string(3)
vm.push(OPCODE.OP_PUSH, s)
vm.push(OPCODE.OP_PUSH, 3)
vm.push(OPCODE.OP_PUSH, -1)
vm.push(OPCODE.OP_ADD)
vm.push(OPCODE.OP_SWAP)
vm.push(OPCODE.OP_DUP)
vm.push(OPCODE.OP_ADD)
vm.push(OPCODE.OP_SWAP)
vm.push(OPCODE.OP_DUP)
vm.push(OPCODE.OP_JMPNIF, 7)
vm.push(OPCODE.OP_POP)
vm.push(OPCODE.OP_REPORT)
return vm.serialize(), {'http.request': 2, 'JSON.stringify': 1}, s * 8
def random_op0(self, vm: VM) -> (dict, str):
s = rnd_string(10)
vm.push(OPCODE.OP_PUSH, s)
vm.push(OPCODE.OP_DUP)
vm.push(OPCODE.OP_POP)
return {}, s
def random_op1(self, vm: VM) -> (dict, str):
s = rnd_string(6)
vm.push(OPCODE.OP_PUSH, 'ascii')
vm.push(OPCODE.OP_PUSH, 1)
vm.push(OPCODE.OP_PUSH, 'base64')
vm.push(OPCODE.OP_PUSH, b64encode(s.encode()).decode())
vm.push(OPCODE.OP_PUSH, 2)
vm.push(OPCODE.OP_CALL, 'context_Buffer_from')
vm.push(OPCODE.OP_INVOKE, 'toString')
return {'Buffer.from': 1}, s
def random_op2(self, vm: VM) -> (dict, str):
s1 = rnd_string(3)
s2 = rnd_string(3)
s3 = rnd_string(3)
vm.push(OPCODE.OP_PUSH, s1)
vm.push(OPCODE.OP_PUSH, s2)
vm.push(OPCODE.OP_PUSH, s3)
vm.push(OPCODE.OP_HIDE)
vm.push(OPCODE.OP_ADD)
vm.push(OPCODE.OP_ADD)
return {}, s2 + s1 + s3
def random_op3(self, vm: VM) -> (dict, str):
a = randint(-100, 100)
b = randint(-100, 100)
vm.push(OPCODE.OP_PUSH, a)
vm.push(OPCODE.OP_PUSH, b)
vm.push(OPCODE.OP_SUB)
return {}, str(b - a)
def random_op4(self, vm: VM) -> (dict, str):
a = randint(1, 4)
vm.push(OPCODE.OP_PUSH, 0)
vm.push(OPCODE.OP_CALL, 'context_Set_constructor')
for i in range(a):
vm.push(OPCODE.OP_PUSH, 1)
vm.push(OPCODE.OP_SWAP)
vm.push(OPCODE.OP_PUSH, i)
vm.push(OPCODE.OP_HIDE)
vm.push(OPCODE.OP_INVOKE, 'add')
vm.push(OPCODE.OP_PUSH, 1)
vm.push(OPCODE.OP_CALL, 'context_Uint8Array_constructor')
return {'Set.constructor': 1, 'Uint8Array.constructor': 1}, ",".join(map(str, range(a)))
def generate_flag_vm(self, flag) -> (dict, str):
vm = VM()
s = rnd_string(100)
vm.push(OPCODE.OP_PUSH, 1)
vm.push(OPCODE.OP_CALL, "context_Buffer_from")
vm.push(OPCODE.OP_PUSH, 1)
vm.push(OPCODE.OP_PUSH, s)
vm.push(OPCODE.OP_PUSH, 1)
vm.push(OPCODE.OP_CALL, "context_Buffer_from")
vm.push(OPCODE.OP_INVOKE, "equals")
vm.push(OPCODE.OP_PUSH, "error")
vm.push(OPCODE.OP_SWAP)
vm.push(OPCODE.OP_HLTCHK)
vm.push(OPCODE.OP_RESET)
lf, rf = self.get_2_parts(flag)
vm.push(OPCODE.OP_PUSH, rf)
vm.push(OPCODE.OP_PUSH, lf)
vm.push(OPCODE.OP_ADD)
vm.push(OPCODE.OP_REPORT)
return vm.serialize(), s
def get_2_parts(self, s) -> (str, str):
i = randint(1, len(s) - 1)
return s[:i], s[i:]
def get_substring(self, s) -> (int, int, str):
l = randint(0, len(s))
r = l
while r == l:
r = randint(0, len(s))
if l > r:
l, r = r, l
return l, r, s[l:r]