Created
September 22, 2014 08:33
-
-
Save rekkusu/ec48cedab4b98a23a246 to your computer and use it in GitHub Desktop.
CSAW CTF 2014 bin300(2) weissman
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
import struct | |
from pwn import * | |
def extract(fname): | |
f = open(fname, 'rb') | |
head = header(f) | |
for i in range(head[2]): | |
fhead = file_header(f) | |
extracted = file_extract(f, fhead[1]) | |
open(fhead[3], 'w+').write(extracted) | |
print 'extracted %s' % fhead[3] | |
def header(f): | |
magic = f.read(8) | |
version = u32(f.read(4)) | |
num_files = u32(f.read(4)) | |
return (magic, version, num_files) | |
def file_header(f): | |
magic = f.read(4) | |
compressed_size = u32(f.read(4)) | |
uncompressed_size = u32(f.read(4)) | |
filename = f.read(32).strip('\0') | |
return (magic, compressed_size, uncompressed_size, filename) | |
def file_extract(f, compressed_size): | |
extracted = '' | |
c = 0 | |
counter = 0 | |
while c < compressed_size: | |
chunk = u8(f.read(1)) | |
c += 1 | |
if chunk == 0x13: | |
buf = f.read(9) | |
extracted += buf | |
c += 9 | |
#print '%x chunk[%02x]: %s (%d)' % (counter, chunk, buf, c - 10) | |
else: | |
buf = u16(f.read(2)) | |
extracted += '\0' * (chunk / 2) | |
c += 2 | |
#print '%x chunk[%02x]: %s (%d)' % (counter, chunk, hex(buf), c - 3) | |
counter += 1 | |
return extracted | |
extract('./weissman.csawlz') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment