Last active
April 30, 2021 17:25
-
-
Save nijave/abd2224b5d0e120132dba7f130258d9f to your computer and use it in GitHub Desktop.
Shows a summary of information from AWS S3 Access logs assuming they're in the same directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import os | |
| import re | |
| # Reference https://stackoverflow.com/questions/7961316/regex-to-split-columns-of-an-amazon-s3-bucket-log | |
| parse = re.compile(r"([^\s\"\[\]]+|\[[^\]\[]+\]|\"[^\"]+\")\s+") | |
| logs = ( | |
| l.strip() | |
| # Get lines from files in current working directory | |
| for contents in (open(fn, "r").read() for fn in os.listdir()) | |
| for l in contents.splitlines() | |
| ) | |
| for line in logs: | |
| matches = parse.findall(line) | |
| ts = matches[2] | |
| user = matches[4] | |
| action = matches[6] | |
| obj = matches[8] | |
| # Filter out certain principals | |
| if any(s in user for s in ("my-iam-user-or-session", "AWS-Crawler", "ConfigRecorderRole", "svc:s3.amazonaws.com")): | |
| continue | |
| print(ts, user, action, obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment