syslog2cef 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- syslog2cef-0.1.0.dist-info/METADATA +202 -0
- syslog2cef-0.1.0.dist-info/RECORD +20 -0
- syslog2cef-0.1.0.dist-info/WHEEL +5 -0
- syslog2cef-0.1.0.dist-info/entry_points.txt +2 -0
- syslog2cef-0.1.0.dist-info/licenses/LICENSE +21 -0
- syslog2cef-0.1.0.dist-info/top_level.txt +1 -0
- syslogcef/__init__.py +19 -0
- syslogcef/__main__.py +7 -0
- syslogcef/api.py +127 -0
- syslogcef/cef.py +103 -0
- syslogcef/cli.py +136 -0
- syslogcef/mappings/__init__.py +40 -0
- syslogcef/mappings/cisco_asa.json +26 -0
- syslogcef/mappings/cisco_ios.json +13 -0
- syslogcef/mappings/f5.json +13 -0
- syslogcef/mappings/linux.json +13 -0
- syslogcef/mappings/vmware.json +12 -0
- syslogcef/normalizer.py +106 -0
- syslogcef/parsers.py +380 -0
- syslogcef/utils.py +144 -0
syslogcef/utils.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import ipaddress
|
|
4
|
+
import logging
|
|
5
|
+
import os
|
|
6
|
+
import re
|
|
7
|
+
import socket
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from datetime import datetime, timezone
|
|
10
|
+
from typing import Any, Dict, Iterable, Mapping, MutableMapping, Optional
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
MONTHS = {
|
|
15
|
+
"Jan": 1,
|
|
16
|
+
"Feb": 2,
|
|
17
|
+
"Mar": 3,
|
|
18
|
+
"Apr": 4,
|
|
19
|
+
"May": 5,
|
|
20
|
+
"Jun": 6,
|
|
21
|
+
"Jul": 7,
|
|
22
|
+
"Aug": 8,
|
|
23
|
+
"Sep": 9,
|
|
24
|
+
"Oct": 10,
|
|
25
|
+
"Nov": 11,
|
|
26
|
+
"Dec": 12,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
KEY_VALUE_RE = re.compile(r"(?P<key>[A-Za-z0-9_.-]+)=(?P<value>\S+)")
|
|
30
|
+
UTF8_REPLACEMENT_CHAR = "\uFFFD"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def month_abbr_to_int(abbr: str) -> int:
|
|
34
|
+
try:
|
|
35
|
+
return MONTHS[abbr]
|
|
36
|
+
except KeyError as exc:
|
|
37
|
+
raise ValueError(f"Unknown month abbreviation: {abbr!r}") from exc
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def convert_pri(pri: Optional[int]) -> tuple[Optional[int], Optional[int]]:
|
|
41
|
+
if pri is None:
|
|
42
|
+
return None, None
|
|
43
|
+
facility = pri // 8
|
|
44
|
+
severity = pri % 8
|
|
45
|
+
return facility, severity
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def parse_iso8601(value: str) -> datetime:
|
|
49
|
+
if value is None:
|
|
50
|
+
raise ValueError("Timestamp value is required")
|
|
51
|
+
value = str(value)
|
|
52
|
+
if value.isdigit():
|
|
53
|
+
micros = int(value)
|
|
54
|
+
if len(value) > 10:
|
|
55
|
+
seconds, micros = divmod(micros, 1_000_000)
|
|
56
|
+
return datetime.fromtimestamp(seconds, tz=timezone.utc).replace(microsecond=micros)
|
|
57
|
+
return datetime.fromtimestamp(int(value), tz=timezone.utc)
|
|
58
|
+
if value.endswith("Z"):
|
|
59
|
+
value = value[:-1] + "+00:00"
|
|
60
|
+
try:
|
|
61
|
+
return datetime.fromisoformat(value)
|
|
62
|
+
except ValueError as exc:
|
|
63
|
+
raise ValueError(f"Invalid ISO8601 timestamp: {value!r}") from exc
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def ensure_tzaware(ts: Optional[datetime]) -> Optional[datetime]:
|
|
67
|
+
if ts is None:
|
|
68
|
+
return None
|
|
69
|
+
if ts.tzinfo is None:
|
|
70
|
+
return ts.replace(tzinfo=timezone.utc)
|
|
71
|
+
return ts
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def sanitize_message(msg: str) -> str:
|
|
75
|
+
return msg.encode("utf-8", "replace").decode("utf-8").replace("\u0000", UTF8_REPLACEMENT_CHAR)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def parse_key_value_pairs(msg: str) -> Dict[str, str]:
|
|
79
|
+
pairs: Dict[str, str] = {}
|
|
80
|
+
for match in KEY_VALUE_RE.finditer(msg):
|
|
81
|
+
key = match.group("key")
|
|
82
|
+
value = match.group("value")
|
|
83
|
+
if value.startswith(('"', "'")) and value.endswith(('"', "'")):
|
|
84
|
+
value = value[1:-1]
|
|
85
|
+
pairs[key] = value
|
|
86
|
+
return pairs
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def guess_hostname() -> str:
|
|
90
|
+
try:
|
|
91
|
+
return socket.gethostname()
|
|
92
|
+
except Exception:
|
|
93
|
+
return "localhost"
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def is_ip(value: str) -> bool:
|
|
97
|
+
try:
|
|
98
|
+
ipaddress.ip_address(value)
|
|
99
|
+
return True
|
|
100
|
+
except ValueError:
|
|
101
|
+
return False
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclass
|
|
105
|
+
class JournalJSONMapping:
|
|
106
|
+
data: Mapping[str, Any]
|
|
107
|
+
|
|
108
|
+
def to_structured_data(self) -> Dict[str, Any]:
|
|
109
|
+
sd: Dict[str, Any] = {}
|
|
110
|
+
for key, value in self.data.items():
|
|
111
|
+
if key.startswith("_") or key.startswith("__"):
|
|
112
|
+
continue
|
|
113
|
+
if isinstance(value, (str, int, float)):
|
|
114
|
+
sd[f"journald.{key}"] = str(value)
|
|
115
|
+
return sd
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def flatten_dict(prefix: str, data: Mapping[str, Any]) -> Dict[str, Any]:
|
|
119
|
+
flat: Dict[str, Any] = {}
|
|
120
|
+
for key, value in data.items():
|
|
121
|
+
if isinstance(value, Mapping):
|
|
122
|
+
flat.update(flatten_dict(f"{prefix}{key}.", value))
|
|
123
|
+
else:
|
|
124
|
+
flat[f"{prefix}{key}"] = value
|
|
125
|
+
return flat
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def cef_escape(value: str) -> str:
|
|
129
|
+
return (
|
|
130
|
+
value.replace("\\", "\\\\")
|
|
131
|
+
.replace("|", "\\|")
|
|
132
|
+
.replace("=", "\\=")
|
|
133
|
+
.replace("\r", "\\r")
|
|
134
|
+
.replace("\n", "\\n")
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def cef_escape_header(value: str) -> str:
|
|
139
|
+
return (
|
|
140
|
+
value.replace("\\", "\\\\")
|
|
141
|
+
.replace("|", "\\|")
|
|
142
|
+
.replace("\r", " ")
|
|
143
|
+
.replace("\n", " ")
|
|
144
|
+
)
|