Skip to content

Instantly share code, notes, and snippets.

import traceback
import logging
def get_exception_info(skip=2):
"""Note, this relies on being called from the exception handler. This is very brittle to the depth of the call."""
frames_output = []
for frame in traceback.extract_stack()[:-skip]:
fname, lineno, parent, function = frame
frame_output = """ File "%s", line %d, in %s\n %s""" % (fname, lineno, parent, function)
frames_output.append(frame_output)
#!/usr/bin/env python
"""
Given the result of a multiplication and one of the factors, calculate the
other factor
Arguments:
known_factor = the factor we're given
result = the result of the multiplication modulo some number
size_of_modulo_result = The number of bits in the result
@moshekaplan
moshekaplan / merge_dicts.py
Created August 17, 2014 15:27
Merge Python dictionaries
def merge_dicts(*dicts):
all_items = []
for dictionary in dicts:
all_items += dictionary.items()
return dict(all_items)
@moshekaplan
moshekaplan / get_public_ip.py
Last active October 10, 2015 15:27
Get Public IP
#! /usr/bin/env python
"""
Includes a function for retrieving a public IP in a simple, programmatic fashion.
NOTE: Does not use HTTPS!
"""
import urllib2
def get_public_ip():
"""Retrieve the external IP address.
@moshekaplan
moshekaplan / pre_and_post_increment.cpp
Created September 13, 2012 03:07
Pre and Post increment
#include <iostream>
using namespace std;
int preincrement(int & x){
// Add one to x, and return the new value for x.
x = x+1;
return x;
}
int postincrement(int & x){
@moshekaplan
moshekaplan / efficient_exponentiation.py
Created October 14, 2012 15:37
Efficient exponentiation
def exp(x,n):
"""Returns x^n, for integers x,n and n>=0
This is done more intelligently, as we can use the definition
that x^(a/2)^2 == x^a to make many less multiplications"""
# count is the amount of multiplications.
count = 0
result = 1
x_raised = x
# On each iteration, multiply our result by x^a if x^a is needed to compute x^n.
@moshekaplan
moshekaplan / ftp_upload.py
Last active December 12, 2015 08:09
This python (2.7) script uploads all of the files in the working directory to a remote FTP server. It's meant to be used for removing files from a remote system once you have the ability to execute a single command. This was written for 29c3 ctf's minesweeper challenge.
#! /usr/bin/env python
# Uploads every file in the directory to the specified FTP server
import os
import os.path
from ftplib import FTP
HOST = "127.0.0.1"
PORT = 21
@moshekaplan
moshekaplan / minesweeper_solver.py
Created February 10, 2013 06:40
Code to solve 29c3ctf's minesweeper challenge.
"""
Steps:
1) Save the game before anything happens
2) Solve the game to get the list of mines, as y,x coordinates
3) Reconstruct the savegame
4) XOR the decoded savegame and the reconstructed save game to get the key
5) Build an exploit to steal the entire key.
6) Build an exploit to grab the flag
"""
@moshekaplan
moshekaplan / matchTemplate_example.py
Created March 4, 2013 06:57
Python OpenCV example (2.3.1) - matchTemplate
import cv2
from cv2 import cv
method = cv.CV_TM_SQDIFF_NORMED
template_name = "mozicon128.png"
image_name = "test2.jpeg"
# Load
needle = cv2.imread(template_name)
@moshekaplan
moshekaplan / get_code.py
Created April 5, 2013 11:20
Simple salting to generate x-digit codes.
import hashlib
salt = "This is a super secret salt. Nobody should ever be able to guess this.6rAtas7swe9ach6rAtas7swe9achXrAtas7swe9ach6rAtas7swe9ach6rAtas7swe9ach6rAtas7swe9ach"
def get_code(number, length):
"""Returns the first length bytes of the generated 'code'"""
return hashlib.sha512(str(number) + str(salt)).hexdigest()[:length]
for i in range(10):
print get_code(i, 7)