Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View sbz's full-sized avatar

Sofian Brabez sbz

View GitHub Profile
@sbz
sbz / retrieve-unassigned-objects.py
Created November 18, 2010 12:40
retrieve unassigned objects using gc module in python
import socket
socket.create_connection(('127.0.0.1', 22))
<socket._socketobject object at 0x9022bc4>
s=socket.create_connection(('127.0.0.1', 22))
import gc
[ i for i in gc.get_objects() if type(i) == type(s) ]
[<socket._socketobject object at 0x9022bc4>, <socket._socketobject object at 0x92a4aac>]
t = [ i for i in gc.get_objects() if type(i) == type(s) ][0]
t
<socket._socketobject object at 0x9022bc4>
@sbz
sbz / addr.c
Created July 13, 2011 12:53
retrieve INET4 interfaces address using getifaddrs(3)
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
int
@sbz
sbz / hexdump.py
Created July 13, 2011 13:04
hexdump implementation in Python
def hexdump(src, length=16):
FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or '.' for x in range(256)])
lines = []
for c in xrange(0, len(src), length):
chars = src[c:c+length]
hex = ' '.join(["%02x" % ord(x) for x in chars])
printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or '.') for x in chars])
lines.append("%04x %-*s %s\n" % (c, length*3, hex, printable))
return ''.join(lines)
@sbz
sbz / stackgrow.c
Created July 13, 2011 13:11
test how system stack is growing
#include <stdio.h>
#include <sys/utsname.h>
inline static void
u() {
struct utsname uts;
uname(&uts);
printf("%s %s %s %s %s", uts.sysname, uts.nodename, uts.release, uts.version, uts.machine);
@sbz
sbz / xsock.c
Created July 13, 2011 13:53
create a unix socket on X11 display in C
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#define _PATH_UNIX_X "/tmp/.X11-unix/X%d"
#define DISPLAY_PORT 0
@sbz
sbz / xsock.py
Created July 13, 2011 14:05
create a unix socket on X11 display in Python
#!/usr/bin/env python
import time
import socket
_PATH_UNIX_X = "/tmp/.X11-unix/X%d"
DISPLAY_PORT = 0
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(_PATH_UNIX_X % DISPLAY_PORT)
@sbz
sbz / logit.py
Created July 13, 2011 14:43
generic logit method in order to log functions calling with arguments
import inspect
import logging
logger = logging.getLogger('application')
def logit(order, *largs, **kwargs):
return True
__line__ = int(inspect.stack()[1][2])
__function__ = inspect.stack()[1][3]
with_color=False
@sbz
sbz / lcap.c
Last active January 26, 2024 14:33
example of using linux capabilities interface libcap(3) and dump capabilities flags for the running process
#include <sys/capability.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define nitems(x) (sizeof(x) / sizeof(x[0]))
int
main(void) {
@sbz
sbz / list-sections.c
Created August 31, 2012 10:42
example to dump sections name using gelf(3) api on FreeBSD
#include <err.h>
#include <fcntl.h>
#include <gelf.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
static void
@sbz
sbz / pf_getrules.c
Last active May 25, 2018 16:11
pf: ioctl get rules call debugging
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#ifdef __FreeBSD__
#include <sys/endian.h>
#endif
#include <net/if.h>
#include <net/pfvar.h>