Skip to content

Instantly share code, notes, and snippets.

View santa4nt's full-sized avatar
💭
😬

Santoso santa4nt

💭
😬
  • Los Angeles, CA
View GitHub Profile
@santa4nt
santa4nt / meta.py
Created February 15, 2011 23:38
A module that defines metaclasses for slotted "info" data types.
# -*- coding: utf-8 -*-
"""A module that defines metaclasses for slotted "info" data types.
"""
class InfoMeta(type):
"""A metaclass for classes that declaratively defines fields in
advance.
@santa4nt
santa4nt / test_testgen.py
Created March 3, 2011 03:17
A metaclass to generate test_{name}_{[true,false]} methods from _test_{name}.
#!/usr/bin/env python
import unittest
class TestMaker(type):
"""For a class to assign its __metaclass__ to this type, the class
creation will look for methods that are prefixed with '_test{name}'.
The method is assumed to take two arguments (self and a boolean).
From that, two methods will be dynamically generated, with the names
@santa4nt
santa4nt / checkdecorator.py
Created May 27, 2011 18:17
A decorator to check a method's object attribute before executing its body.
def check(attr):
def _check_fn(fn):
"""Use this function as a method decorator. When decorated with this, a
method will only execute its body when its associated object's attribute
is defined (evaluates to True).
"""
def meth(self, *args, **kwargs):
if getattr(self, attr, None):
return fn(self, *args, **kwargs)
@santa4nt
santa4nt / wintime.py
Created June 2, 2011 18:06
Demonstrate usage of ctypes Structure to synthesize a C struct
from ctypes import *
from ctypes.wintypes import *
class SYSTEMTIME(Structure):
"""See: WinBase.h"""
_fields_ = [
('wYear', WORD),
('wMonth', WORD),
('wDayOfWeek', WORD),
@santa4nt
santa4nt / scapy_frag_icmpv6.py
Created July 12, 2012 00:13
Send a fragmented ICMPv6 Echo Request with scapy
from scapy.all import *
# XXX: make sure conf.route6 includes the target IPv6 address in its route table;
# you can do so by first calling ping6 so that a neighbor solicitation is done
sip = 'fe80::20c:29ff:fe67:22c2'
dip = 'fe80::700d:da7e:80bb:aca9'
packets = fragment6(IPv6(src=sip, dst=dip) / IPv6ExtHdrFragment() / ICMPv6EchoRequest(data='A'*5000), 1024)
send(packets)
@santa4nt
santa4nt / decrypt_aes_cbc.py
Created November 29, 2012 22:58
A toy decryption algorithm using AES in CBC mode, with PKCS5 padding scheme.
from Crypto.Cipher import AES, XOR # from pycrypto library (https://www.dlitz.net/software/pycrypto/)
block_size = AES.block_size
def decrypt(key, ciphertext):
# assume: len(key) == 16 bytes; PKCS5 padding scheme; len(IV) == 16 bytes
iv = ciphertext[:block_size]
ciph = ciphertext[block_size:]
@santa4nt
santa4nt / decrypt_aes_ctr.py
Created November 29, 2012 22:59
A toy decryption algorithm using AES in CTR mode.
from Crypto.Cipher import AES, XOR # from pycrypto library (https://www.dlitz.net/software/pycrypto/)
block_size = AES.block_size
def decrypt(key, ciphertext):
# assume: len(key) == len(IV) == 16 bytes; no padding
iv = ciphertext[:block_size]
ciph = ciphertext[block_size:]
@santa4nt
santa4nt / streamhash.py
Created November 30, 2012 19:40
An algorithm to calculate a chained hashing schema suitable for streamed file chunks.
# -*- coding:utf-8 -*-
"""An algorithm to calculate a hashing schema described in:
https://class.coursera.org/crypto-004/quiz/feedback?submission_id=193325
"""
import io
import sys
import hashlib
@santa4nt
santa4nt / arp_scapy.py
Created February 28, 2013 19:23
Some quick-n-dirty sample code and functions to manipulate ARP packets (for network filter testing).
from scapy.all import *
# change this to your test machine's MAC address
SELF_MAC = '00:0c:29:67:22:c2'
BCAST_MAC = 'ff:ff:ff:ff:ff:ff'
# this will send a PROBE ARP request packet to the supplied IP address argument
@santa4nt
santa4nt / .profile
Created March 15, 2013 22:49
A .profile file
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022