Skip to content

Instantly share code, notes, and snippets.

Avatar

Alba Mendez mildsunrise

View GitHub Profile
@mildsunrise
mildsunrise / linux_to_hid.c
Created Jan 13, 2021
Linux keycode to USB HID translation table (see https://alba.sh/keycodes)
View linux_to_hid.c
// Based on "usb_kbd_keycode" from drivers/hid/usbhid/usbkbd.c @ 1a59d1b
// Maps KEY_BACKSLASH into 0x31 (not 0x32)
static const int linux_to_hid[256] = {
0, 41, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 45, 46, 42, 43,
20, 26, 8, 21, 23, 28, 24, 12, 18, 19, 47, 48, 40,224, 4, 22,
7, 9, 10, 11, 13, 14, 15, 51, 52, 53,225, 49, 29, 27, 6, 25,
5, 17, 16, 54, 55, 56,229, 85,226, 44, 57, 58, 59, 60, 61, 62,
63, 64, 65, 66, 67, 83, 71, 95, 96, 97, 86, 92, 93, 94, 87, 89,
90, 91, 98, 99, 0,148,100, 68, 69,135,146,147,138,136,139,140,
@mildsunrise
mildsunrise / hid_keycodes.md
Last active Jan 10, 2021
USB HID keycode table + JSON, extracted from HID Usage Tables v1.21
View hid_keycodes.md

Keyboard/Keypad Page (0x07)

This section is the Usage Page for key codes to be used in implementing a USB keyboard. A Boot Keyboard (84-, 101- or 104-key) should at a minimum support all associated usage codes as indicated in the Boot column below.

The usage type of all key codes is Selectors (Sel), except for the modifier keys Keyboard Left Control (0x224) to Keyboard Right GUI (0x231) which are Dynamic Flags (DV).

Note: A general note on Usages and languages: Due to the variation of keyboards from language to language, it is not feasible to specify exact key mappings for every language. Where this list is not specific for a key

@mildsunrise
mildsunrise / reverse_crc.py
Last active Dec 12, 2020
calculate the seed, given a message and its expected CRC32
View reverse_crc.py
from zlib import crc32
from polynomial_arithmetic import p_mod_mul, p_mult_inv, p_mod_pow
reverse32 = lambda x: int('{:032b}'.format(x)[::-1], 2)
rev_neg_32 = lambda x: reverse32(x ^ ((1 << 32) - 1))
# generic (mathematical version): get the required seed given the actual and expected CRC, the polynomial and message size
get_seed = lambda msgbits, checksum1, checksum2, poly: \
p_mod_mul(checksum1 ^ checksum2, p_mod_pow(p_mult_inv(2, poly), msgbits, poly), poly)
@mildsunrise
mildsunrise / der.ts
Last active Nov 7, 2020
low-level DER encoding / decoding
View der.ts
/**
* Low-level DER parser / encoder
*/
/** */
export interface PrimitiveDerNode {
tagClass: TagClass
tagNumber: bigint
value: Buffer
}
View flatten.py
#!/usr/bin/env python3
from git import Repo, Commit
list_commits = lambda commits: '\n'.join(' {} {}'.format(n.hexsha[:8], n.message.splitlines()[0]) for n in commits)
repo = Repo.init('path to bare clone', bare=True, mkdir=False)
refs = repo.refs
# load whole graph
def visit_graph(roots, get_parents=lambda n: n.parents):
queue = list(roots)
@mildsunrise
mildsunrise / mysql_pcap.py
Created May 7, 2020
Script to extract statistics about MySQL queries in a .pcap.
View mysql_pcap.py
#!/usr/bin/env python3
'''
Script to extract statistics about MySQL queries in a .pcap.
'''
import sys
from subprocess import run
import json, csv, re
args = sys.argv[1:]
if len(args) != 2:
@mildsunrise
mildsunrise / README.md
Last active Mar 17, 2020
Wrapper that simplifies SSH tunnels
View README.md

ssh-from

ssh-from simplifies common usage of SSH tunnels, and makes double (or triple) tunnels a piece of cake.
For instance, it lets me SSH to a computer that is behind two NATs, tunneling through each router.

Installation

Install dependencies (tsocks is optional):

sudo apt-get install python3 lsof tsocks
@mildsunrise
mildsunrise / sona_gif.py
Last active Dec 2, 2020
GIF of chilling Sona
View sona_gif.py
# 1. download video:
# youtube-dl -f 399 https://www.youtube.com/watch?v=hawThTG5No8 -o youtube_video.mkv
# 2. extract important segment:
# ffmpeg -ss 1:08 -to 1:27 -i youtube_video.mkv -c copy -map 0:0 out.mkv
# 3. run this to generate out.gif
from math import tau
import numpy as np
import cv2
import av
@mildsunrise
mildsunrise / parser.py
Created Dec 31, 2019
Parser for Darkest Dungeon saves
View parser.py
from struct import unpack, pack
from sys import argv, stderr
import io
import json
def checked_read(f, amount):
result = f.read(amount)
assert len(result) == amount
return result
View crc32.js
const table = new Int32Array(256).map((_,n) => [...Array(8)]
.reduce(c => (c>>>1) ^ ((c&1) * 0xEDB88320), n))
const fn = (C, b) => (C>>>8) ^ table[(C^b) & 0xFF]
const crc = (x, seed) => ~x.reduce(fn, ~seed)
// Example:
crc( Buffer.from('Hello') ) === 0xF7D18982