Programming for security work: Python, shell, and reading code for flaws

The programming a security learner needs: enough Python and shell to automate checks and parse logs, and enough code reading to spot where input reaches a query, a command, or a file path.

Lesson 12 of 18 · 12 min read · practice only with authorization

Diagram

From a question to a script

Follow the path. One example at a time.

Step 1 of 4

Question

Which hosts in last week’s logs had more than 20 failed SSH logins?

Illustration: Question
# Question
# Which hosts in last week’s logs had more than 20 failed
# SSH logins?

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Read where input goes

    What fails
    Most web flaws are input reaching an interpreter: SQL, a shell, a template, or a file path.
    Who is affected
    Users of the application.
    What you would notice
    String concatenation building a query or a command.
    What you change
    Use parameterized APIs and argument lists instead of strings.
  2. Automate the boring checks

    What fails
    Manual checks are skipped when people are busy.
    Who is affected
    Systems that drift from their baseline.
    What you would notice
    A hardening checklist nobody has run in months.
    What you change
    Script the check, run it on a schedule, and alert on differences.
  3. Scripts are code too

    What fails
    Security scripts handle secrets and run with privileges.
    Who is affected
    Every host the script touches.
    What you would notice
    Tokens hard-coded in a script in a shared folder.
    What you change
    Read secrets from the environment or a vault, and run shellcheck and code review.

Python first

Python reads files, parses JSON, calls APIs, and has libraries for nearly every protocol. Learn file handling, regular expressions, the requests library, the json and csv modules, and virtual environments. Most daily security scripting is parsing output and calling an API.

Shell for glue

Bash, or PowerShell on Windows, glues tools together. Start every Bash script with set -euo pipefail, quote variables, and prefer reading structured output with jq over cutting columns.

#!/usr/bin/env bash
set -euo pipefail
aws iam list-users --output json \
  | jq -r '.Users[] | [.UserName, (.PasswordLastUsed // "never")] | @tsv'

Reading code for flaws

When reviewing code, follow each piece of user input from where it enters to where it is used. Mark every place it reaches a database, a shell, a template, a file path, a redirect, or an outbound HTTP request. Those are the places to check for parameterization, allow-lists, and authorization.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

Why use subprocess.run(["ping", "-c", "1", host]) instead of a shell string?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

This page stands on its own. The lab and the glossary card are the next reading.

Practice

  1. LabScope deskDecide whether a situation is in scope, needs a written change, or stops.