forked from ctfs/write-ups-2014
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminer.py
executable file
·88 lines (72 loc) · 2.29 KB
/
miner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
# coding=utf-8
import hashlib
import os
import subprocess
import sys
import time
if len(sys.argv) < 3:
print """Usage: ./miner.py <clone_url> <public_username>
Arguments:
<clone_url> is the string you’d pass to `git clone` (i.e.
something of the form `username@hostname:path`)
<public_username> is the public username provided to you in
the CTF web interface."""
sys.exit(1)
clone_spec = sys.argv[1]
public_username = sys.argv[2]
def solve():
# Start with a number with lots of digits so that the length of the commit
# object can be predicted and is unlikely to ever increase (because we’ll
# _probably_ have found a coin by then).
nonce = 1000000000000000 # length=16
#difficulty = '000001'
with open('difficulty.txt', 'r') as f:
difficulty = f.read().strip()
tree = subprocess.check_output(['git', 'write-tree']).strip()
with open('.git/refs/heads/master', 'r') as f:
parent = f.read().strip()
timestamp = int(time.time())
print 'Mining…'
base_hasher = hashlib.sha1()
# The length of all such commit messages is 233, as long as the nonce is 16
# digits long.
header = "commit 233\x00"
base_content = """tree %s
parent %s
author CTF user <[email protected]> 1333333337 +0000
committer CTF user <[email protected]> 1333333337 +0000
Give me a Gitcoin
""" % (tree, parent)
base_hasher.update(header + base_content)
while True:
nonce = nonce + 1
hasher = base_hasher.copy()
noncestr = str(nonce)
hasher.update(noncestr)
content = base_content + noncestr
sha1 = hasher.hexdigest()
#print '>>%s<<' % sha1
if sha1 < difficulty:
with open('commit.txt', 'w') as f:
f.write(content)
print 'Mined a Gitcoin! The SHA-1 is:'
os.system('git hash-object -t commit commit.txt -w')
os.system('git reset --hard %s' % sha1)
break
def prepare_index():
os.system('perl -i -pe \'s/(%s: )(\d+)/$1 . ($2+1)/e\' LEDGER.txt' % public_username)
os.system('grep -q "%s" LEDGER.txt || echo "%s: 1" >> LEDGER.txt' % (public_username, public_username))
os.system('git add LEDGER.txt')
def reset():
os.system('git fetch origin master >/dev/null 2>/dev/null')
os.system('git reset --hard origin/master >/dev/null')
while True:
prepare_index()
solve()
if os.system('git push origin master') == 0:
print 'Success :)'
break
else:
print 'Starting over :('
reset()