|
| 1 | +import boto3 |
| 2 | +import json |
| 3 | +import pprint |
| 4 | +import traceback |
| 5 | +import time |
| 6 | +from cbapi.response.models import BannedHash |
| 7 | +from cbapi.example_helpers import build_cli_parser, get_cb_response_object |
| 8 | + |
| 9 | +processed_list = set() |
| 10 | + |
| 11 | +cb = None |
| 12 | +watchlist_name = None |
| 13 | + |
| 14 | + |
| 15 | +def process_events(data): |
| 16 | + # |
| 17 | + # Split on newline |
| 18 | + # |
| 19 | + for event in data.split('\n'): |
| 20 | + if not event: |
| 21 | + continue |
| 22 | + |
| 23 | + # |
| 24 | + # Load the event as json |
| 25 | + # |
| 26 | + event_json = json.loads(event) |
| 27 | + |
| 28 | + # |
| 29 | + # Check for watchlist event type |
| 30 | + # |
| 31 | + if event_json.get('type', '') == "watchlist.hit.process" or \ |
| 32 | + event_json.get('type', '') == "watchlist.hit.binary": |
| 33 | + # |
| 34 | + # Check if matches our watchlist_name |
| 35 | + # |
| 36 | + md5sum = event_json.get('docs', [])[0].get('process_md5', '') |
| 37 | + if event_json.get('watchlist_name', '').lower() == watchlist_name.lower(): |
| 38 | + print "[+]: Banning Hash: {}".format(md5sum) |
| 39 | + |
| 40 | + try: |
| 41 | + bh = cb.create(BannedHash) |
| 42 | + bh.md5hash = md5sum |
| 43 | + bh.text = "Auto-Blacklist from s3-watchlist-ban.py" |
| 44 | + bh.save() |
| 45 | + print bh |
| 46 | + except Exception as e: |
| 47 | + print e.message |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +def save_progress(processed_list): |
| 52 | + # |
| 53 | + # Save our progress in a log file |
| 54 | + # |
| 55 | + with open('script_progress.log', 'wb') as hfile: |
| 56 | + hfile.write(json.dumps(list(processed_list))) |
| 57 | + |
| 58 | +def listen_mode(bucket): |
| 59 | + |
| 60 | + print("[+]: Listen Mode") |
| 61 | + |
| 62 | + # |
| 63 | + # Init Set with all current files |
| 64 | + # |
| 65 | + current_list = set() |
| 66 | + for obj in bucket.objects.all(): |
| 67 | + key = obj.key |
| 68 | + current_list.add(key) |
| 69 | + |
| 70 | + # |
| 71 | + # Infinite loop and we only process new files we see |
| 72 | + # |
| 73 | + while True: |
| 74 | + for obj in bucket.objects.all(): |
| 75 | + key = obj.key |
| 76 | + if key not in current_list: |
| 77 | + print "[+]: New File: {}".format(key) |
| 78 | + # |
| 79 | + # We have not processed this file. |
| 80 | + # |
| 81 | + body = obj.get()['Body'].read() |
| 82 | + process_events(body) |
| 83 | + current_list.add(key) |
| 84 | + else: |
| 85 | + pass |
| 86 | + |
| 87 | + print "Sleeping for 1 min" |
| 88 | + time.sleep(60) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + print "Starting s3-watchlist-ban script..." |
| 93 | + # |
| 94 | + # Argument parsing |
| 95 | + # |
| 96 | + parser = build_cli_parser() |
| 97 | + parser.add_argument("-w", '--watchlist', |
| 98 | + help="Watchlist Name", |
| 99 | + required=True) |
| 100 | + |
| 101 | + parser.add_argument("-b", '--bucket', |
| 102 | + help="S3 Bucket Name", |
| 103 | + required=True) |
| 104 | + |
| 105 | + parser.add_argument("-a", '--awsprofile', |
| 106 | + help="AWS Credential profile", |
| 107 | + required=False) |
| 108 | + |
| 109 | + parser.add_argument("-l", '--listen', |
| 110 | + help="Listen mode and only process new events", |
| 111 | + action='store_true', |
| 112 | + required=False) |
| 113 | + |
| 114 | + args = parser.parse_args() |
| 115 | + |
| 116 | + if not args.profile: |
| 117 | + aws_profile = "default" |
| 118 | + else: |
| 119 | + aws_profile = args.awsprofile |
| 120 | + |
| 121 | + # |
| 122 | + # Connect to S3 |
| 123 | + # |
| 124 | + session = boto3.Session(profile_name=aws_profile) |
| 125 | + s3 = session.resource('s3') |
| 126 | + my_bucket = s3.Bucket(args.bucket) |
| 127 | + |
| 128 | + # |
| 129 | + # Connect to Cb Response so we can create Banned Hashes |
| 130 | + # |
| 131 | + cb = get_cb_response_object(args) |
| 132 | + |
| 133 | + # |
| 134 | + # Save watchlist name from arguments |
| 135 | + # |
| 136 | + watchlist_name = args.watchlist |
| 137 | + |
| 138 | + if args.listen: |
| 139 | + listen_mode(my_bucket) |
| 140 | + |
| 141 | + try: |
| 142 | + with open('script_progress.log', 'rb') as hfile: |
| 143 | + for item in json.loads(hfile.read()): |
| 144 | + processed_list.add(item) |
| 145 | + except: |
| 146 | + print("[?]: No previous progress file found: script_progress.log") |
| 147 | + processed_list = set() |
| 148 | + |
| 149 | + # |
| 150 | + # List all files in S3 Bucket |
| 151 | + # |
| 152 | + for obj in reversed(list(my_bucket.objects.all())): |
| 153 | + key = obj.key |
| 154 | + |
| 155 | + # |
| 156 | + # Check to see if we have already processed this file |
| 157 | + # |
| 158 | + if key not in processed_list: |
| 159 | + print "[+]: Processing file: {}".format(key) |
| 160 | + # |
| 161 | + # We have not processed this file. |
| 162 | + # |
| 163 | + body = obj.get()['Body'].read() |
| 164 | + |
| 165 | + process_events(body) |
| 166 | + processed_list.add(key) |
| 167 | + save_progress(processed_list) |
| 168 | + else: |
| 169 | + print "[+]: We have already processed file: {}".format(key) |
| 170 | + |
| 171 | + print("[+]: saving progess to file script_progress.log") |
| 172 | + save_progress(processed_list) |
| 173 | + |
0 commit comments