Skip to content

Instantly share code, notes, and snippets.

@berdario
berdario / strings.py
Created June 29, 2015 15:58
the "strings" command implemented in Python
from mmap import mmap, PROT_READ
import re
import sys
def strings(fname, n=6):
with open(fname, 'rb') as f, mmap(f.fileno(), 0, prot=PROT_READ) as m:
for match in re.finditer(('([\w/]{%s}[\w/]*)' % n).encode(), m):
yield match.group(0)
if __name__ == '__main__':
@berdario
berdario / _utils.py
Last active February 21, 2023 13:29
A simple python hook system that doesn't mutate global variables and tries to be as side-effect free as possible
from __future__ import print_function
import sys
from collections import namedtuple
from imp import load_module, find_module
from pathlib import Path
Hooks = namedtuple('Hooks', 'preactivate postactivate get_env_details postrmvirtualenv')
_HOOKS = Hooks._fields
@berdario
berdario / dummyhttp.py
Created December 7, 2014 21:22
A toy HTTP server in pure Python3 I wrote in September 2013
#! /usr/bin/env python
import sys
import os
import locale
from collections import defaultdict
from socket import socket, AF_INET, SOCK_STREAM
from urllib.parse import urlparse
from contextlib import closing
from functools import wraps
@berdario
berdario / Transpose.java
Created September 3, 2018 07:43
Java type errors can be nasty
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import static java.util.stream.Collectors.toMap;
class Transpose{
static <A, B> HashMap<B, HashSet<A>> transposeMap(HashMap<A, B> map){
return map.entrySet().stream().collect(toMap(e -> e.getValue(), e -> new HashSet<>(Set.of(e.getKey())), (x, y) -> {x.addAll(y); return x;}));
}
}
@berdario
berdario / fibs.hs
Created September 3, 2018 08:19
expansion of a recursive fibs impl
scanl f q ls = q : (case ls of
[] -> []
x:xs -> scanl f (f q x) xs)
fibs = 1 : scanl (+) 1 fibs
1 : 1 : (case fibs of
[] -> []
x:xs -> scanl (+) ((+) 1 x) xs)
# fibs is not []
# x:xs becomes 1:fibs
@berdario
berdario / obfuscate_curl.py
Created August 19, 2018 20:35
http://www.lambdashell.com/ uses `command.includes('curl')` to blacklist curl, such a check can be bypassed by piping the output of this script into sh
from random import choices, seed
from sys import argv
payload = b'''curl -v -X POST -H 'Content-Type: application/json' -d '{command: "echo \\"place here another CURL to a server that will return a newly encoded cmd to tie the knot\\""}' https://yypnj3yzaa.execute-api.us-west-1.amazonaws.com/dev'''
seed(argv[1])
obfuscation_key = choices(range(256), k=len(payload))
obfuscated_payload = [x ^ y for x, y in zip(payload, obfuscation_key)]
#!/usr/bin/env stack
-- stack --install-ghc runghc --package turtle
{-# LANGUAGE OverloadedStrings #-}
import Turtle
import Data.Text (intercalate)
@berdario
berdario / perf.js
Created May 14, 2017 23:29
Javascript sum types performance test
function B (tag, value0, value1, value2, value3, value4) {
this.tag = tag;
this.value0 = value0;
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
this.value4 = value4;
}
#!/usr/bin/env stack
-- stack runghc --resolver lts-7.19 --install-ghc --package turtle -- -Wall
-- install stack with "curl -sSL https://get.haskellstack.org/ | sh"
-- create an AWS image with `env AWS_PROFILE=YOUR_PROFILE packer build -var 'stackage=lts-7.19' -var 'base_ami=ami-aaaaaaaa' bisect_tests.packer.json`
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad.Trans.Maybe (MaybeT (..), runMaybeT)
* Checking your package using psc-publish...
There is a problem with your package, which meant that it could not be
published.
Details:
Compile error: