keepcalculator

06 Numbers and Powers

Detect and Remove Malicious Instructions in AI Code

Card No. 06·56899 · Updated · Standard formula, shown below

AI-generated code can contain unsafe commands, hidden data collection, or changes that weaken security, even when it appears to do the requested task. Review it as untrusted code before running or deploying it; the steps below help you identify suspicious behavior, remove it safely, and verify the result.

What You’ll Need

  • The generated code and its surrounding project files, including dependency manifests and configuration.
  • A text editor or code editor with search, plus a way to inspect changes with version control or a diff tool.
  • A safe, isolated environment for testing, such as a disposable virtual machine or container without access to sensitive files, credentials, or production systems.
  • Available static-analysis, dependency-audit, and secret-scanning tools appropriate to the language. These help, but do not replace review.
  • A known-good copy or commit of the project so you can compare and restore legitimate changes.

Step-by-Step

  1. Keep the code untrusted until reviewed. Do not run it, install its dependencies, open its build scripts, or paste it into a privileged terminal. Build and install steps can execute code too. If you have already run a suspicious project, disconnect it from networks and sensitive services, then follow your organization’s incident-response process.
  2. Establish what the code is supposed to do. Write down the requested features, expected inputs and outputs, and files that should change. Compare the generated output with the prompt and the existing project. Unrequested behavior—especially access to files, accounts, networks, or system settings—deserves investigation.
  3. Inspect the full change, not just the main file. Use version-control status and a diff to find new, modified, renamed, and deleted files. Check source code, tests, package manifests and lockfiles, shell scripts, build configuration, editor tasks, startup files, and CI workflows. Malicious behavior may be placed in a dependency or a command that runs during installation or build.
  4. Search for high-risk operations. Look for code that reads environment variables, credential stores, browser profiles, SSH keys, configuration files, or broad directory paths. Check for outbound network requests, uploads, webhooks, unusual DNS lookups, subprocess or shell execution, dynamic code loading, persistence, and changes to permissions or security settings. These APIs can be legitimate; trace their purpose, inputs, destinations, and data flow rather than deleting them automatically.
  5. Look for concealment and obfuscation. Investigate encoded or compressed strings, long unreadable expressions, reflection, dynamically constructed commands, hidden files, misleading names, and code that disables logs or checks. Search for suspicious terms and inspect unfamiliar generated files, but remember that a clean keyword search does not prove the code is safe.
  6. Review dependencies and external sources. Identify packages added or versions changed, and verify that each is needed and comes from the expected registry or repository. Check package lifecycle scripts and install hooks. Use the ecosystem’s dependency-audit tools and review reported advisories; do not treat a clean report as proof that a package is trustworthy. Remove unnecessary dependencies and pin or lock versions according to the project’s normal process.
  7. Remove behavior precisely. Delete clearly unrelated commands, data access, network transmission, persistence, or obfuscated loaders. For legitimate functionality that overlaps with a risky operation, replace it with the narrowest implementation that meets the stated requirement. Preserve necessary behavior, update tests, and remove unused dependencies and configuration. Avoid broad search-and-replace edits that could break unrelated code.
  8. Check secrets and sensitive data. Search the diff for credentials, tokens, private keys, personal data, and hard-coded endpoints. If a real secret was exposed in code, logs, or a prompt, remove it from the current files and follow the provider’s rotation and incident procedures; deleting a visible line alone does not invalidate a leaked credential or erase version history.
  9. Run checks in isolation. First use static analysis, format or type checks, tests, secret scanning, and dependency auditing without executing unknown install scripts. Then, if appropriate, run the project in a disposable environment with minimal permissions, no real credentials, restricted network access, and only test data. Observe file changes, processes, and network activity. Do not use production data or give the test environment access to personal or work accounts.
  10. Review the final diff and document the decision. Confirm every remaining change is required, explain any sensitive operations, and ensure tests cover the intended behavior. Have another developer review high-impact code where possible. Keep the approved changes in version control and follow your team’s normal review and deployment controls.

Common Mistakes

  • Trusting clean-looking code: Malicious behavior can be split across files, hidden in dependencies, or triggered only by a build or particular input.
  • Running first and inspecting later: A test run may already expose secrets or modify the system. Inspect scripts and isolate execution before testing.
  • Deleting every network or shell call: Some applications need these operations. Determine whether each call is required and constrain its destination, permissions, and data.
  • Relying on a scanner alone: Tools miss unknown or context-dependent threats and can produce false positives. Combine automated checks with a human review of behavior.
  • Removing a suspicious line but keeping the dependency: An install hook or transitive package may still perform the unwanted action. Check the full dependency and build chain.

Tips

  • Ask for small, reviewable changes and a brief explanation of new files, dependencies, permissions, and network access.
  • Prefer least privilege: give test processes only the files and permissions they need, and use disposable credentials when a test genuinely requires authentication.
  • Use a clean branch and inspect the diff after each generated change. Smaller changes make unexpected behavior easier to spot and revert.
  • Keep secrets out of code and prompts. Use an approved secret manager or environment-specific configuration, and do not assume environment variables are automatically safe to expose.
  • Treat security findings as clues to investigate. Confirm the affected code path and impact before changing or dismissing a report.

Troubleshooting

A scanner flags a normal feature: Trace the flagged operation to its caller and check the data it can access or transmit. If it is required, document why and reduce its scope; do not silence the finding without evidence.

You cannot tell what an obfuscated section does: Do not execute it on your workstation. Preserve a copy, examine it with safe static tools, and ask a qualified security reviewer. If its purpose cannot be established, remove it and replace the feature with understandable code.

Tests fail after removal: Identify the exact missing behavior and decide whether it was part of the original requirement. Restore only legitimate functionality with explicit tests; do not reintroduce a risky operation merely to make a test pass.

The code has already run or a secret may have leaked: Stop further execution, isolate the affected environment, and notify the responsible security or IT team. Revoke or rotate exposed credentials through the relevant provider, check authorized logs for suspicious activity, and preserve evidence. Follow established incident-response procedures rather than relying on a code edit alone.

Frequently Asked Questions

Can a code scanner prove generated code is safe?
No. Scanners detect known patterns and issues, but can miss hidden, novel, or context-dependent behavior. Review the code and its execution paths as well.

What counts as a malicious instruction in code?
It is behavior that is harmful or unauthorized, such as stealing secrets, sending data to an unapproved destination, creating hidden persistence, or weakening protections. Judge intent and effect in context.

Should I delete every unfamiliar dependency?
Not automatically. Verify its source, purpose, version, and install behavior. Remove dependencies that are unnecessary, unexplained, or cannot be trusted.

Is a container enough to make testing safe?
No. Containers reduce some risks but may share a kernel, mounted files, credentials, or network access. Use a disposable, minimally privileged environment and avoid sensitive mounts.

What if I already committed suspicious code?
Stop deployment, notify the project’s maintainers or security team, and investigate whether it ran or exposed secrets. Revert or repair the change, rotate exposed credentials, and address repository history and incident handling with the responsible team.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →