# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?P<FechayHora>(?:20[0-9]{2}-[0-1]\d-[0-2]\d)\s(?:(?:[0-1]\d|2[0-3]):){2}(?:[0-1]\d|2[0-3]))(?:,|\s)(?P<LogLevel>(?:\w*\.?\w*))(?:,|\s)(?P<Hostname>(?:\d{1,3}\.){3}\d{1,3}|(?:\w*\.cerrey\.com\.mx))(?:,|\s)(?P<mensaje>\"?[\w\s:,\[\]()/-]*\"?)"
test_str = ("2023-05-21 00:00:00,Daemon.Info,172.16.1.247,May 21 00:40:01 flex3 systemd: Created slice User Slice of root.\n"
"2023-05-21 00:00:00,Daemon.Info,MTYDC02.cerrey.com.mx,May 21 00:40:01 flex3 systemd: Starting User Slice of root.\n"
"2023-05-21 00:00:00,Daemon.Info,172.16.1.247,May 21 00:40:01 flex3 systemd: Started Session 2782 of user root.\n"
"2023-05-21 00:00:00,Daemon.Info,172.16.1.247,May 21 00:40:01 flex3 systemd: Starting Session 2782 of user root.\n"
"2023-05-21 00:00:00,Cron.Info,172.16.1.247,May 21 00:40:01 flex3 CROND[23978]: (root) CMD (/usr/lib64/sa/sa1 1 1)\n"
"2023-05-21 00:00:00,Daemon.Info,172.16.1.247,May 21 00:40:01 flex3 systemd: Removed slice User Slice of root.\n"
"2023-05-21 00:00:00,Daemon.Info,172.16.1.247,May 21 00:40:01 flex3 systemd: Stopping User Slice of root.\n"
"2023-05-21 00:00:01,System3.Critical,MTYSYSLOG.cerrey.com.mx,may 21 00:00:01 MTYSYSLOG.cerrey.com.mx MSWinEventLog 2 Security 10337 dom may 21 00:00:00 2023 4673 Microsoft-Windows-Security-Auditing N/A Audit Failure MTYSYSLOG.cerrey.com.mx 13056 A privileged service was called.\n\n"
"Subject:\n"
" Security ID: S-1-5-19\n"
" Account Name: LOCAL SERVICE\n"
" Account Domain: NT AUTHORITY\n"
" Logon ID: 0x3E5\n\n"
"Service:\n"
" Server: Security\n"
" Service Name: -\n\n"
"Process:\n"
" Process ID: 0x1f1c\n"
" Process Name: C:\\Windows\\System32\\svchost.exe\n\n"
"Service Request Information:\n"
" Privileges: SeProfileSingleProcessPrivilege\n"
"2023-05-21 00:00:01,System3.Critical,MTYSYSLOG.cerrey.com.mx,may 21 00:00:01 MTYSYSLOG.cerrey.com.mx MSWinEventLog 2 Security 10338 dom may 21 00:00:00 2023 4673 Microsoft-Windows-Security-Auditing N/A Audit Failure MTYSYSLOG.cerrey.com.mx 13056 A privileged service was called.\n\n"
"Subject:\n"
" Security ID: S-1-5-19\n"
" Account Name: LOCAL SERVICE\n"
" Account Domain: NT AUTHORITY\n"
" Logon ID: 0x3E5\n\n"
"Service:\n"
" Server: Security\n"
" Service Name: -\n\n"
"Process:\n"
" Process ID: 0x1f1c\n"
" Process Name: C:\\Windows\\System32\\svchost.exe\n\n"
"Service Request Information:\n"
" Privileges: SeProfileSingleProcessPrivilege\n"
"2023-05-21 00:00:01,System4.Notice,MTYSPDB2.cerrey.com.mx,may 21 00:00:01 MTYSPDB2.cerrey.com.mx MSWinEventLog 5 Security 105034 dom may 21 00:00:00 2023 4672 Microsoft-Windows-Security-Auditing N/A Audit Success MTYSPDB2.cerrey.com.mx 12548 Special privileges assigned to new logon.\n\n"
"Subject:\n"
" Security ID: S-1-5-21-1430283757-1547701557-1542849698-18847\n"
" Account Name: SP_Farm\n"
" Account Domain: CERREY\n"
" Logon ID: 0x592C221\n\n"
"Privileges: SeSecurityPrivilege\n"
" SeBackupPrivilege\n"
" SeRestorePrivilege\n"
" SeTakeOwnershipPrivilege\n"
" SeDebugPrivilege\n"
" SeSystemEnvironmentPrivilege\n"
" SeLoadDriverPrivilege\n"
" SeImpersonatePrivilege\n"
" SeDelegateSessionUserImpersonatePrivilege\n"
"2023-05-21 00:00:01,System4.Notice,MTYSPDB2.cerrey.com.mx,\"may 21 00:00:01 MTYSPDB2.cerrey.com.mx MSWinEventLog 5 Security 105035 dom may 21 00:00:00 2023 4624 Microsoft-Windows-Security-Auditing N/A Audit Success MTYSPDB2.cerrey.com.mx 12544 An account was successfully logged on.\n\n"
"Subject:\n"
" Security ID: S-1-0-0\n"
" Account Name: -\n"
" Account Domain: -\n"
" Logon ID: 0x0\n\n"
"Logon Information:\n"
" Logon Type: 3\n"
" Restricted Admin Mode: -\n"
" Virtual Account: No\n"
" Elevated Token: Yes\n\n"
"Impersonation Level: Impersonation\n\n"
"New Logon:\n"
" Security ID: S-1-5-21-1430283757-1547701557-1542849698-18847\n"
" Account Name: SP_Farm\n"
" Account Domain: CERREY.COM.MX\n"
" Logon ID: 0x592C221\n"
" Linked Logon ID: 0x0\n"
" Network Account Name: -\n"
" Network Account Domain: -\n"
" Logon GUID: {fc043bcf-045d-fc8b-47dd-711370b096cf}\n\n"
"Process Information:\n"
" Process ID: 0x0\n"
" Process Name: -\n\n"
"Network Information:\n"
" Workstation Name: -\n"
" Source Network Address: -\n"
" Source Port: -\n\n"
"Detailed Authentication Information:\n"
" Logon Process: Kerberos\n"
" Authentication Package: Kerberos\n"
" Transited Services: -\n"
" Package Name (NTLM only): -\n"
" Key Length: 0\n\n"
"This event is generated when a logon session is created. It is generated on the computer that was accessed.\n\n"
"The subject fields indicate the account on the local system which requested the logon. This is most commonly a service such as the Server service, or a local process such as Winlogon.exe or Services.exe.\n\n"
"The logon type field indicates the kind of logon that occurred. The most common types are 2 (interactive) and 3 (network).\n\n"
"The New Logon fields indicate the account for whom the new logon was created, i.e. the account that was logged on.\n\n"
"The network fields indicate where a remote logon request originated. Workstation name is not always available and may be left blank in some cases.\n\n"
"The impersonation level field indicates the extent to which a process in the logon session can impersonate.\n\n"
"The authentication information fields provide detailed information about this specific logon request.\n"
" - Logon GUID is a unique identifier that can be used to correlate this event with a KDC event.\n"
" - Transited services indicate which intermediate services have participated in this logon request.\n"
" - Package name indicates which sub-protocol was used among the NTLM protocols.\n"
" - Key length indicates the length of the generated session key. This will be 0 if no session key was requested.\"")
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