pyrograph 0.1.0

GPU-accelerated taint analysis for supply chain malware detection
Documentation
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#![cfg(feature = "python")]

use pyrograph::{analyze, parse::parse_python};

fn assert_detects(name: &str, source: &str) {
    let graph = parse_python(source, &format!("{name}.py")).unwrap();
    let findings = analyze(&graph).unwrap();
    assert!(!findings.is_empty(), "expected finding for {name}");
}

fn assert_clean(name: &str, source: &str) {
    let graph = parse_python(source, &format!("{name}.py")).unwrap();
    let findings = analyze(&graph).unwrap();
    assert!(findings.is_empty(), "unexpected finding for {name}: {findings:?}");
}

#[test]
fn tp_01_setup_os_system_environ() {
    assert_detects("tp_01_setup_os_system_environ", r#"
import os
from setuptools import setup

def malicious_setup():
    cmd = os.environ.get("MALICIOUS_CMD")
    if cmd:
        os.system(cmd)

malicious_setup()
setup(name="innocent_pkg")
"#);
}

#[test]
fn tp_02_setup_subprocess_popen_sys_argv() {
    assert_detects("tp_02_setup_subprocess_popen_sys_argv", r#"
import sys
import subprocess
from setuptools import setup

payload = sys.argv[1]
subprocess.Popen(["bash", "-c", payload])

setup(name="bad_pkg")
"#);
}

#[test]
fn tp_03_setup_subprocess_run_open_read() {
    assert_detects("tp_03_setup_subprocess_run_open_read", r#"
import subprocess
from setuptools import setup

with open(".hidden_payload") as f:
    code = f.read()

subprocess.run(code, shell=True)
setup(name="test_pkg")
"#);
}

#[test]
fn tp_04_setup_os_system_input() {
    assert_detects("tp_04_setup_os_system_input", r#"
import os
from setuptools import setup

user_cmd = input("Enter command: ")
os.system(user_cmd)
setup(name="test_pkg")
"#);
}

#[test]
fn tp_05_setup_subprocess_call_request_form() {
    assert_detects("tp_05_setup_subprocess_call_request_form", r#"
from flask import request
import subprocess
from setuptools import setup

cmd = request.form['cmd']
subprocess.call(cmd, shell=True)
setup(name="test_pkg")
"#);
}

#[test]
fn tp_06_setup_subprocess_check_output_getenv() {
    assert_detects("tp_06_setup_subprocess_check_output_getenv", r#"
import os
import subprocess
from setuptools import setup

payload = os.getenv("PAYLOAD")
subprocess.check_output(payload, shell=True)
setup(name="test_pkg")
"#);
}

#[test]
fn tp_07_setup_os_popen_pathlib() {
    assert_detects("tp_07_setup_os_popen_pathlib", r#"
import os
import pathlib
from setuptools import setup

payload = pathlib.Path("payload.sh").read_text()
os.popen(payload)
setup(name="test_pkg")
"#);
}

#[test]
fn tp_08_setup_subprocess_run_request_args() {
    assert_detects("tp_08_setup_subprocess_run_request_args", r#"
import subprocess
from flask import request
from setuptools import setup

subprocess.run(request.args['cmd'], shell=True)
setup(name="test_pkg")
"#);
}

#[test]
fn tp_09_setup_exec_base64_request_json() {
    assert_detects("tp_09_setup_exec_base64_request_json", r#"
from flask import request
import base64
from setuptools import setup

data = request.json['payload']
decoded = base64.b64decode(data)
exec(decoded)

setup(name="test_pkg")
"#);
}

#[test]
fn tp_10_setup_eval_open_read() {
    assert_detects("tp_10_setup_eval_open_read", r#"
from setuptools import setup

with open("config.txt") as f:
    config = f.read()

eval(config)
setup(name="test_pkg")
"#);
}

#[test]
fn tp_11_setup_compile_request_args() {
    assert_detects("tp_11_setup_compile_request_args", r#"
from flask import request
from setuptools import setup

src = request.args.get("src")
compiled = compile(src, "<string>", "exec")
exec(compiled)
setup(name="test_pkg")
"#);
}

#[test]
fn tp_12_setup_exec_pathlib_read() {
    assert_detects("tp_12_setup_exec_pathlib_read", r#"
import pathlib
from setuptools import setup

code = pathlib.Path("payload.py").read_text()
exec(code)
setup(name="test_pkg")
"#);
}

#[test]
fn tp_13_setup_eval_sys_argv() {
    assert_detects("tp_13_setup_eval_sys_argv", r#"
import sys
from setuptools import setup

expr = sys.argv[2]
eval(expr)
setup(name="test_pkg")
"#);
}

#[test]
fn tp_14_setup_exec_input() {
    assert_detects("tp_14_setup_exec_input", r#"
from setuptools import setup

cmd = input("Paste code here: ")
exec(cmd)
setup(name="test_pkg")
"#);
}

#[test]
fn tp_15_setup_socket_connect_environ() {
    assert_detects("tp_15_setup_socket_connect_environ", r#"
import os
import socket
from setuptools import setup

c2_ip = os.environ.get("C2_HOST")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((c2_ip, 1337))

setup(name="test_pkg")
"#);
}

#[test]
fn tp_16_setup_socket_connect_sys_argv() {
    assert_detects("tp_16_setup_socket_connect_sys_argv", r#"
import sys
import socket
from setuptools import setup

host = sys.argv[1]
s = socket.socket()
s.connect((host, 80))

setup(name="test_pkg")
"#);
}

#[test]
fn tp_17_setup_pickle_loads_open_read() {
    assert_detects("tp_17_setup_pickle_loads_open_read", r#"
import pickle
from setuptools import setup

data = open("payload.pkl", "rb").read()
pickle.loads(data)

setup(name="test_pkg")
"#);
}

#[test]
fn tp_18_setup_pickle_loads_request_form() {
    assert_detects("tp_18_setup_pickle_loads_request_form", r#"
import pickle
from flask import request
from setuptools import setup

blob = request.form['data']
pickle.loads(blob)

setup(name="test_pkg")
"#);
}

#[test]
fn tp_19_setup_requests_get_environ() {
    assert_detects("tp_19_setup_requests_get_environ", r#"
import os
import requests
from setuptools import setup

exfil = os.environ.get("WEBHOOK_URL")
requests.get(exfil)

setup(name="test_pkg")
"#);
}

#[test]
fn tp_20_setup_urllib_request_json() {
    assert_detects("tp_20_setup_urllib_request_json", r#"
from flask import request
import urllib.request
from setuptools import setup

url = request.json['webhook']
urllib.request.urlopen(url)

setup(name="test_pkg")
"#);
}

#[test]
fn fp_01_setup_hardcoded_os_system() {
    assert_clean("fp_01_setup_hardcoded_os_system", r#"
import os
from setuptools import setup

os.system("echo 'building module'")
setup(name="benign_pkg")
"#);
}

#[test]
fn fp_02_setup_hardcoded_subprocess_run() {
    assert_clean("fp_02_setup_hardcoded_subprocess_run", r#"
import subprocess
from setuptools import setup

subprocess.run(["gcc", "-c", "math.c"])
setup(name="benign_pkg")
"#);
}

#[test]
fn fp_03_setup_hardcoded_exec() {
    assert_clean("fp_03_setup_hardcoded_exec", r#"
from setuptools import setup

exec("x = 10")
setup(name="benign_pkg")
"#);
}

#[test]
fn fp_04_setup_disconnected_flow() {
    assert_clean("fp_04_setup_disconnected_flow", r#"
import os
import sys
from setuptools import setup

source = sys.argv[1]
cmd = "ls -la"
os.system(cmd)

setup(name="benign_pkg")
"#);
}

#[test]
fn fp_05_setup_hardcoded_socket() {
    assert_clean("fp_05_setup_hardcoded_socket", r#"
import socket
from setuptools import setup

s = socket.socket()
s.connect(("127.0.0.1", 8080))
setup(name="benign_pkg")
"#);
}

#[test]
fn fp_06_setup_hardcoded_requests() {
    assert_clean("fp_06_setup_hardcoded_requests", r#"
import requests
from setuptools import setup

requests.get("https://api.github.com")
setup(name="benign_pkg")
"#);
}

#[test]
fn fp_07_setup_hardcoded_pickle() {
    assert_clean("fp_07_setup_hardcoded_pickle", r#"
import pickle
from setuptools import setup

pickle.loads(b"\x80\x04\x95\x11\x00\x00\x00\x00\x00\x00\x00}\x94\x8c\x04test\x94\x8c\x04data\x94s.")
setup(name="benign_pkg")
"#);
}

#[test]
fn fp_08_setup_sanitized_int() {
    assert_clean("fp_08_setup_sanitized_int", r#"
import sys
import os
from setuptools import setup

val = int(sys.argv[1])
os.system(val)
setup(name="benign_pkg")
"#);
}

#[test]
fn fp_09_setup_sanitized_float() {
    assert_clean("fp_09_setup_sanitized_float", r#"
from flask import request
import subprocess
from setuptools import setup

timeout = float(request.args['timeout'])
subprocess.run(timeout)
setup(name="benign_pkg")
"#);
}

#[test]
fn fp_10_setup_sanitized_str() {
    assert_clean("fp_10_setup_sanitized_str", r#"
import os
import requests
from setuptools import setup

port = str(os.environ.get("PORT", "8080"))
requests.get(port)
setup(name="benign_pkg")
"#);
}

#[test]
fn fp_11_setup_sanitized_shlex_quote() {
    assert_clean("fp_11_setup_sanitized_shlex_quote", r#"
import sys
import os
import shlex
from setuptools import setup

user_input = sys.argv[2]
os.system(shlex.quote(user_input))
setup(name="benign_pkg")
"#);
}

#[test]
fn fp_12_setup_sanitized_basename() {
    assert_clean("fp_12_setup_sanitized_basename", r#"
from flask import request
import os
from setuptools import setup

path = request.form['file']
os.popen(os.path.basename(path))
setup(name="benign_pkg")
"#);
}

// TRUE POSITIVE: passing os.environ to subprocess.Popen is suspicious in a setup.py
// context because it leaks the full environment (potentially containing secrets) into
// a spawned process. This is expected behavior — do not weaken detection.
#[test]
fn tp_21_setup_popen_with_os_environ_is_flagged() {
    assert_detects("tp_21_setup_popen_with_os_environ", r#"
import os
import subprocess
from setuptools import setup

subprocess.Popen("ls", env=os.environ)
setup(name="leaky_pkg")
"#);
}

#[test]
fn fp_13_setup_sanitized_bleach_clean() {
    assert_clean("fp_13_setup_sanitized_bleach_clean", r#"
import bleach
import sys
from setuptools import setup

data = sys.argv[1]
exec(bleach.clean(data))
setup(name="benign_pkg")
"#);
}