The Academy is free // the war room is optional
DAEMONCORE // ACADEMY
← FIELD NOTES

Leveraging Python for security tools and parsing requests

2026.09.12//8 MIN READpythonhttpprogrammingsecurity-labs

// Introduction

The simplicity of Python makes it a go-to language for quick scripting tasks in security operations. Whether you're crafting small utilities, parsing HTTP requests, or automating mundane tasks, Python can save you time and headaches. Here’s how to leverage it effectively.

// Setting Up Your Environment

Make sure you have Python installed on your system. You can check this by running:

python3 --version

If it's not installed, you can download it from python.org.

For our examples, we’ll use the requests library, which simplifies working with HTTP requests. Install it using pip:

pip install requests

// Crafting a Simple HTTP Request Tool

Let’s create a small script to fetch a webpage and print out certain elements. This can be useful for quickly checking the status of services or detecting changes in web content.

Sample Script

Create a file named fetch_page.py:

import requests

url = 'http://example.com'
try:
    response = requests.get(url)
    response.raise_for_status()  # Raise an error for bad responses
    print(f'Status Code: {response.status_code}')  # Print response status
    print('Headers:')
    for header, value in response.headers.items():
        print(f'{header}: {value}')  # Print headers
    print('Content Preview:')
    print(response.text[:200])  # Print the first 200 characters of the content
except requests.exceptions.RequestException as e:
    print(f'Error fetching {url}: {e}')  # Handle errors

Explanation

  • import requests: Imports the requests library for handling HTTP requests.
  • response = requests.get(url): Sends a GET request to the specified URL.
  • response.raise_for_status(): Checks if the response was successful (status code 200).
  • response.headers: Accesses the response headers.
  • response.text: Contains the body of the response.

Running the Script

Execute the script in your terminal:

python3 fetch_page.py

You should see the HTTP status code, headers, and a preview of the content returned by the server.

Common Mistakes

  • Forgetting to handle exceptions can lead to unhandled crashes. Always use try-except blocks when dealing with network requests.
  • Hardcoding URLs can reduce flexibility. Consider allowing them to be passed as command-line arguments.

// Parsing HTTP Responses

Often, you need to extract specific data from HTTP responses. This is where Python shines.

Example: Extracting Links

Extend your previous script to extract links from the fetched page:

from bs4 import BeautifulSoup

# After fetching the response
soup = BeautifulSoup(response.text, 'html.parser')
links = soup.find_all('a')  # Find all anchor tags
for link in links:
    print(link.get('href'))  # Print the href attribute of each link

Explanation

  • BeautifulSoup: A library used for parsing HTML and XML documents. Install it using pip install beautifulsoup4.
  • soup.find_all('a'): Finds all anchor tags in the HTML.
  • link.get('href'): Retrieves the hyperlink reference from each anchor tag.

// Building Small Utility Tools

Python can also help you build small utility tools for various tasks. Here’s an example of a tool to check if a list of URLs are up:

Sample URL Checker Script

Create a file named url_checker.py:

import requests

urls = ['http://example.com', 'http://nonexistent.badtld']

for url in urls:
    try:
        response = requests.get(url)
        print(f'{url} is up: {response.status_code}')
    except requests.exceptions.RequestException:
        print(f'{url} is down or unreachable.')

Running the URL Checker

Run the script as follows:

python3 url_checker.py

You should see the status of each URL printed to your terminal.

// Defensive Implications

Creating tools with Python not only aids offensive operations but also strengthens your defensive posture. Here are some considerations:

  • Regularly monitor services using scripts to catch downtime early.
  • Parse logs and alerts to automate responses to specific threat patterns.
  • Ensure any scripts handling sensitive data maintain strict access controls and logging.

// Checklist

  • Ensure Python and required libraries are installed.
  • Validate URLs and request headers before running scripts to avoid unwanted errors.
  • Test scripts in a controlled environment before deploying them in production.
  • Regularly review and update your scripts to adapt to changing environments or vulnerabilities.

// Conclusion

Python's versatility allows for rapid development of tools that enhance security operations, from fetching web data to monitoring URL statuses. The above examples are a starting point. The DaemonCore Academy curriculum is free and offers various resources to refine your skills. Remember to practice these techniques in a disposable range you own.