Skip to content

feat(heuristics): add whitespace check to detect excessive spacing and invisible characters for malware check #1086

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
2815028
feat(heuristics): add Whitespace Check to detect excessive spacing an…
AmineRaouane May 19, 2025
6978bd7
chore: add config variable to defaults.ini and minor cleanup
AmineRaouane May 26, 2025
76bc6dc
chore: store provenance asset info (#975)
benmss May 29, 2025
8dad2ae
feat(security): add package name typosquatting detection (#1059)
AmineRaouane Jun 3, 2025
a470123
refactor: improve experimental source code pattern analysis of pypi p…
art1f1c3R Jun 4, 2025
d6134ad
feat: add GitHub attestation discovery (#1020)
benmss Jun 4, 2025
279a001
build: replace shared library with standalone cuevalidator binary (#1…
behnazh-w Jun 6, 2025
540cf2d
fix: include inspector links with information on if they are reachabl…
art1f1c3R Jun 12, 2025
d943e7b
docs: include source code analysis subsection in malicious package tu…
art1f1c3R Jun 12, 2025
f28a8cd
fix(pypi): update get_maintainers_of_package to avoid request blockin…
AmineRaouane Jun 13, 2025
c2b016a
feat(heuristics): add Whitespace Check to detect excessive spacing an…
AmineRaouane May 19, 2025
2cbfd7b
chore: add config variable to defaults.ini and minor cleanup
AmineRaouane May 26, 2025
d2b5af5
Merge upstream/main into white-spaces-heuristic
AmineRaouane Jun 17, 2025
697106a
feat(heuristics): add Whitespace Check to Semgrep
AmineRaouane Jun 17, 2025
a62851e
refactor(semgrep): update excessive spacing regex and clarify obfusca…
AmineRaouane Jun 23, 2025
c1f07a4
Merge branch 'main' into white-spaces-heuristic
behnazh-w Aug 8, 2025
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
10 changes: 10 additions & 0 deletions src/macaron/resources/pypi_malware_rules/obfuscation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,13 @@ rules:
- pattern: os.writev(...)
- pattern: os.pwrite(...)
- pattern: os.pwritev(...)

- id: obfuscation_excessive-spacing
metadata:
description: Detects the use of excessive spacing in code, which may indicate obfuscation or hidden code.
message: Hidden code after excessive spacing
languages:
- python
severity: WARNING
pattern-either:
- pattern-regex: '[ \t\n\r\f\v]{50,}[^ \t\n\r\f\v]+' # The 50 here is the threshold for excessive spacing , more than that is considered obfuscation
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.

"""
Running this code will not produce any malicious behavior, but code isolation measures are
in place for safety.
"""

import sys

# ensure no symbols are exported so this code cannot accidentally be used
__all__ = []
sys.exit()

def test_function():
"""
All code to be tested will be defined inside this function, so it is all local to it. This is
to isolate the code to be tested, as it exists to replicate the patterns present in malware
samples.
"""
sys.exit()

# excessive spacing obfuscation
def excessive_spacing_flow():
print("Hello world!")
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,21 @@
"end": 68
}
]
},
"src.macaron.resources.pypi_malware_rules.obfuscation_excessive-spacing": {
"message": "Hidden code after excessive spacing",
"detections": [
{
"file": "obfuscation/excessive_spacing.py",
"start": 24,
"end": 25
},
{
"file": "obfuscation/inline_imports.py",
"start": 27,
"end": 27
}
]
}
},
"disabled_sourcecode_rule_findings": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_function():
__import__('builtins')
__import__('subprocess')
__import__('sys')
__import__('os')
print("Hello world!") ;__import__('os')
__import__('zlib')
__import__('marshal')
# these both just import builtins
Expand Down
Loading