Create scripts/transformers/Mordor-Transform.py
Browse files
scripts/transformers/Mordor-Transform.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import logging
|
| 5 |
+
import copy
|
| 6 |
+
import time
|
| 7 |
+
import re
|
| 8 |
+
import yaml
|
| 9 |
+
import sys
|
| 10 |
+
|
| 11 |
+
# Initial description
|
| 12 |
+
text = 'This script allows you to replace values in a mordor file to make it look as if it occurred in your own network'
|
| 13 |
+
example_text = f'''Examples:
|
| 14 |
+
python3 {sys.argv[0]} -f mordor_file_2019-10-10221033.json -t 2020-12-18102055
|
| 15 |
+
python3 {sys.argv[0]} -f mordor_file_2019-10-10221033.json -t 2020-12-18102055 -m string-mappings.yml
|
| 16 |
+
'''
|
| 17 |
+
|
| 18 |
+
# Initiate the parser
|
| 19 |
+
parser = argparse.ArgumentParser(description=text,epilog=example_text,formatter_class=argparse.RawDescriptionHelpFormatter)
|
| 20 |
+
|
| 21 |
+
# Add arguments (store_true means no argument needed)
|
| 22 |
+
parser.add_argument("-f", "--file", help="file to customize and send", type=str , required=True)
|
| 23 |
+
parser.add_argument("-t", "--timestamp", help="new timestamp from where to start. Format YYYY-MM-DDHHMMSS", type=lambda d: datetime.strptime(d, '%Y-%m-%d%H%M%S'), required=True)
|
| 24 |
+
parser.add_argument("-m", "--mappings-file", help="YAML file with string mappings in a dictionary format to replace string values", type=str , required=False)
|
| 25 |
+
parser.add_argument("-d", "--debug", help="Print lots of debugging statements", action="store_const", dest="loglevel", const=logging.DEBUG, default=logging.WARNING)
|
| 26 |
+
parser.add_argument("-v", "--verbose", help="Be verbose", action="store_const", dest="loglevel", const=logging.INFO)
|
| 27 |
+
|
| 28 |
+
args = parser.parse_args()
|
| 29 |
+
|
| 30 |
+
logging.basicConfig(level=args.loglevel, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
|
| 31 |
+
log = logging.getLogger(__name__)
|
| 32 |
+
|
| 33 |
+
log.debug(f'Started Stopwatch')
|
| 34 |
+
time_start = time.time()
|
| 35 |
+
|
| 36 |
+
# Read mordor file json
|
| 37 |
+
print("[+] Old Mordor File:",args.file)
|
| 38 |
+
log.debug(f'Reading {args.file} to dataframe..')
|
| 39 |
+
df = pd.read_json(args.file, lines=True)
|
| 40 |
+
|
| 41 |
+
# If string mappings are defined to replace
|
| 42 |
+
if args.mappings_file:
|
| 43 |
+
log.info('Replacing strings..')
|
| 44 |
+
log.debug(f'Reading {args.mappings_file} to dictionary..')
|
| 45 |
+
replace_dict = yaml.safe_load(open(args.mappings_file).read())
|
| 46 |
+
log.debug(f'Replacing the following: {replace_dict}..')
|
| 47 |
+
df = df.replace(replace_dict, regex = True)
|
| 48 |
+
|
| 49 |
+
# Pick earliest time
|
| 50 |
+
log.info('Calculating earliest timestamp')
|
| 51 |
+
earliest_timestamp = df['@timestamp'].min()
|
| 52 |
+
earliest_timestamp = datetime.strptime(earliest_timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')
|
| 53 |
+
log.debug(f'Earliest timestamp: {earliest_timestamp}')
|
| 54 |
+
|
| 55 |
+
# Calculate Delta
|
| 56 |
+
log.info('Calculating time delta..')
|
| 57 |
+
deltatime = args.timestamp - earliest_timestamp
|
| 58 |
+
log.debug(f'Time Delta: {deltatime}')
|
| 59 |
+
|
| 60 |
+
# Replace timestamp column
|
| 61 |
+
log.info('Replacing timestamp values..')
|
| 62 |
+
df['@timestamp'] = pd.to_datetime(df['@timestamp']) + deltatime
|
| 63 |
+
|
| 64 |
+
# Set Timestamp column as string
|
| 65 |
+
log.info('Updating timestamp type from datetime to string with specific format..')
|
| 66 |
+
df['@timestamp'] = df['@timestamp'].dt.strftime('%Y-%m-%dT%H:%M:%S.%f').str[:-3]+'Z'
|
| 67 |
+
|
| 68 |
+
# Getting new name of file
|
| 69 |
+
log.info('Defining new file name..')
|
| 70 |
+
log.debug(f'Old file name: {args.file}')
|
| 71 |
+
old_file_name = copy.deepcopy(args.file)
|
| 72 |
+
old_file_name = re.split('\d{4}-\d{2}-\d{8}.json',old_file_name)[0]
|
| 73 |
+
timestamp_input_string = args.timestamp.strftime('%Y-%m-%d%H%M%S')
|
| 74 |
+
log.debug(f'New file name: {old_file_name}{timestamp_input_string}.json')
|
| 75 |
+
new_file_name = f'{old_file_name}{timestamp_input_string}.json'
|
| 76 |
+
|
| 77 |
+
# Write event dictionaries from a list to a json file
|
| 78 |
+
log.info('Exporting dataframe to JSON')
|
| 79 |
+
df.apply(lambda x: x.dropna().to_dict(), axis=1).to_json(new_file_name,orient='records', lines=True)
|
| 80 |
+
|
| 81 |
+
# Final time
|
| 82 |
+
time_end = time.time()
|
| 83 |
+
total_time = time_end - time_start
|
| 84 |
+
log.debug(f'Stopped Stopwatch')
|
| 85 |
+
|
| 86 |
+
# Final stats
|
| 87 |
+
print("[+] Time taken:",total_time,"seconds")
|
| 88 |
+
print("[+] New Mordor File:",new_file_name)
|