Cyberlium

Python › Module 4 › Lesson 2

BeginnerModule 4Lesson 2/4

Secrets, Ethics & Scope

Never hardcode API keys, never scan without permission, and keep scripts out of gray zones

15 min+21 XP3 quiz
Module progress2 of 4
🔒
Scope · Secrets · Authorization

Opening

You can open sockets, fetch URLs, parse logs, and generate secrets. That is enough capability to end a career — “I was just practicing” will not save it.

Modules 1–3 gave you a working set: files and loops, localhost sockets, requests with ethics, a scanner that must stay on 127.0.0.1, a password generator that must not become Hydra, a parser that must not become a production scrape. This lesson is the lock on that set. Secrets handling: no API keys in source, environment variables or a chmod 600 file, .gitignore so the file never rides a push. Scope: written rules of engagement, localhost and lab VMs you own versus the internet, no scanning without permission. Gray zones: curiosity is not authorization; a classmate’s laptop is not a lab; a bug bounty without a published program is still a guess. You will practice os.environ.get, a dummy key name, and a notes file that records habits — not a live token. You will not scan the public internet “to finish the lab.” You will not commit SOC_API_KEY. You will not test generated passwords against live logins. You will not harvest production logs. Next is a hash lab: SHA-256 of YOUR files in cyberlium-lab to detect change, not rainbow tables and not password cracking.

1. Hardcoded keys are incidents: os.environ, chmod 600 files, .gitignore

A string like api_key = "sk_live_example" inside a .py file will be copied, screenshotted, committed, and scraped by bots that watch public git. That is not a style issue. It is a secret-exposure incident. The fix is mechanical: the process environment holds the value; the source holds the name. import os; api_key = os.environ.get("SOC_API_KEY"); if not api_key: raise SystemExit("Missing SOC_API_KEY"). The script fails closed when the key is absent instead of falling back to a default that someone left in the repo. For local labs, a file $HOME/cyberlium-lab/.env.soc with chmod 600 and a line SOC_API_KEY=... is acceptable if that path is in .gitignore. You still do not put a real billed cloud key in the notes you might screenshot for class. Use a clearly fake value like CHANGE_ME_NOT_A_REAL_KEY when you are only proving the read path.

Least privilege: a token that can only read a test mailbox is better than a god token that can delete production. Rotate when exposed: if it hit git, revoke it at the provider, do not “delete the commit locally” and hope clones died. git history keeps secrets. Assume leak, rotate, then add .gitignore. Do not scan other people’s repositories for keys without a program that invites that (and even then, disclose, do not use the key). This course does not teach secret-scanning other orgs. Your job is your tree: grep your own repo for key-shaped literals before you push. Never commit API keys. chmod 600 any file that holds one. Windows: restrict the ACL in your profile if you lack chmod.

2. Written RoE and localhost versus the internet: permission is a control, not a vibe

Rules of engagement (RoE) are a written statement of what you may touch, when, and with which tools. In a pentest, that is a signed PDF. In this course, the RoE is the lesson text: localhost, lab VMs you created, files in $HOME/cyberlium-lab you created, accounts you own. The internet, a neighbor’s router, a classmate’s SSH, a work VPN, a random hostname from a tutorial, and “the cloud” without a project you own are outside. Module 2 already forced HOST = "127.0.0.1". That was not a training-wheels insult. Connecting to a foreign address with a scanner is unauthorized access in many jurisdictions even if you “only connected.” requests.get to a site you do not have in writing, at volume, is the same family of mistake as a scan.

A ticket, an email from the owner, or a published bounty program with a scope section beats a Slack joke. Dates matter: permission for last quarter is not permission tonight. Host lists matter: *.lab.example.com does not include the company’s identity provider. If it is not written, you do not scan. If you own the host, you can still wreck it — that is why localhost and snapshots exist. Gray-zone scripts: “anonymous” mass scanners, auto-exploit frameworks pointed at the WAN, password sprays “just to see if default creds work on the office Wi-Fi.” Those are not labs. They are how people get expelled, fired, or charged. Keep offensive learning inside a VM network that does not route to the office or the ISP’s other customers.

3. “I was just practicing” is not a defense — intent speeches do not rewrite packets

Courts, employers, and schools look at what you pointed the tool at, not at the comment in your source that says learning. A port connect is a port connect. A login POST is a login POST. A copy of production logs is a copy of production logs. Python is not a magic language that converts those into homework. Curiosity, boredom, “but the tutorial used scanme.example.com,” and “I was going to tell them if it worked” are not controls. The control is scope you can show: localhost, your VM, your file, a letter. If you cannot show it, do not send the packet. If you already sent it, stop, do not double down with a bigger scan, and do not invent a cover story. This lesson will not give you a legal script. It will give you a habit: fail closed on missing keys, fail closed on missing permission.

4. Wrong vs right: keys in git and gray-zone scans vs env, gitignore, written scope

