Skip to content

Instantly share code, notes, and snippets.

@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 / static_linked_list_demo.cpp
Created September 13, 2012 02:58
Simple example of creating a static 'Linked list' in C++
struct node{
// A node has two components: some data and a pointer to the next node.
int node_data;
node* next;
};
int main(){
// Make some nodes:
node A,B,C;
@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 / python_md5.py
Created November 27, 2012 17:51
MD5 length-extension, as described in Thai Duong's Flickr API attack. Based on http://www.huyng.com/posts/dont-hash-your-secrets-heres-why-in-python/
"""
MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
rights reserved.
License to copy and use this software is granted provided that it
is identified as the "RSA Data Security, Inc. MD5 Message-Digest
Algorithm" in all material mentioning or referencing this software
or this function.
@moshekaplan
moshekaplan / lcs.py
Created December 16, 2012 04:47
Sample code for recursively calculating the longest-common subsequence.
def lcs(str1, str2):
# If either string is empty, stop
if len(str1) == 0 or len(str2) == 0:
return ""
# First property
if str1[-1] == str2[-1]:
return lcs(str1[:-1], str2[:-1]) + str1[-1]
# Second proprerty
def chunks(chunkable, n):
""" Yield successive n-sized chunks from l.
"""
for i in xrange(0, len(chunkable), n):
yield chunkable[i:i+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)