Skip to content
Files

Files

File read, write, and wordlist iteration

Reads and writes complete text files with a context manager, or iterates through a wordlist one line at a time.

# write
with open("script.sh", "w") as f:
    f.write(payload)

# read complete file
with open("loot.txt", "r") as f:
    data = f.read()

# iterate through a wordlist
with open(WORDLIST, "r") as f:
    for line in f:
        word = line.strip()
        if word:
            ...

Find by: file, read, write, open, wordlist, lines, save, load, payload file, iterate, strip, with open

Filesystem paths with pathlib

Path stores a filesystem location. File operations and path transformations are performed explicitly so each intermediate value remains available for later exploit stages.

from pathlib import Path

# save a downloaded binary file
output_path = Path("certificate.pdf")
output_path.write_bytes(r.content)
absolute_output_path = output_path.resolve()
print(f"[+] Saved to {absolute_output_path}")

# read binary data
raw_bytes = output_path.read_bytes()

# read a complete text file
loot_path = Path("loot.txt")
text = loot_path.read_text()

# guard and cleanup
if output_path.exists():
    output_path.unlink()

The same path object can guard creation of a generated payload and provide its absolute path to a later process:

payload_path = Path("payload.php")
if payload_path.exists():
    print("Payload already exists")
else:
    payload_path.write_text(payload)

absolute_payload_path = payload_path.resolve()

Find by: pathlib, path, write_bytes, read_bytes, read_text, write_text, resolve, exists, unlink, save file, download, absolute path, binary file, generated payload