Worked failure — same os.environ skill, opposite leak and opposite target. Right never includes unauthorized scanning or live-login tests.

  • Wrong

    Paste a live API key into monitor.py and push. chmod 644 a .env on a shared PC. Scan the internet because the scanner script “needed a real target.” Hydra the office VPN to practice. Scrape prod logs. Say you were just practicing. Search other companies’ repos for keys and try them. Disable TLS “to make the lab work” against random hosts. This course forbids all of that.

  • Right

    os.environ.get with fail-closed. Dummy or lab-only values. .gitignore for env files. chmod 600. Written RoE: localhost / your VMs / cyberlium-lab files. No scan without permission. No live-login tests. Notes in $HOME/cyberlium-lab/ethics-scope.txt. Next lab hashes YOUR files — integrity, not cracking.

5. Practical: prove the env read with a fake key — never commit a real one

The script below reads SOC_API_KEY from the environment. You export a clearly fake value, run it, then write ethics notes. You do not put a production token anywhere in cyberlium-lab. You add a .gitignore stanza so .env.soc cannot be added by accident. chmod 600 the env file and the notes.

Command guide

Read SOC_API_KEY from the environment — fake value, fail closed

DEFENSIVE secrets habit. Use a FAKE key. NEVER commit real API keys. NEVER scan without permission. NEVER Hydra / live-login test. NEVER scrape prod.

Command — copy this

mkdir -p "$HOME/cyberlium-lab"
cd "$HOME/cyberlium-lab"

Command — copy this

cat > read_secret.py << 'PY'
import os
import sys

api_key = os.environ.get("SOC_API_KEY")
if not api_key:
    sys.exit("Missing SOC_API_KEY — fail closed, no hardcoded default")
if api_key.startswith("sk_live_") or "BEGIN" in api_key:
    sys.exit("Refusing a value that looks like a live secret in this teaching script")
print("SOC_API_KEY loaded from environment, length:", len(api_key))
print("ok: source file contains no key literal")
PY

Fake lab value only — not a real token.

Command — copy this

export SOC_API_KEY="CHANGE_ME_NOT_A_REAL_KEY"
python3 read_secret.py

Optional file form (still not a real key). Keep it untracked.

Command — copy this

printf '%s
' 'SOC_API_KEY=CHANGE_ME_NOT_A_REAL_KEY' > .env.soc
chmod 600 .env.soc read_secret.py

Ignore env files if this folder is ever a git repo.

Command — copy this

touch .gitignore
grep -q '\.env' .gitignore 2>/dev/null || printf '%s
' '.env' '.env.*' '*.pem' >> .gitignore

Command — copy this

NOTES="$HOME/cyberlium-lab/ethics-scope.txt"
{
  echo "=== SECRETS, ETHICS, SCOPE ==="
  echo "keys: os.environ.get / chmod 600 file — NEVER literals in .py"
  echo "gitignore: .env .env.* *.pem"
  echo "roe: localhost, lab VMs I own, files I created in cyberlium-lab"
  echo "not_in_scope: internet scans, classmate SSH, work VPN, prod logs"
  echo "phrase_that_fails: I was just practicing"
  echo "no_hydra: generated passwords are for MY manager, not live logins"
} > "$NOTES"
chmod 600 "$NOTES"

NEVER: git add .env.soc after putting a real key in it NEVER: python scanner.py --host 8.8.8.8 NEVER: use a leaked key "to confirm it works"

Mission: env read + ethics-scope.txt (mode 600), no real keys

1) Run a fail-closed os.environ.get("SOC_API_KEY") with a clearly fake value — no live token in source or notes. 2) Record .gitignore habits, chmod 600, written RoE (localhost / your VMs / your files), and that “I was just practicing” is not a defense. 3) Save to $HOME/cyberlium-lab/ethics-scope.txt and chmod 600. Never commit API keys. Never scan without permission. Never test passwords against live logins.

Stuck? Ask Cyberlium AI Mentor

If “the key has to live in the file or cron will not work” or “scanme hosts are fair game” still feels true, ask for a hint — not a target list. Try: "Hint only: why os.environ.get plus .gitignore beats a literal in .py, why localhost is the scanner RoE, and why I was just practicing is not a defense?" You still fill ethics-scope.txt. No live keys, no WAN scans, no Hydra.

You now treat a hardcoded key as an incident, permission as a written control, and practice-as-defense as a sentence that fails. Environment, chmod 600, .gitignore, localhost. Next — Lab: File Integrity Hash Check — hashlib SHA-256 of YOUR files in cyberlium-lab, before/after an edit, integrity rather than cracking or rainbow tables.

Knowledge Check

1

APPLY: monitor.py contains api_key = "sk_live_example" and you are about to git push. What kind of event is that, and the fix?

Multiple choice

Knowledge Check

2

APPLY: True or False: Written authorization (RoE, ticket, published bounty scope) matters for scanning beyond your lab, and “I was just practicing” makes an unauthorized connect legal.

True or False

Knowledge Check

3

APPLY: os.environ.get("SOC_API_KEY") returned None. A classmate says hardcode the key and point the leftover scanner at a random public host so the lab “does something.” Your move?

Multiple choice

← Previous

Answer all 3 knowledge checks to continue. (0/3 answered)