Python Input Lab

Lab 1: Secure Port Checker

Objective: Create a script that takes a port number and checks if it’s a “Reserved” port (1-1024).

Python

# Step 1: Capture Input
port_input = input("Enter the Port Number to scan: ")

# Step 2: Input Validation & Type Casting
try:
    port = int(port_input) # Convert string to integer
    
    if port <= 1024:
        print(f"Warning: Port {port} is a Reserved System Port!")
    else:
        print(f"Port {port} is safe to use for your application.")
        
except ValueError:
    print("Error: Please enter a valid numeric number, not text!")

Lab 2: The “Environment Allow-list” Validator

Objective: Prevent unauthorized strings from entering your script logic.

Scenario: You are writing a script that triggers a security scan. The script should only proceed if the user enters “dev”, “test”, or “prod”. If they enter anything else (like “hacker_env”), the script must stop.

The Task:

  1. Use an “Allow-list” (a Python list of approved strings).
  2. Clean the input using .lower() and .strip() to ensure ” PROD ” or “Prod” works correctly.
  3. Check if the input is in the list.

Solution:

allowed_environments = ["dev", "test", "prod"]

# Capture and Clean
target_env = input("Enter Target Environment (dev/test/prod): ").strip().lower()

# Validate against Allow-list
if target_env in allowed_environments:
    print(f"Validated! Starting security audit on [{target_env}]...")
else:
    print(f"CRITICAL ERROR: Environment '{target_env}' is NOT authorized.")

13.6.2 Lab 3: Password Masking (Secure Input)

Objective: Capture sensitive data (like API Keys or Passwords) without showing them on the screen.

Scenario: In DevSecOps, “Shoulder Surfing” is a risk. If you use standard input(), everyone can see the password as you type it. We need to hide it.

The Task:

  1. Use the built-in getpass module.
  2. Ensure the sensitive data is never printed back to the terminal.

Solution:

import getpass

user = input("Enter Admin Username: ")
# This hides the characters as you type
api_token = getpass.getpass("Enter your Secret API Token: ")

if api_token == "Secret123":
    print(f"Access Granted. Welcome, {user}!")
else:
    print("Authentication Failed!")

13.6.3 Lab 4: The “Non-Interactive” Architect (argparse)

Objective: Convert an interactive script into a professional CLI tool for Jenkins/GitHub Actions.

Scenario: You have a script that requires an IP address and a Port. You want to pass these as Command Line Arguments so the script can run in an automated CI/CD pipeline without waiting for a human to type.

The Task:

  1. Use the argparse library.
  2. Define a required argument for the IP and an optional argument for the Port (default 80).

Solution:

import argparse

# 1. Initialize Parser
parser = argparse.ArgumentParser(description="DevSecOps Automated Scanner")

# 2. Add Arguments
parser.add_argument("--host", help="Target IP Address", required=True)
parser.add_argument("--port", type=int, default=80, help="Target Port (Default: 80)")

# 3. Parse and Access
args = parser.parse_args()

print(f"Automated Scan initiated for {args.host} on port {args.port}...")
  • How to run it: python scanner.py --host 192.168.1.1 --port 443

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top