-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgivemelogons.py
181 lines (139 loc) · 5.6 KB
/
givemelogons.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
#!/usr/bin/python3
import csv
import sys
import re
import argparse
import os
# open a csv file with the open function
#ID 4768 - TGT was granted (successfull domain auth)
# options:
# domain logins
# local logins
def dump_rdp_successful_logins(infile, writer):
count = 0
logins = 0
result = []
if args.verbose:
header = ["Event ID", "Timestamp", "Username", "Domain", "Source Address", "Message"]
else:
header = ["Event ID", "Timestamp", "Username", "Domain", "Source Address"]
if not args.append:
writer.writerow(header)
with open(infile, newline='') as csvfile:
# create an iterator object with the csv.reader() function
dict_reader = csv.DictReader(csvfile)
# iterate over the reader object
for row in dict_reader:
count += 1
if row["Event ID"] == "1149":
logins += 1
username = re.search(r'User:(.*)', row[None][0]).group(1).strip()
domain = re.search(r'Domain:(.*)', row[None][0]).group(1).strip()
source = re.search(r'Source Network Address:(.*)', row[None][0]).group(1).strip()
message = row[None][0]
timestamp = row["Date and Time"]
eventID = "1149"
if args.verbose:
writer.writerow([eventID, timestamp, username, domain, source, message])
else:
writer.writerow([eventID, timestamp, username, domain, source])
print(f"Found {str(count)} records")
print(f"Found {str(logins)} successfull rdp authentications")
def dump_kerb_auth_events(infile, writer):
count = 0
logins = 0
result = []
if args.verbose:
header = ["Event ID", "Timestamp", "Username", "Domain", "Source Address", "Message"]
else:
header = ["Event ID", "Timestamp", "Username", "Domain", "Source Address"]
if not args.append:
writer.writerow(header)
with open(infile, newline='') as csvfile:
# create an iterator object with the csv.reader() function
dict_reader = csv.DictReader(csvfile)
# iterate over the reader object
for row in dict_reader:
count += 1
if row["Event ID"] == "4768" and "$" not in row[None][0]:
logins += 1
username = re.search(r'Account Name:(.*)', row[None][0]).group(1).strip()
domain = re.search(r'Realm Name:(.*)', row[None][0]).group(1).strip()
source = re.search(r'Client Address:(.*)', row[None][0]).group(1).strip()
message = row[None][0]
timestamp = row["Date and Time"]
eventID = "4768"
if args.verbose:
writer.writerow([eventID, timestamp, username, domain, source, message])
else:
writer.writerow([eventID, timestamp, username, domain, source])
print(f"Found {str(count)} records")
print(f"Found {str(logins)} successfull kerberos authentication events")
def dump_login_auth_events(infile, writer):
count = 0
logins = 0
result = []
if args.verbose:
header = ["Event ID", "Timestamp", "Username", "Domain", "Source Address", "Message"]
else:
header = ["Event ID", "Timestamp", "Username", "Domain", "Source Address"]
if not args.append:
writer.writerow(header)
with open(infile, newline='') as csvfile:
# create an iterator object with the csv.reader() function
dict_reader = csv.DictReader(csvfile)
# iterate over the reader object
for row in dict_reader:
count += 1
if row["Event ID"] == "4624" and "$" not in row[None][0]:
logins += 1
username = re.findall(r'Account Name:(.*)', row[None][0])[1].strip()
domain = re.findall(r'Account Domain:(.*)', row[None][0])[1].strip()
source = re.search(r'Source Network Address:(.*)', row[None][0]).group(1).strip()
message = row[None][0]
timestamp = row["Date and Time"]
eventID = "4624"
if args.verbose:
writer.writerow([eventID, timestamp, username, domain, source, message])
else:
writer.writerow([eventID, timestamp, username, domain, source])
print(f"Found {str(count)} records")
print(f"Found {str(logins)} successfull user authentication events")
def main():
global args
USAGE = f"""
Take in a csv formatted event log and output a new csv with specific login events.
Currently works for domain authentication events and local successful rdp events.
rdp logs -> keyword:rdp eventid: 1149
kerberos logs -> keyword:kerb eventid:4768
generic logon logs -> keyword:logons eventid:4624
{sys.argv[0]} domain -i security.csv -o logins.csv
{sys.argv[0]} domain -i security.csv -v
{sys.argv[0]} rdp -i rdp_logs.csv -o rpd_logins.csv
"""
parser = argparse.ArgumentParser(description="",formatter_class=argparse.RawDescriptionHelpFormatter, epilog=USAGE)
parser.add_argument('-v', action="store_true", required=False, dest='verbose',help="output event log message in csv file")
parser.add_argument('-o', type=str, action="store", required=False, default="events.csv",dest='outfile',help="csv save file")
parser.add_argument('-i', type=str, action="store", required=True, dest='infile',help="csv formatted event log")
parser.add_argument('-a', action="store_true", required=False, dest='append',help="append to an already existing csv output file")
parser.add_argument("eventtype", choices=['rdp', 'kerb', 'logons'], help="choose the event type to be used for paring events")
args = parser.parse_args()
if os.path.splitext(args.infile)[1] != ".csv":
print("Not a recognized file type, must be .csv")
sys.exit()
else:
infile = args.infile
if args.append:
file = open(args.outfile, 'a', encoding='utf-8')
else:
file = open(args.outfile, 'w', encoding='utf-8')
writer = csv.writer(file)
if args.eventtype == "kerb":
dump_kerb_auth_events(infile, writer)
elif args.eventtype == "logons":
dump_login_auth_events(infile, writer)
elif args.eventtype == "rdp":
dump_rdp_successful_logins(infile,writer)
file.close()
print("Output File:", args.outfile)
main()