Skip to content

Add PyArmor obfuscation detection heuristic#672

Open
stevenelliottjr wants to merge 1 commit intoDataDog:mainfrom
stevenelliottjr:feature/detect-pyarmor
Open

Add PyArmor obfuscation detection heuristic#672
stevenelliottjr wants to merge 1 commit intoDataDog:mainfrom
stevenelliottjr:feature/detect-pyarmor

Conversation

@stevenelliottjr
Copy link
Contributor

Summary

This PR adds a new Semgrep rule to detect malicious packages that use PyArmor obfuscation, which is a common technique used by malware to hide malicious code from static analysis.

  • Adds pyarmor.yml rule that detects PyArmor obfuscation patterns
  • Covers both legacy (< 8.0) and modern (>= 8.0) PyArmor versions
  • Includes comprehensive test cases

Detection Coverage

The rule detects the following PyArmor patterns:

Bootstrap Code:

  • __pyarmor__(__name__, __file__, b'...') - main obfuscation wrapper

Legacy PyTransform (PyArmor < 8.0):

  • from pytransform import pyarmor_runtime
  • import pytransform
  • pyarmor_runtime() calls

Modern Runtime (PyArmor >= 8.0):

  • from pyarmor_runtime import __pyarmor__
  • from pyarmor_runtime_XXXXXX import __pyarmor__

Bytecode Markers:

  • __armor_enter__ / __armor_exit__
  • __pyarmor_enter__ / __pyarmor_exit__

Verification Functions:

  • check_armored()
  • @assert_armored()

Example Detection

from pytransform import pyarmor_runtime
pyarmor_runtime()
__pyarmor__(__name__, __file__, b'PY00000000\x00\x03\x0a...')

Test Plan

  • Created test file with all PyArmor patterns (17 positive cases, 8 negative cases)
  • Verified rule detects all pattern variants
  • Ran semgrep tests - all 31 rules pass
  • Ran existing test suite (pytest tests/core/test_sourcecode_analyzer.py) - all pass

Closes #533

Adds a new Semgrep rule to detect packages that use PyArmor obfuscation,
which is commonly used by malware to hide malicious code.

Detects the following PyArmor patterns:
- __pyarmor__() bootstrap code
- pytransform runtime imports (PyArmor < 8.0)
- pyarmor_runtime package imports (PyArmor >= 8.0)
- __armor_enter__/__armor_exit__ bytecode markers
- check_armored() and @assert_armored verification functions

Closes DataDog#533
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect pyArmor obfuscation

1 participant