# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\s+CommandLine: net share"
test_str = ("Time Event\n"
"1/21/19\n"
"1:48:09.000 PM \n"
"01/21/2019 01:48:09 PM\n"
"LogName=Microsoft-Windows-Sysmon/Operational\n"
"SourceName=Microsoft-Windows-Sysmon\n"
"EventCode=1\n"
"EventType=4\n"
"Type=Information\n"
"ComputerName=DC01.PurpleHaze.local\n"
"User=NOT_TRANSLATED\n"
"Sid=S-1-5-18\n"
"SidType=0\n"
"TaskCategory=Process Create (rule: ProcessCreate)\n"
"OpCode=Info\n"
"RecordNumber=431111\n"
"Keywords=None\n"
"Message=Process Create:\n"
"RuleName: \n"
"UtcTime: 2019-01-21 13:48:09.340\n"
"ProcessGuid: {834924C0-CD99-5C45-0000-0010B3DAC700}\n"
"ProcessId: 4680\n"
"Image: C:\\Windows\\SysWOW64\\net1.exe\n"
"FileVersion: 10.0.14393.0 (rs1_release.160715-1616)\n"
"Description: Net Command\n"
"Product: Microsoft® Windows® Operating System\n"
"Company: Microsoft Corporation\n"
"CommandLine: C:\\Windows\\system32\\net1 share \n"
"CurrentDirectory: C:\\Windows\\system32\\\n"
"User: NT AUTHORITY\\SYSTEM\n"
"LogonGuid: {834924C0-3E0A-5C40-0000-0020E7030000}\n"
"LogonId: 0x3E7\n"
"TerminalSessionId: 0\n"
"IntegrityLevel: System\n"
"Hashes: SHA1=382169595D5BBEB535C4575B3EC8CC7E5E933115\n"
"ParentProcessGuid: {834924C0-CD99-5C45-0000-00100FDAC700}\n"
"ParentProcessId: 1540\n"
"ParentImage: C:\\Windows\\SysWOW64\\net.exe\n"
"ParentCommandLine: net share\n"
"Collapse\n"
"CommandLine = C:\\Windows\\system32\\net1 share host = DC01 source = WinEventLog:Microsoft-Windows-Sysmon/Operational sourcetype = WinEventLog:Microsoft-Windows-Sysmon/Operational\n\n\n"
"1/21/19\n"
"1:48:09.000 PM \n"
"01/21/2019 01:48:09 PM\n"
"LogName=Microsoft-Windows-Sysmon/Operational\n"
"SourceName=Microsoft-Windows-Sysmon\n"
"EventCode=1\n"
"EventType=4\n"
"Type=Information\n"
"ComputerName=DC01.PurpleHaze.local\n"
"User=NOT_TRANSLATED\n"
"Sid=S-1-5-18\n"
"SidType=0\n"
"TaskCategory=Process Create (rule: ProcessCreate)\n"
"OpCode=Info\n"
"RecordNumber=431110\n"
"Keywords=None\n"
"Message=Process Create:\n"
"RuleName: \n"
"UtcTime: 2019-01-21 13:48:09.330\n"
"ProcessGuid: {834924C0-CD99-5C45-0000-00100FDAC700}\n"
"ProcessId: 1540\n"
"Image: C:\\Windows\\SysWOW64\\net.exe\n"
"FileVersion: 10.0.14393.0 (rs1_release.160715-1616)\n"
"Description: Net Command\n"
"Product: Microsoft® Windows® Operating System\n"
"Company: Microsoft Corporation\n"
"CommandLine: net share \n"
"CurrentDirectory: C:\\Windows\\system32\\\n"
"User: NT AUTHORITY\\SYSTEM\n"
"LogonGuid: {834924C0-3E0A-5C40-0000-0020E7030000}\n"
"LogonId: 0x3E7\n"
"TerminalSessionId: 0\n"
"IntegrityLevel: System\n"
"Hashes: SHA1=B160F4462A4728BEC8FA053B99709622A4B4DD20\n"
"ParentProcessGuid: {834924C0-C9D7-5C45-0000-0010FCA2C500}\n"
"ParentProcessId: 3064\n"
"ParentImage: C:\\Windows\\SysWOW64\\cmd.exe\n"
"ParentCommandLine: C:\\Windows\\system32\\cmd.exe\n"
"Collapse\n"
"CommandLine = net share host = DC01 source = WinEventLog:Microsoft-Windows-Sysmon/Operational sourcetype = WinEventLog:Microsoft-Windows-Sysmon/Operational")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html