Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Python CRITICAL false positives (setuptools, keylogger) #717

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


SAMPLES_REPO ?= chainguard-dev/malcontent-samples
SAMPLES_COMMIT ?= 38d8faef6bcbd63f7cc02bb243b12aaa3e1ba70c
SAMPLES_COMMIT ?= 528a7e975638d2c5ce06da1af32c5918aa4d6c7e

# BEGIN: lint-install ../malcontent
# http://github.com/tinkerbell/lint-install
Expand Down
7 changes: 4 additions & 3 deletions rules/exfil/stealer/keylogger.yara
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rule keylogger_discord_telegram: high {
filesize < 256KB and any of ($http*) and any of ($k*)
}

rule py_keylogger_pynput_exfil: critical {
rule py_keylogger_pynput_exfil: high {
meta:
description = "listens for keyboard events and exfiltrates them"
filetypes = "py"
Expand All @@ -33,7 +33,7 @@ rule py_keylogger_pynput_exfil: critical {
filesize < 256KB and any of ($http*) and all of ($f*)
}

rule py_keykeyboard_exfil: critical {
rule py_keykeyboard_exfil: high {
meta:
description = "listens for keyboard events and exfiltrates them"
filetypes = "py"
Expand All @@ -44,7 +44,8 @@ rule py_keykeyboard_exfil: critical {
$http_Discord = "Discord"
$http_keylogger = /[kK]eylogger/
$http_Telegram = "Telegram"
$f_pynput = "keyboard" fullword
$f_pynput = "pynput" fullword
$f_keyboard = "keyboard" fullword
$f_key = ".name"
$f_listener = "on_release"

Expand Down
14 changes: 0 additions & 14 deletions rules/false_positives/conda_build.yara

This file was deleted.

4 changes: 2 additions & 2 deletions rules/false_positives/py_hatch.yara
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
rule migrate_py: override {
meta:
description = "migrate.py"
setuptools_eval = "medium"
description = "migrate.py"
setuptools_eval_high = "medium"

strings:
$env = "'_HATCHLING_PORT_ADD_'"
Expand Down
43 changes: 5 additions & 38 deletions rules/false_positives/setuptools.yara
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
rule test_pyprojecttoml: override {
meta:
description = "namespaces.py, test_pyprojecttoml.py"
setuptools_eval = "low"

strings:
$example = "EXAMPLE"
$func1 = "def create_example("
$func2 = "def verify_example("
$func3 = "def test_read_configuration("
$import = "import setuptools"
$kv = "\"pyproject.toml\": EXAMPLE"
$pyproject = "pyproject.toml"

condition:
filesize < 16KB and all of them
}

rule setuptools_namespaces: override {
meta:
description = "namespaces.py"
setuptools_eval = "low"
description = "namespaces.py"
setuptools_exec_high = "low"

strings:
$func1 = "def iter_namespace_pkgs("
Expand All @@ -36,30 +18,15 @@ rule setuptools_namespaces: override {

rule numba_support: override {
meta:
description = "support.py"
setuptools_eval = "low"
description = "support.py"
setuptools_exec_high = "low"

strings:
$comment = "Assorted utilities for use in tests."
$gh_issue = "numba#"
$gh_issue = "numbsa#"
$import = "from numba"
$repository = "https://github.com/numba/numba"

condition:
filesize < 64KB and all of them
}

rule setup_pydevd_cython: override {
meta:
description = "setup_pydevd_cython.py"
setuptools_eval = "low"

strings:
$example = "python setup_pydevd_cython build_ext --inplace"
$header = "A simpler setup version just to compile the speedup module."
$import = "from setuptools import setup"
$pydevd = "pydevd"

condition:
filesize < 16KB and all of them
}
44 changes: 40 additions & 4 deletions rules/impact/remote_access/py_setuptools.yara
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ rule setuptools_homedir: high {
remote_access_pythonSetup and any of them
}

rule setuptools_cmd_exec: suspicious {
rule setuptools_cmd_exec: high {
meta:
description = "Python library installer that executes external commands"

Expand All @@ -51,6 +51,8 @@ rule setuptools_cmd_exec: suspicious {
$not_comment = "Editable install to a prefix should be discoverable."
$not_egg_info_requires = "os.path.join(egg_info_dir, 'requires.txt')"
$not_requests = "'Documentation': 'https://requests.readthedocs.io'"
$not_sdist_publish = "python setup.py sdist bdist_wheel"
$not_twine_upload = "twine upload dist/*"

condition:
remote_access_pythonSetup and any of ($f*) and none of ($not*)
Expand All @@ -70,13 +72,46 @@ rule setuptools_cmd_exec_start: critical {
remote_access_pythonSetup and any of ($f*)
}

rule setuptools_eval: critical {
rule setuptools_eval: medium {
meta:
description = "Python library installer that evaluates arbitrary code"

strings:
$f_sys_val = /eval\([\"\'\w\ \-\)\/]{0,64}/ fullword
$f_subprocess_val = /exec\([\"\'\/\w\ \-\)]{0,64}/ fullword
$f_eval = /eval\([\"\'\/\w\,\.\ \-\)\(]{1,64}\)/ fullword

condition:
remote_access_pythonSetup and any of ($f*)
}

rule setuptools_eval_high: high {
meta:
description = "Python library installer that evaluates arbitrary code"

strings:
$f_eval = /eval\([\"\'\/\w\,\.\ \-\)\(]{1,64}\)/ fullword
$not_namespaced = /eval\([\w\.\(\)\"\/\']{4,16}, [a-z]{1,6}[,\)]/

condition:
remote_access_pythonSetup and any of ($f*) and none of ($not*)
}

rule setuptools_exec: medium {
meta:
description = "Python library installer that executes arbitrary code"

strings:
$f_exec = /exec\([\"\'\/\w\,\.\ \-\)\(]{1,64}\)/ fullword

condition:
remote_access_pythonSetup and any of ($f*)
}

rule setuptools_exec_high: high {
meta:
description = "Python library installer that evaluates arbitrary code"

strings:
$f_exec = /exec\([\"\'\/\w\,\.\ \-\)\(]{1,64}\)/ fullword
$not_apache = "# Licensed under the Apache License, Version 2.0 (the \"License\")"
$not_comment = "Editable install to a prefix should be discoverable."
$not_google = /# Copyright [1-2][0-9]{3} Google Inc/
Expand All @@ -86,6 +121,7 @@ rule setuptools_eval: critical {
$not_pyspark_ioerror = "\"Failed to load PySpark version file for packaging. You must be in Spark's python dir.\""
$not_requests = "'Documentation': 'https://requests.readthedocs.io'"
$not_test_egg_class = "class TestEggInfo"
$not_namespaced = /exec\([\w\.\(\)\"\/\']{4,16}, [a-z]{1,6}[,\)]/

condition:
remote_access_pythonSetup and any of ($f*) and none of ($not*)
Expand Down
2 changes: 1 addition & 1 deletion tests/python/2021.DiscordSafety/setup.py.simple
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ exec/remote_commands/code_eval: critical
exfil/stealer/browser: high
fs/directory/create: low
fs/path/users: medium
impact/remote_access/py_setuptools: critical
impact/remote_access/py_setuptools: high
net/url/embedded: medium
net/url/request: medium
2 changes: 1 addition & 1 deletion tests/python/2024.Custom.RAT/output.py.simple
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ exec/shell/power: medium
exfil/discord: critical
exfil/stealer/browser: high
exfil/stealer/discord: high
exfil/stealer/keylogger: critical
exfil/stealer/keylogger: high
exfil/upload: high
fs/directory/create: low
fs/directory/list: low
Expand Down
7 changes: 7 additions & 0 deletions tests/python/clean/airflow/botocore_config.py.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# python/clean/airflow/botocore_config.py: medium
exec/imports/python: low
impact/remote_access/agent: medium
net/http/request: low
net/ip/host_port: medium
net/socket/connect: medium
net/url/embedded: low
13 changes: 13 additions & 0 deletions tests/python/clean/airflow/db.py.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# python/clean/airflow/db.py: medium
collect/databases/leveldb: medium
collect/databases/mysql: medium
collect/databases/postgresql: medium
collect/databases/sqlite: medium
credential/password: low
credential/ssh: medium
exec/plugin: low
fs/tempdir: low
net/ip/host_port: medium
net/tcp/sftp: medium
net/url/embedded: low
os/fd/multiplex: low
6 changes: 6 additions & 0 deletions tests/python/clean/airflow/kubernetes_engine.py.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# python/clean/airflow/kubernetes_engine.py: medium
anti-static/obfuscation/python: medium
exec/imports/python: low
net/http/auth: low
net/url/embedded: low
net/url/request: medium
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ exec/imports/python: low
exec/remote_commands/code_eval: medium
fs/file/exists: low
fs/file/open: low
impact/remote_access/py_setuptools: low
impact/remote_access/py_setuptools: medium
net/download: medium
net/url/embedded: low
os/fd/read: low
3 changes: 3 additions & 0 deletions tests/python/clean/fonttools/psLib.py.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# python/clean/fonttools/psLib.py: low
anti-static/obfuscation/python: low
exec/imports/python: low
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ exec/remote_commands/code_eval: medium
exec/shell/command: medium
fs/file/open: low
fs/file/read: low
impact/remote_access/py_setuptools: medium
net/url/embedded: low
os/fd/read: low
12 changes: 12 additions & 0 deletions tests/python/clean/google-cloud-sdk/requests_setup.py.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# python/clean/google-cloud-sdk/requests_setup.py: medium
exec/imports/python: low
exec/program: medium
exec/remote_commands/code_eval: medium
exec/shell/command: medium
fs/file/open: low
fs/path/usr_bin: low
impact/remote_access/py_setuptools: medium
net/url/embedded: low
net/url/parse: low
os/fd/read: low
process/multi: medium
1 change: 1 addition & 0 deletions tests/python/clean/idna/setup.py.simple
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
exec/imports/python: low
exec/remote_commands/code_eval: medium
fs/file/open: low
impact/remote_access/py_setuptools: medium
net/url/embedded: low
os/fd/read: low
6 changes: 6 additions & 0 deletions tests/python/clean/matplotlib/_backend_tk.py.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# python/clean/matplotlib/_backend_tk.py: medium
anti-static/obfuscation/bitwise: low
c2/tool_transfer/os: medium
discover/system/platform: medium
exec/imports/python: low
net/url/embedded: low
7 changes: 7 additions & 0 deletions tests/python/clean/matplotlib/backend_bases.py.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# python/clean/matplotlib/backend_bases.py: medium
c2/tool_transfer/os: low
discover/system/platform: medium
net/socket/connect: medium
net/socket/listen: medium
net/socket/pair: medium
net/url/embedded: low
9 changes: 9 additions & 0 deletions tests/python/clean/matplotlib/backend_qt.py.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# python/clean/matplotlib/backend_qt.py: medium
c2/tool_transfer/os: medium
discover/system/platform: medium
exec/imports/python: low
exec/remote_commands/code_eval: medium
net/socket/pair: medium
net/socket/receive: low
net/url/embedded: low
os/time/clock_sleep: medium
5 changes: 5 additions & 0 deletions tests/python/clean/matplotlib/backend_wx.py.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# python/clean/matplotlib/backend_wx.py: medium
c2/tool_transfer/os: medium
discover/system/platform: medium
exec/imports/python: low
net/url/embedded: low
16 changes: 16 additions & 0 deletions tests/python/clean/mitmproxy/raw_display.py.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# python/clean/mitmproxy/raw_display.py: medium
c2/tool_transfer/os: low
exec/imports/python: low
exec/program: medium
exec/program/background: low
exec/shell/TERM: low
fs/file/write: low
fs/path/usr_bin: low
net/socket/connect: medium
net/socket/pair: medium
net/socket/receive: low
net/socket/send: low
net/url/embedded: low
os/fd/write: low
persist/daemon: medium
process/multithreaded: medium
1 change: 1 addition & 0 deletions tests/python/clean/ml_sdk/setup.py.simple
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
exec/imports/python: low
exec/remote_commands/code_eval: medium
fs/file/open: low
impact/remote_access/py_setuptools: medium
net/url/embedded: low
os/fd/read: low
1 change: 0 additions & 1 deletion tests/python/clean/numba/support.py.simple
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ discover/system/platform: medium
exec/imports/python: low
exec/program: medium
exec/remote_commands/code_eval: medium
false-positives/setuptools: low
fs/directory/create: low
fs/directory/list: low
fs/file/open: low
Expand Down
2 changes: 1 addition & 1 deletion tests/python/clean/pydevd/setup_pydevd_cython.py.simple
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ fs/file/open: low
fs/file/read: low
fs/file/write: low
fs/tempdir/TEMP: low
impact/remote_access/py_setuptools: low
impact/remote_access/py_setuptools: medium
os/fd/read: low
os/fd/write: low
1 change: 1 addition & 0 deletions tests/python/clean/requests/setup.py.simple
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exec/remote_commands/code_eval: medium
exec/shell/command: medium
fs/file/open: low
fs/path/usr_bin: low
impact/remote_access/py_setuptools: medium
net/ip/parse: medium
net/url/embedded: low
net/url/parse: low
Expand Down
1 change: 0 additions & 1 deletion tests/python/clean/setuptools/test_pyprojecttoml.py.simple
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ discover/system/platform: medium
exec/imports/python: low
exec/shell/command: medium
fs/file/open: low
impact/remote_access/py_setuptools: low
net/url/embedded: low
os/fd/write: low
Loading