Skip to content

Instantly share code, notes, and snippets.

View M0r13n's full-sized avatar
🦔

Leon Morten Richter M0r13n

🦔
View GitHub Profile
@M0r13n
M0r13n / gil_demo.py
Created December 30, 2023 11:41
GIL demo for Python3 using hashcash as a computational challenge
"""An example demonstrating the limitations of the GIL (CPython). This example
increments a counter for a given challenge so that the first N bits of the SHA1
hash of the challenge are all zeros (0). The computational effort increases
exponentially with the number of bits required to be zero.
This demo illustrates that this task cannot be sped up by parallelizing the
computation."""
import base64
import hashlib
import threading
@M0r13n
M0r13n / aoc_day5.py
Created January 1, 2024 11:32
Python solution for Advent of Code 2023 (AOC) day 5 part 2 based on iterative transformation of ranges
import itertools
def row2int(row: str) -> list[int]:
return list(map(int, row.strip().split(' ')))
def rows2list(fd):
l = []
for row in fd:
@M0r13n
M0r13n / index.py
Last active February 1, 2024 10:13
llama_index local model
import torch
from llama_index.llms import HuggingFaceLLM
from llama_index.prompts import PromptTemplate
selected_model = 'mistralai/Mixtral-8x7B-Instruct-v0.1'
SYSTEM_PROMPT = """You are an AI assistant that answers questions in a friendly manner, based on the given source documents. Here are some rules you always follow:
- Generate human readable output, avoid creating output with gibberish text.
- Generate only the requested output, don't include any other language before or after the requested output.
- Never say thank you, that you are happy to help, that you are an AI agent, etc. Just answer directly.
FROM python:3.11-slim
WORKDIR /multi
COPY . .
CMD ["python", "./sender.py"]
@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
@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 / 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 / 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 / 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 / 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):