Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python for CTF Scripting [Lect 2: Competitive C...

Python for CTF Scripting [Lect 2: Competitive Cybersecurity 101]

Moving beyond basic syntax, this session focuses on writing robust, automated scripts to solve CTF challenges efficiently. Students learn to leverage Python dictionaries for fast data mapping and functions for modular "decoder toolboxes." The lecture covers essential forensic and web exploitation techniques, including memory-efficient File I/O for parsing massive logs, bulletproof exception handling (`try/except`), command-line arguments via `sys`, and making HTTP requests using the `requests` library. The session culminates in a hands-on automated log parser challenge

Kannan Murugapandian

October 13, 2026

Transcript

20 slides
  1. Competitive Cybersecurity 101 Session 2: Python for CTF Scripting Kannan

    Murugapandian July 17, 2026 Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 1 / 20
  2. Disclaimer & Ethical Rules Strictly for Educational Purposes All knowledge

    and tools must only be applied in environments where you have explicit permission. Do not test on systems you do not personally own or lack authorization to assess. Unauthorized access constitutes a criminal offense under the Computer Misuse Act. Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 2 / 20
  3. Today’s Agenda Data & Modularity: Dictionaries & Functions Forensics Prep:

    File I/O (Reading & Writing) Bulletproof Code: Exception Handling The Hacker’s Toolkit: sys & requests Hands-On: Automated Log Parser Challenge Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 3 / 20
  4. From Foundations to Scripting Session 1: We learned the alphabet

    (variables, loops). Session 2: We are writing sentences (automation). The Golden Rule of CTFs: If you have to do a task more than three times, write a script for it! Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 4 / 20
  5. Dictionaries: The ”Why” What are they? Collections that store data

    in key:value pairs. Why it matters in CTF: Perfect for mapping ports to services. Extremely fast for looking up data compared to lists. Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 5 / 20
  6. Dictionaries: The Code # Creating a dictionary of common ports

    services = { " http " : 80 , " ssh " : 22 } # Safe lookup using . get () # ( won ’t crash if key is missing ) print ( services . get ( " ftp " , " Not found " ) ) Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 6 / 20
  7. Functions: The ”Why” What are they? Reusable blocks of code

    that only run when called. Why it matters in CTF: Build your own custom ”decoder toolbox”. Keeps your scripts clean, modular, and easy to read. Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 7 / 20
  8. Functions: The Code def decode_hex ( hex_string ) : "

    " " Converts hex back to text . " " " return bytes . fromhex ( hex_string ) . decode () # Calling the function secret = " 68656 c6c6f " print ( decode_hex ( secret ) ) Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 8 / 20
  9. File I/O: The ”Why” What is it? Reading from and

    writing to the filesystem. Why it matters in CTF: Forensics: Parsing massive log files or memory dumps. Always use the with statement to prevent memory leaks! Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 9 / 20
  10. File I/O: Reading # Reading a file line by line

    # ( Highly memory efficient !) with open ( ’ server_logs . txt ’ , ’r ’) as file : for line in file : if " ERROR " in line : print ( line . strip () ) Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 10 / 20
  11. File I/O: Writing # ’w ’ overwrites . ’a ’

    appends to the end . with open ( ’ found_flags . txt ’ , ’a ’) as file : file . write ( " CTF { f1l3_10_m4st3r }\ n " ) print ( " Flag saved to disk ! " ) Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 11 / 20
  12. Exception Handling: The ”Why” What is it? Using try and

    except to catch errors. Why it matters in CTF: CTF servers crash. Files get corrupted. Networks drop. Goal: Bulletproof scripts that don’t quit mid-competition! Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 12 / 20
  13. Exception Handling: The Code try : with open ( ’

    secret . txt ’ , ’r ’) as f : data = f . read () except Fil eNotF oundEr ror : print ( " Error : File missing ! " ) except Exception as e : print ( f " Unexpected error : { e } " ) Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 13 / 20
  14. Command Line Arguments (sys) The Concept: Pass inputs directly from

    the terminal. Example: python solve.py target.txt Why it matters in CTF: Makes your scripts flexible and reusable without editing the code. Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 14 / 20
  15. sys in Action import sys # sys . argv [0]

    is ’ solve . py ’ # sys . argv [1] is the first argument if len ( sys . argv ) < 2: print ( " Usage : python solve . py < file > " ) else : print ( f " Analyzing { sys . argv [1]}... " ) Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 15 / 20
  16. Web Exploitation (requests) The Concept: The industry standard library for

    HTTP requests. Note: Install via pip install requests Why it matters in CTF: Your primary weapon for Web Exploitation. Fetch pages, submit forms, and interact with APIs. Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 16 / 20
  17. requests in Action import requests url = " http ://

    target . com / hidden " response = requests . get ( url ) # Check for Status 200 ( Success ) if response . status_code == 200: print ( " Page found ! " ) Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 17 / 20
  18. Mini Challenge: Automated Log Parser Scenario: You have a massive

    access.log file containing hidden Base64 strings. Your Script Must: 1 Accept the log file name via sys.argv. 2 Read the file line by line. 3 Use try/except to handle decoding errors. 4 Save decoded lines containing ”FLAG” to evidence.txt. Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 18 / 20
  19. Key Takeaways Automate: Use functions to avoid repetitive work. Parse:

    File I/O is the backbone of Forensics. Protect: Always use try/except for stability. Interact: Master requests for Web Exploitation. Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 19 / 20
  20. Thank You! Questions? Email: [email protected] Course Hub: kannan.bearblog.dev/ctf-dps/ Next Session:

    Cryptography I (Classical & Symmetric) July 24, 2026 Kannan Murugapandian Competitive Cybersecurity 101 July 17, 2026 20 / 20

More Decks by Kannan Murugapandian

Other Decks in Technology