Skip to content

Commit 40fd394

Browse files
authored
Improve code execution and shady links rules (#463)
1 parent 217a20e commit 40fd394

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

guarddog/analyzer/sourcecode/code-execution.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ rules:
4141
- pattern: subprocess.run([..., "... $ARG1 ...", ...], ...)
4242
- pattern: run([..., "$ARG1", ...], ...)
4343

44+
- pattern: subprocess.check_call($ARG1, ...)
45+
- pattern: check_call($ARG1, ...)
46+
- pattern: subprocess.check_call([..., "... $ARG1 ...", ...], ...)
47+
- pattern: check_call([..., "$ARG1", ...], ...)
48+
4449
# eval, allow checking for version
4550
- patterns:
4651
- pattern-either:
@@ -74,6 +79,10 @@ rules:
7479
- pattern: popen($ARG1, ...)
7580
- pattern: popen([..., $ARG1, ...], ...)
7681

82+
# distutils spawn
83+
- pattern: spawn($ARG1, ...)
84+
- pattern: $OBJ.spawn($ARG1, ...)
85+
7786
# miscellaneous
7887
- pattern: os.system($ARG1, ...)
7988
- pattern: system($ARG1, ...)

guarddog/analyzer/sourcecode/shady-links.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@ rules:
1515
- pattern-not-regex: ^\s*"""(.|\n)*?"""\s*$
1616

1717
# Exclude local IPv4 sometimes used in tests
18-
- pattern-not-regex: (https?:\/\/[^\n\[\/\?#"']*?(?:192\.168|10\.\d{1,3}|172\.(?:1[6-9]|2\d|3[0-1])|127\.\d{1,3})\.\d{1,3}\.\d{1,3}|0\.0\.0\.0|localhost)
18+
- pattern-not-regex: ((?:https?:\/\/)?[^\n\[\/\?#"']*?(?:192\.168|10\.\d{1,3}|172\.(?:1[6-9]|2\d|3[0-1])|127\.\d{1,3})\.\d{1,3}\.\d{1,3}|0\.0\.0\.0|localhost)
1919

2020
# Exclude public IPv4 sometimes used in tests
21-
- pattern-not-regex: (https?:\/\/[^\n\[\/\?#"']*?(?:1\.1\.1\.1|8\.8\.8\.8))
21+
- pattern-not-regex: ((?:https?:\/\/)?[^\n\[\/\?#"']*?(?:1\.1\.1\.1|8\.8\.8\.8))
22+
23+
# Exclude cloud provider metadata service IPs
24+
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
25+
- pattern-not-regex: ((?:https?:\/\/)?[^\n\[\/\?#"']*?(?:169\.254\.\d{1,3}\.\d{1,3}|\[fd00:ec2::254\]))
2226

2327
# ignore discord allowed
24-
- pattern-not-regex: https?:\/\/discord.com\/(invite|oauth2\/authorize)
28+
- pattern-not-regex: (?:https?:\/\/)?discord.com\/(invite|oauth2\/authorize)
2529

2630
- patterns:
2731
- pattern: ("...")
2832
- pattern-either:
2933
# complete domains
30-
- pattern-regex: (https?:\/\/[^\n\[\/\?#"']*?(bit\.ly|discord\.com|workers\.dev|transfer\.sh|filetransfer\.io|sendspace\.com|appdomain\.cloud|backblazeb2\.com\|paste\.ee|ngrok\.io|termbin\.com|localhost\.run|webhook\.site|oastify\.com|burpcollaborator\.me)\/)
31-
- pattern-regex: (http[s]?:\/\/[^\n\[\/\?#"']*?(oast\.(pro|live|site|online|fun|me)|api\.telegram\.org|rentry\.co)\/)
34+
- pattern-regex: ((?:https?:\/\/)?[^\n\[\/\?#"']*?(bit\.ly|discord\.com|workers\.dev|transfer\.sh|filetransfer\.io|sendspace\.com|appdomain\.cloud|backblazeb2\.com\|paste\.ee|ngrok\.io|termbin\.com|localhost\.run|webhook\.site|oastify\.com|burpcollaborator\.me)\/)
35+
- pattern-regex: ((?:https?:\/\/)?[^\n\[\/\?#"']*?(oast\.(pro|live|site|online|fun|me)|api\.telegram\.org|rentry\.co)\/)
3236
# top-level domains
3337
- pattern-regex: (https?:\/\/[^\n\[\/\?#"']*?\.(link|xyz|tk|ml|ga|cf|gq|pw|top|club|mw|bd|ke|am|sbs|date|quest|cd|bid|cd|ws|icu|cam|uno|email|stream)\/)
3438
# IPv4

tests/analyzer/sourcecode/code-execution.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,19 @@ def run_file(path):
196196
# ruleid: code-execution
197197
p = subprocess.Popen(f"python {path}",shell=True,stdin=None,stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True)
198198
out, err = p.communicate()
199+
200+
# ruleid: code-execution
201+
subprocess.check_call(["rm", "-rf", "target_dir"], cwd="/tmp")
202+
203+
# ok: code-execution
204+
subprocess.check_call(["cmake", "--build", "."])
205+
206+
class install_ext_solibs(install_lib):
207+
def run(self):
208+
super().run()
209+
for wheel in glob.glob("build/bdist.*/wheel"):
210+
for solib in os.listdir(wheel):
211+
for mext in re.finditer("^([^/]*).cpython.*", solib):
212+
if not mext.group(1) in templates:
213+
# ruleid: code-execution
214+
self.spawn(["rm", "-f", f"{wheel}/{solib}"])

tests/analyzer/sourcecode/shady-links.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,6 @@ def f():
173173
# ok: shady-links
174174
trackingServiceUrl = 'https//discord.com/invite/u9zUjWbbQ'
175175

176+
def f():
177+
# ruleid: shady-links
178+
trackingUrl = "qkrfaniquihoswritqaqwbg5r4l072qp7.oast.fun/track"

0 commit comments

Comments
 (0)