Skip to content

Instantly share code, notes, and snippets.

View M0r13n's full-sized avatar
🦔

Leon Morten Richter M0r13n

🦔
View GitHub Profile
@M0r13n
M0r13n / totp.py
Last active April 19, 2025 12:20
Basic HMAC-based One-Time Password Algorithm in Python
# Basic HMAC-based One-Time Password Algorithm in Python
# Based on RFC 4226 and RFC 6238
import base64
import datetime
import hashlib
import struct
import time
def dt(hmac_result: bytes) -> int:
@M0r13n
M0r13n / virt2phy.py
Created March 23, 2025 12:38
Python3: Translate virtual memory address into physical memory address using `/proc/self/pagemap`
import ctypes
import os
import sys
buffer = bytearray(1_000_000)
vaddr = ctypes.addressof(ctypes.c_char.from_buffer(buffer))
print(f'Allocated an array of {len(buffer)} bytes at 0x{vaddr:x}.')
@M0r13n
M0r13n / tunny.py
Created February 9, 2025 13:32
Python script that demonstrates network namespace isolation and TUN device manipulation. The code creates a user namespace with root privileges and a network namespace, then sets up a TUN virtual network interface to handle ICMP (ping) traffic. It implements a basic IPv4 pseudo-gateway that responds to ICMP Echo Requests with Echo Replies.
#!/usr/bin/env python3
# sudo sysctl -w kernel.apparmor_restrict_unprivileged_unconfined=0
# sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
# unshare --user --map-user=0
from dataclasses import dataclass
import fcntl
import getpass
import multiprocessing
import os
@M0r13n
M0r13n / fullmatch.py
Created December 26, 2024 10:54
Backport of pathlibs `full_match` to Python 3.10+
import pathlib
import re
import os
import functools
def _translate(pat, STAR, QUESTION_MARK):
res = []
add = res.append
i, n = 0, len(pat)
while i < n:
@M0r13n
M0r13n / inotify.py
Last active January 26, 2025 21:30
Play around with the inotify C-API in Python
import ctypes
import select
import pathlib
from ctypes import c_char_p, c_int, c_uint32
import os
class EventStruct(ctypes.Structure):
@M0r13n
M0r13n / autostart.py
Last active December 24, 2024 12:13
Flask like auto reload of arbitrary executables/scripts on code change
#!/bin/env python3
"""This is a simple self-contained script to reload an arbitrary application
when its source changes (like using Flask in debug mode). It comes without
any 3rd party dependencies and can run standalone - a copy of this script is
all you need.
Examples:
./autostart.py -p '*.py' -i 'venv/*' flake8 autostart.py --max-line-length 120
./autostart.py -p '*.py' -i 'venv/*' mypy ./autostart.py
./autostart.py -p '*.py' -i 'venv/*' "$(which python3)" ./server.py
@M0r13n
M0r13n / nat.md
Created December 16, 2024 13:21
NAT Hole Punching using NetCat

NAT Traversal Setup with Netcat

This guide demonstrates a simple NAT traversal setup using tcpdump and nc (Netcat) for UDP communication.

Step 1: Monitor UDP Request on the Server

On the server, use tcpdump to monitor the incoming UDP packets on port 12345:

sudo tcpdump -i any udp and port 12345

@M0r13n
M0r13n / hole.md
Created December 16, 2024 12:37
How to punch a hole through a stateful firewall using UDP opening a reverse shell.

UDP Hole Punching

The following example demonstrates how to punch a hole through a stateful firewall using UDP. It opens a reverse shell on the server.

⚠️ Using reverse or bind shells can be highly insecure and potentially illegal if executed without authorization. Always ensure you have explicit permission before performing such actions in a network.

Assumptions

  • Server: The target machine on which the shell will be opened.
  • Client: The machine used to remotely connect to the shell.
@M0r13n
M0r13n / tracking.py
Created September 1, 2024 11:23
AIS: How to collect and maintain the state of individual vessels over time by keeping track of several messages using pyais
import pathlib
import pyais
from pyais.tracker import AISTrackEvent
def do_something(track):
# called every time an AISTrack is created or updated
print(track.mmsi, track)
@M0r13n
M0r13n / wc.py
Created June 21, 2024 11:49
word count like using `wc` using async state machine parsing. Inspired by: https://github.com/robertdavidgraham/wc2
WAS_SPACE = 0
NEW_LINE = 1
NEW_WORD = 2
WAS_WORD = 3
SPACES = [9,10,11,12,13,32]
NEWLINE = 10
def init_table():
# 0 => was space