Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finished #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added BIG DATA – LOGS ANALYSIS.pptx
Binary file not shown.
Binary file added detections.docx
Binary file not shown.
89 changes: 89 additions & 0 deletions security_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import csv

def remove_quotes(s):
if s.startswith('"'):
s = s[1:]
if s.endswith('"'):
s = s[:-1]
return s

def remove_colon(s):
if s.endswith(":"):
s = s[:-1]
return s

delim = '\t'

input_filename = 'PC1_security.csv'
output_filename = 'processed_' + input_filename
with open(input_filename) as input_file:
with open(output_filename, "w") as output_file:
csv_reader = csv.reader(input_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
column_names = row[0]
print(f'Column names are {", ".join(row)}')
column_names = column_names.replace('Message',
'"Message";"Process ID";"Application Name";"Network Information";"Direction";"Source Address";"Source Port";"Destination Address";"Destination Port";"Protocol";"Filter Information";"Filter Run-Time ID";"Layer Name";"Layer Run-Time ID"')
without_quotes = []
for col_name in column_names.split(';'):
without_quotes.append(remove_quotes(col_name))
column_names = delim.join(without_quotes)
output_file.write(column_names + '\n')
line_count += 1
else:
line_d = {}
message_keys = '"Message";"Process ID";"Application Name";"Network Information";"Direction";"Source Address";"Source Port";"Destination Address";"Destination Port";"Protocol";"Filter Information";"Filter Run-Time ID";"Layer Name";"Layer Run-Time ID"'.split(';')
message_keys_list = []
for key in message_keys:
key = remove_quotes(key)
message_keys_list.append(key)
line_d[key] = ''
#print(row)
r = row[0]
#print(len(r.split(';')))
#print(r.split(';'))

message = r.split(';')[0]
#print(message)
message_values = []
index = 0
for pair in message.strip().split('\n'):
pair_objects = pair.strip().split('\t')
pair_objects = [obj for obj in pair_objects if obj != '']
if len(pair_objects) == 0:
continue
#print('pair_objects_len=', len(pair_objects), pair_objects)

value = ''
if len(pair_objects) == 2:
key = remove_quotes(pair_objects[0])
key = remove_colon(key)
line_d[key] = pair_objects[1]
elif len(pair_objects) == 1 and (not pair_objects[0].lower().endswith('information:') or index == 0):
line_d["Message"] = pair_objects[0]
index += 1

message_values = []
for message_key in message_keys_list:
message_values.append(line_d[message_key])
concatenated_message_values = ';'.join(message_values)
#print(concatenated_message_values)
concatenated_values = concatenated_message_values + ';' + ';'.join(r.split(';')[1:])

without_quotes = []
for value in concatenated_values.split(';'):
without_quotes.append(remove_quotes(value))
concatenated_values = delim.join(without_quotes)
output_file.write(concatenated_values + '\n')


line_count += 1

#if line_count == 100:
# print("Exiting after sample lines...")
# break

#print(column_names)
print(f'Processed {line_count} lines.')
118 changes: 118 additions & 0 deletions sysmon_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import csv

def remove_quotes(s):
if s.startswith('"'):
s = s[1:]
if s.endswith('"'):
s = s[:-1]
return s

def remove_colon(s):
if s.endswith(":"):
s = s[:-1]
return s

def remove_quotes_and_colon(strs):
return [remove_quotes(remove_colon(s)) for s in strs]

LINES_LIMIT = -1
delim = '\t'

def get_all_columns(filename):
with open(filename) as input_file:
csv_reader = csv.reader(input_file, delimiter=';')
line_count = 0
message_keys = {}
for row in csv_reader:
if line_count == 0:
column_names = remove_quotes_and_colon(row[0].split(";"))
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
message = row[0]
message_parts = message.strip().split('\n')
for part in message_parts:
if (part.strip() == ''):
continue

del_pos = part.find(': ')
key = part.strip()[:del_pos]
key = remove_quotes(key)
key = remove_colon(key)
message_keys[key] = ''

line_count += 1

if line_count == LINES_LIMIT:
print("Exiting after sample lines...")
break

message_keys_list = ['Message'] + list(message_keys.keys())
message_keys_list = sorted(message_keys_list)
message_columns = delim.join(message_keys_list)
column_names = delim.join(column_names)
all_columns = column_names.replace('Message', message_columns)
return all_columns, message_keys_list


input_filename = 'PC6_sysmon.csv'
output_filename = 'processed_' + input_filename



with open(input_filename) as input_file:
all_columns, message_keys_list = get_all_columns(input_filename)
all_columns_list = all_columns.split(';')

with open(output_filename, "w") as output_file:
output_file.write(all_columns + '\n')

csv_reader = csv.reader(input_file, delimiter=';')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
line_d = {}
for key in message_keys_list:
key = remove_quotes(key)
line_d[key] = ''

message = row[0]
message_parts = message.strip().split('\n')
index = 0
for part in message_parts:
if (part.strip() == ''):
continue

del_pos = part.find(': ')
if index == 0:
key = 'Message'
else:
key = part.strip()[:del_pos]
value = part.strip()[del_pos + 1:].strip()
line_d[key] = value
index += 1

message_values = []
for message_key in message_keys_list:
message_values.append(line_d[message_key])
concatenated_message_values = delim.join(message_values)
#print(concatenated_message_values)
concatenated_values = concatenated_message_values + delim + delim.join(row[1:])

"""without_quotes = []
for value in concatenated_values.split(';'):
without_quotes.append(remove_quotes(value))
concatenated_values = ';'.join(without_quotes)"""
output_file.write(concatenated_values + '\n')

line_count += 1

if line_count == LINES_LIMIT:
print("Exiting after sample lines...")
break

#print(column_names)
print(f'Processed {line_count} lines.')