Skip to content

Instantly share code, notes, and snippets.

@devilholk
devilholk / test_re_h_mod.py
Created July 6, 2020 01:57
Showcase of re_h_mod
import re, re_h_mod
p1=re.compile(r'test\s+stuff')
p2=re.compile(r'test\h+stuff')
print('p1', p1)
print('\t', p1.match('test \t stuff'))
print('\t', p1.match('test \n stuff'))
print()
print('p2', p2)
print('\t', p2.match('test \t stuff'))
@devilholk
devilholk / re_h_mod.py
Created July 6, 2020 01:59
Duckpunching python re module to add a horizontal whitespace pattern as \h
import sre_parse
h_rep, = sre_parse.parse(r'[^\S\n\v\f\r\u2028\u2029]')
def sre_parse_escape(source, escape, state):
if escape == r'\h':
return h_rep
else:
return original_sre_parse_escape(source, escape, state)
import colorsys, sys
def bold(text):
return f'\x1b[1m{text}\x1b[21m'
def italics(text):
return f'\x1b[3m{text}\x1b[23m'
class color:
black = lambda text: f'\x1b[30m{text}\x1b[38m'
red = lambda text: f'\x1b[31m{text}\x1b[38m'
import ansi_standalone, sys
ansi_standalone.install_colored_stdout_retabulator()
def F2():
with sys.stdout.indented:
print('This is F2')
print('We had two lines to say today')
void uart_send_data(volatile void* data, int length) {
dma_channel_reset(DMA1, DMA_CHANNEL4);
dma_set_peripheral_address(DMA1, DMA_CHANNEL4, (uint32_t) &USART1_DR);
dma_set_memory_address(DMA1, DMA_CHANNEL4, (uint32_t) data);
dma_set_number_of_data(DMA1, DMA_CHANNEL4, length);
dma_set_read_from_memory(DMA1, DMA_CHANNEL4);
dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL4);
dma_set_peripheral_size(DMA1, DMA_CHANNEL4, DMA_CCR_PSIZE_8BIT);
dma_set_memory_size(DMA1, DMA_CHANNEL4, DMA_CCR_MSIZE_8BIT);
@devilholk
devilholk / af_unix_send_fd.py
Created August 16, 2020 09:24
Test of sending file descriptors to another process using AF_UNIX
from socket import *
import struct
#Open a file (opens itself now)
this_script = open(__file__, 'r')
#List of file descriptors to send
fd_list = [this_script.fileno()]
#Prepare ancillary data
@devilholk
devilholk / af_unix_recv_fd.py
Created August 16, 2020 09:25
Test of receiving file descriptors from another process using AF_UNIX
AUTO_DELETE = False #Change this to True to delete the socket before usage in case it was never cleaned up for some reason
import socketserver, os, struct
from socket import *
class my_handler(socketserver.StreamRequestHandler):
def handle(self):
print('New request!', self.request)
while True:
msg, ancdata, flags, addr = self.request.recvmsg(1024, 1024)
@devilholk
devilholk / uggly_bt_snoop.py
Last active August 30, 2020 23:02
Really uggly hack to get bluetooth snoop data from non rooted android. Will output raw data, pipe to hexeditor or file location.
import subprocess, base64, sys, time, struct, zlib
debug = False
#added stuff from https://android.googlesource.com/platform/system/bt/+/master/tools/scripts/btsnooz.py
def get_snoop():
p = subprocess.Popen(('adb', 'shell', 'dumpsys', 'bluetooth_manager'), stdout=subprocess.PIPE)
stdout, stderr = p.communicate()
extract = False
@devilholk
devilholk / charge_control.py
Last active September 23, 2020 10:50
Script that controls an external circuit to keep the laptops battery voltage within nominal voltage. The DTR signal is used to control the circuit.
#/sys/bus/acpi/drivers/battery/PNP0C0A:00/power_supply/BAT0
#See https://i.imgur.com/yuVyiW7.png for picture of the böp this controls
import sys, serial, time
min_voltage = 3.5
max_voltage = 3.8
interval = 300 #Every 5 minutes
def read_num(filename):
with open(filename, 'r') as infile:
@devilholk
devilholk / fs_uuid_devnode.py
Last active September 25, 2020 12:42
Get filesystem ID from device node
import ctypes
lib = ctypes.CDLL('libblkid.so')
lib.blkid_new_probe_from_filename.argtypes = (ctypes.c_char_p,)
lib.blkid_new_probe_from_filename.restype = ctypes.c_void_p
lib.blkid_free_probe.argtypes = (ctypes.c_void_p,)
lib.blkid_do_probe.argtypes = (ctypes.c_void_p,)