Skip to content

Instantly share code, notes, and snippets.

@y0ug
Created September 21, 2014 08:38
Show Gist options
  • Save y0ug/9f6afdaa25997d0bee6f to your computer and use it in GitHub Desktop.
Save y0ug/9f6afdaa25997d0bee6f to your computer and use it in GitHub Desktop.
Extract from system evtx logon
import mmap
import contextlib
import argparse
from bs4 import BeautifulSoup, element
from Evtx.Evtx import FileHeader
from Evtx.Views import evtx_file_xml_view
evtxs = {
4624: 'An account was successfully logged on.',
4625: 'An account failed to log on.',
4648: 'A logon was attempted using explicit credentials',
4778: 'A session was reconnected to a Window Station',
4647: 'User initiated logoff',
4634: 'An account was logged off',
4779: 'A session was disconnected from a Window Station'
}
def main():
parser = argparse.ArgumentParser(
description="Dump a binary EVTX file into XML.")
parser.add_argument("evtx", type=str,
help="Path to the Windows System EVTX event log file")
args = parser.parse_args()
with open(args.evtx, 'r') as f:
with contextlib.closing(mmap.mmap(f.fileno(), 0,
access=mmap.ACCESS_READ)) as buf:
fh = FileHeader(buf, 0x0)
for xml, record in evtx_file_xml_view(fh):
soup = BeautifulSoup(xml)
eventid = int(soup.event.system.eventid.string)
date = soup.event.system.timecreated['systemtime']
processid = soup.event.system.execution['processid']
threadid = soup.event.system.execution['threadid']
computer = soup.event.system.computer.string
userid = soup.event.system.security['userid']
if eventid in evtxs:
out = "%s (%s); %s; %s; %s; %s; %s" % (eventid, evtxs[eventid], date, processid, threadid, computer, userid)
else:
out = "%s; %s; %s; %s; %s; %s" % (eventid, date, processid, threadid, computer, userid)
#print soup
info = {}
try:
for child in soup.userdata.children:
if type(child) is element.Tag:
info[child.name] = ' '.join(child.text.split())
out += "; %s: %s " % (child.name, ' '.join(child.text.split()))
except:
pass
try:
for child in soup.eventdata.children:
if type(child) is element.Tag:
info[child['name']] = ' '.join(child.text.split())
out += "; %s: %s " % (child['name'], ' '.join(child.text.split()))
except:
pass
if eventid in [4624, 4625, 4648, 4778, 4647, 4634, 4779]:
if 'LogonType' in info and info['LogonType'] not in ['5', '3', '4']:
print out
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment