Generating Adversarial Computer Programs using Optimized Obfuscations

Shashank Srikant, Sijia Liu, Tamara Mitrovska, Shiyu Chang, Quanfu Fan, Gaoyuan Zhang, Una-May O'Reilly

Machine learning (ML) models are increasingly deployed for critical software engineering tasks, such as bug detection, malware classification, and automated code summarization. However, these models are vulnerable to adversarial attacks. Unlike images—where attacks involve subtle, often imperceptible pixel changes—attacking code is uniquely challenging because any modification must be functionally imperceivable. The program must still compile, run, and produce the exact same output as the original.

Previous attempts to fool code models relied on heuristic or random program obfuscations, such as arbitrarily renaming a variable. This paper introduces a principled mathematical framework for generating adversarial computer programs using optimized obfuscations. By treating "where to change" and "what to change" as a joint optimization problem, the authors demonstrate that current ML models for code are significantly more vulnerable than previously thought, while also providing a path toward training more robust models.

Formulating the Attack

The authors formalize the adversarial attack as a constrained combinatorial optimization problem. They break the attack down into two key components:

  1. Site-Selection ($z$): Identifying which specific tokens or locations in the code to modify.
  2. Site-Perturbation ($u$): Deciding which specific transformation (e.g., a new variable name or a specific dead-code snippet) to apply at those locations.
Mathematical formulation of site-selection and perturbation

To solve this discrete optimization problem, they propose several first-order optimization techniques:

  • Joint Optimization (JO): Uses Projected Gradient Descent (PGD) to solve for both location and transformation simultaneously within a continuous relaxation of the discrete code space.
  • Alternating Optimization (AO): An iterative approach that fixes the location to optimize the transformation, then fixes the transformation to optimize the location. This proved more effective at escaping poor local optima.
  • Randomized Smoothing (RS): Because the "loss landscape" of code is discrete and highly irregular (or "spiky"), the authors use a convolution-based smoothing technique to flatten the landscape, making it easier for the optimizer to navigate.

The transformations used include Replace operations (renaming variables, function parameters, or object fields; replacing boolean literals) and Insert operations (adding print statements or dead code).

Key Insights: Location is Everything

The most significant insight from this work is that where you apply an obfuscation matters just as much as what the obfuscation is. Randomly picking a variable to rename is far less effective than using gradients to find the "weakest link" in the program's representation.

Comparison of random vs. optimal site selection

By reframing traditional software obfuscation—originally meant to thwart human reverse-engineering—as a mathematically grounded tool for adversarial ML, the authors achieved substantial improvements in attack success. Furthermore, code models are notoriously difficult to optimize because changing a single token can cause a massive jump in the model's output. Randomized smoothing acts like a "blur filter" on the optimization landscape, allowing the attack generator to find better adversarial examples.

Evaluation and Results

The researchers evaluated their methods on two large-scale datasets for the task of program summarization (predicting a function's name based on its body): a Python dataset of ~150,000 programs and a Java dataset of ~700,000 programs. They targeted standard SEQ2SEQ models and the specialized CODE2SEQ model.

  • Massive Attack Improvement: Their best attack (Alternating Optimization with Randomized Smoothing) achieved a 52% improvement in Attack Success Rate (ASR) over the previous state-of-the-art approach for Python programs.
  • Efficiency: The AO approach converged in just 3 iterations, outperforming the JO approach which took nearly 10.
  • Robustness Gains: Better attacks lead to better defenses. Models trained using the authors' adversarial framework (Adversarial Training) showed an improvement of ~16 points in robustness compared to standard training when facing strong attacks.
  • Transferability: The attacks proved effective across different programming languages and model architectures.

Limitations and Future Work

While highly effective, the study was limited to six specific types of obfuscations. Future work could incorporate more complex structural changes, such as loop unrolling or refactoring control flow. Additionally, while AO is faster than JO, generating these optimized attacks remains computationally expensive compared to simple heuristic attacks. The authors suggest that future research should test these optimized obfuscations on other tasks like vulnerability detection, and explore more advanced optimization schemes like ADMM (Alternating Direction Method of Multipliers) to yield even stronger attacks.

Summary updated

BibTeX
@inproceedings{srikant2021generating,
  author = {Shashank Srikant and Sijia Liu and Tamara Mitrovska and Shiyu Chang and Quanfu Fan and Gaoyuan Zhang and Una-May O'Reilly},
  title = {{Generating Adversarial Computer Programs using Optimized Obfuscations}},
  booktitle = {International Conference on Learning Representations},
  year = {2021},
  url = {https://openreview.net/forum?id=PH5PH9ZO_4},
}
Figure