Skip to content

Latest commit

 

History

History
117 lines (87 loc) · 4.46 KB

File metadata and controls

117 lines (87 loc) · 4.46 KB

VS Code Setup

Configuring Visual Studio Code with the Python extension, selecting a virtual-environment interpreter, and debugging security scripts.

Overview

VS Code is the most popular free editor for Python and a strong default for security tooling. With Microsoft's Python extension it gains IntelliSense, linting, an integrated debugger, and per-project interpreter selection. The critical setup step for pentest work is pointing VS Code at your project's [[Managing-Virtual-Environments|venv]] so it edits, lints, and runs against the exact dependencies you pinned.

Syntax

# Install VS Code (Debian/Kali via Microsoft repo already configured, or snap)
sudo apt install -y code        # if the Microsoft apt repo is set up

# Install the Python extension from the CLI
code --install-extension ms-python.python

Explanation

Core pieces:

  • Python extension (ms-python.python) — language support, interpreter management, test/debug integration. Pulls in Pylance for fast IntelliSense.
  • Interpreter selectionCtrl+Shift+P -> "Python: Select Interpreter". Choose the .venv/bin/python inside your project.
  • Workspace settings — stored in .vscode/settings.json, committed with the project so the whole team gets the same interpreter and lint config.
  • Debugger — configured in .vscode/launch.json; set breakpoints in the gutter and inspect variables live.
// .vscode/settings.json — pin the venv and enable linting
{
    "python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
    "python.terminal.activateEnvironment": true,
    "python.analysis.typeCheckingMode": "basic"
}
// .vscode/launch.json — debug the current file with arguments
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "args": ["--target", "10.10.10.5"],
            "console": "integratedTerminal"
        }
    ]
}

Examples

# scanner.py — set a breakpoint on the parsed_port line and step through
import argparse

def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--target", required=True)
    args = parser.parse_args()
    parsed_port = 443          # breakpoint here, inspect `args`
    print(f"Scanning {args.target}:{parsed_port}")

if __name__ == "__main__":
    main()

Output

Scanning 10.10.10.5:443

Security Use Cases

  • Stepping through an exploit PoC with the debugger to watch a buffer/offset value instead of littering the code with print().
  • Using Remote - SSH to edit and debug tooling directly on a lab jump box while keeping files off your laptop.
  • Committing .vscode/settings.json so every operator on an engagement uses the identical pinned interpreter and linter.

Best Practices

  • Always run "Python: Select Interpreter" and choose the project venv before editing.
  • Enable python.terminal.activateEnvironment so the integrated terminal auto-activates the venv.
  • Turn on a type checker (basic) to catch bytes/str mix-ups common in network code.
  • Keep secrets and target lists out of launch.json args if the repo is shared.

Common Mistakes

  • Editing with the global interpreter selected, so Pylance shows imports as unresolved or resolves the wrong package versions.
  • Committing a launch.json that hardcodes live target IPs or credentials.
  • Assuming "Run Python File" activates the venv — verify the terminal prompt shows (.venv).

Practical Lab

Goal: debug a script running inside a venv.

  1. python3 -m venv .venv && source .venv/bin/activate && pip install requests.
  2. Open the folder in VS Code, install ms-python.python, and select the .venv interpreter.
  3. Create a script that GETs a URL with requests, set a breakpoint on the response line.
  4. Press F5, step over the request, and inspect the response object in the debugger's Variables pane.

References

Related

  • [[Selecting-an-IDE]]
  • [[Managing-Virtual-Environments]]
  • [[Pip-Package-Manager]]
  • [[Python-Environment-Setup/Readme|Python Environment Setup]] — module index
  • [[Readme|Python for Security Professionals]] — course home