Skip to content

Instantly share code, notes, and snippets.

@kai5263499
kai5263499 / http_traffic.md
Last active October 30, 2017 19:02
Capturing http traffic
apt-get -y install ngrep tcpflow httpry scapy tshark

httpry

ngrep -l -q -d eth0 "^GET|^POST " tcp and port 80

tcpflow -p -c -i eth0 port 80 | grep -oE '(GET|POST|HEAD) .* HTTP/1.[01]|Host: .*'

tcpdump -s 0 -A 'tcp dst port 80 and (tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x504f5354)'
@kai5263499
kai5263499 / package_management.sh
Created October 24, 2017 18:36
Debian package management
# Find packages that depend on PACKAGE
apt-cache rdepends PACKAGE
# Find packages that PACKAGE depends on
apt-cache depends PACKAGE
# Find packages that provide FILE
dpkg -S FILE
apt-file find FILE
@kai5263499
kai5263499 / mix_with_ratio.py
Created October 1, 2017 21:41
Mix lists with a given ratio
# inspired by https://stackoverflow.com/questions/10644925/randomly-interleave-2-arrays-in-python
def mix_with_ratio(a, b, ratio):
c = []
while a and b:
which_list = random.random()
if which_list < ratio:
c.append(a.pop(0))
else:
c.append(b.pop(0))
@kai5263499
kai5263499 / chunky.py
Created September 28, 2017 11:08
Split a list into equally sized chunks
# from https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in xrange(0, len(l), n):
yield l[i:i + n]
@kai5263499
kai5263499 / timeout.py
Created September 27, 2017 16:22
Timeout python function
class TimeoutError(Exception):
pass
class timeout:
def __init__(self, seconds=1, error_message='Timeout'):
self.seconds = seconds
self.error_message = error_message
def handle_timeout(self, signum, frame):
raise TimeoutError(self.error_message)
def __enter__(self):
@kai5263499
kai5263499 / ratelimit.py
Created September 27, 2017 15:41
Python rate limit
def RateLimited(maxPerSecond):
minInterval = 1.0 / float(maxPerSecond)
def decorate(func):
lastTimeCalled = [0.0]
def rateLimitedFunction(*args,**kargs):
elapsed = time.clock() - lastTimeCalled[0]
leftToWait = minInterval - elapsed
if leftToWait>0:
time.sleep(leftToWait)
ret = func(*args,**kargs)
@kai5263499
kai5263499 / interface_check.go
Created September 5, 2017 03:54
Example of checking whether a struct fulfills an interface dynamically at runtime
package main
import (
"fmt"
"reflect"
)
type A interface {
Set(string) error
}
@kai5263499
kai5263499 / simpleservice
Created August 4, 2017 02:44
Simple service daemon
#!/bin/bash
# myapp daemon
# chkconfig: 345 20 80
# description: myapp daemon
# processname: myapp
DAEMON_PATH="/home/wes"
DAEMON=myapp
DAEMONOPTS="-my opts"