fix(packaging): move templates/ into decnet/ package so they ship with pip install
The docker build contexts and syslog_bridge.py lived at repo root, which meant setuptools (include = ["decnet*"]) never shipped them. Agents installed via `pip install $RELEASE_DIR` got site-packages/decnet/** but no templates/, so every deploy blew up in deployer._sync_logging_helper with FileNotFoundError on templates/syslog_bridge.py. Move templates/ -> decnet/templates/ and declare it as setuptools package-data. Path resolutions in services/*.py and engine/deployer.py drop one .parent since templates now lives beside the code. Test fixtures, bandit exclude path, and coverage omit glob updated to match.
This commit is contained in:
28
decnet/templates/conpot/Dockerfile
Normal file
28
decnet/templates/conpot/Dockerfile
Normal file
@@ -0,0 +1,28 @@
|
||||
ARG BASE_IMAGE=honeynet/conpot:latest
|
||||
FROM ${BASE_IMAGE}
|
||||
|
||||
USER root
|
||||
|
||||
# Replace 5020 with 502 in all templates so Modbus binds on the standard port
|
||||
RUN find /opt /usr /etc /home -name "*.xml" -exec sed -i 's/<port>5020<\/port>/<port>502<\/port>/g' {} + 2>/dev/null || true
|
||||
RUN find /opt /usr /etc /home -name "*.xml" -exec sed -i 's/port="5020"/port="502"/g' {} + 2>/dev/null || true
|
||||
|
||||
# Install libcap and give the Python interpreter permission to bind ports < 1024
|
||||
RUN (apt-get update && apt-get install -y --no-install-recommends libcap2-bin 2>/dev/null) || (apk add --no-cache libcap 2>/dev/null) || true
|
||||
RUN find /home/conpot/.local/bin /usr /opt -type f -name 'python*' -exec setcap 'cap_net_bind_service+eip' {} \; 2>/dev/null || true
|
||||
|
||||
# Bridge conpot's own logger into syslog-relay's RFC 5424 syslog pipeline.
|
||||
# entrypoint.py is self-contained (inlines the formatter) because the
|
||||
# conpot base image runs Python 3.6, which cannot import the shared
|
||||
# syslog_bridge.py (that file uses 3.9+ / 3.10+ type syntax).
|
||||
COPY entrypoint.py /home/conpot/entrypoint.py
|
||||
RUN chown conpot:conpot /home/conpot/entrypoint.py \
|
||||
&& chmod +x /home/conpot/entrypoint.py
|
||||
|
||||
# The upstream image already runs as non-root 'conpot'.
|
||||
# We do NOT switch to a 'logrelay' user — doing so breaks pkg_resources
|
||||
# because conpot's eggs live under /home/conpot/.local and are only on
|
||||
# the Python path for that user.
|
||||
USER conpot
|
||||
|
||||
ENTRYPOINT ["/usr/bin/python3", "/home/conpot/entrypoint.py"]
|
||||
144
decnet/templates/conpot/entrypoint.py
Normal file
144
decnet/templates/conpot/entrypoint.py
Normal file
@@ -0,0 +1,144 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Entrypoint wrapper for the Conpot ICS/SCADA honeypot.
|
||||
|
||||
Launches conpot as a child process and bridges its log output into the
|
||||
syslog-relay structured syslog pipeline. Each line from conpot stdout/stderr
|
||||
is classified and emitted as an RFC 5424 syslog line so the host-side
|
||||
collector can ingest it alongside every other service.
|
||||
|
||||
Written to be compatible with Python 3.6 (the conpot base image version).
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
import re
|
||||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
from datetime import datetime, timezone
|
||||
|
||||
# ── RFC 5424 inline formatter (Python 3.6-compatible) ─────────────────────────
|
||||
|
||||
_FACILITY_LOCAL0 = 16
|
||||
_SD_ID = "relay@55555"
|
||||
_NILVALUE = "-"
|
||||
|
||||
SEVERITY_INFO = 6
|
||||
SEVERITY_WARNING = 4
|
||||
SEVERITY_ERROR = 3
|
||||
|
||||
|
||||
def _sd_escape(value):
|
||||
return value.replace("\\", "\\\\").replace('"', '\\"').replace("]", "\\]")
|
||||
|
||||
|
||||
def _syslog_line(event_type, severity=SEVERITY_INFO, **fields):
|
||||
pri = "<{}>".format(_FACILITY_LOCAL0 * 8 + severity)
|
||||
ts = datetime.now(timezone.utc).isoformat()
|
||||
host = NODE_NAME[:255]
|
||||
appname = "conpot"
|
||||
msgid = event_type[:32]
|
||||
|
||||
if fields:
|
||||
params = " ".join('{}="{}"'.format(k, _sd_escape(str(v))) for k, v in fields.items())
|
||||
sd = "[{} {}]".format(_SD_ID, params)
|
||||
else:
|
||||
sd = _NILVALUE
|
||||
|
||||
return "{pri}1 {ts} {host} {appname} {nil} {msgid} {sd}".format(
|
||||
pri=pri, ts=ts, host=host, appname=appname,
|
||||
nil=_NILVALUE, msgid=msgid, sd=sd,
|
||||
)
|
||||
|
||||
|
||||
def _log(event_type, severity=SEVERITY_INFO, **fields):
|
||||
print(_syslog_line(event_type, severity, **fields), flush=True)
|
||||
|
||||
|
||||
# ── Config ────────────────────────────────────────────────────────────────────
|
||||
|
||||
NODE_NAME = os.environ.get("NODE_NAME", "conpot-node")
|
||||
TEMPLATE = os.environ.get("CONPOT_TEMPLATE", "default")
|
||||
|
||||
_CONPOT_CMD = [
|
||||
"/home/conpot/.local/bin/conpot",
|
||||
"--template", TEMPLATE,
|
||||
"--logfile", "/var/log/conpot/conpot.log",
|
||||
"-f",
|
||||
"--temp_dir", "/tmp",
|
||||
]
|
||||
|
||||
# Grab the first routable IPv4 address from a log line
|
||||
_IP_RE = re.compile(r"\b((?!127\.)(?!0\.)(?!255\.)\d{1,3}(?:\.\d{1,3}){3})\b")
|
||||
|
||||
_REQUEST_RE = re.compile(
|
||||
r"request|recv|received|connect|session|query|command|"
|
||||
r"modbus|snmp|http|s7comm|bacnet|enip",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
_ERROR_RE = re.compile(r"error|exception|traceback|critical|fail", re.IGNORECASE)
|
||||
_WARN_RE = re.compile(r"warning|warn", re.IGNORECASE)
|
||||
_STARTUP_RE = re.compile(
|
||||
r"starting|started|listening|server|initializ|template|conpot",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
|
||||
# ── Classifier ────────────────────────────────────────────────────────────────
|
||||
|
||||
def _classify(raw):
|
||||
"""Return (event_type, severity, fields) for one conpot log line."""
|
||||
fields = {}
|
||||
|
||||
m = _IP_RE.search(raw)
|
||||
if m:
|
||||
fields["src"] = m.group(1)
|
||||
|
||||
fields["msg"] = raw[:300]
|
||||
|
||||
if _ERROR_RE.search(raw):
|
||||
return "error", SEVERITY_ERROR, fields
|
||||
if _WARN_RE.search(raw):
|
||||
return "warning", SEVERITY_WARNING, fields
|
||||
if _REQUEST_RE.search(raw):
|
||||
return "request", SEVERITY_INFO, fields
|
||||
if _STARTUP_RE.search(raw):
|
||||
return "startup", SEVERITY_INFO, fields
|
||||
return "log", SEVERITY_INFO, fields
|
||||
|
||||
|
||||
# ── Main ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
def main():
|
||||
_log("startup", msg="Conpot ICS honeypot starting (template={})".format(TEMPLATE))
|
||||
|
||||
proc = subprocess.Popen(
|
||||
_CONPOT_CMD,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
bufsize=1,
|
||||
universal_newlines=True,
|
||||
)
|
||||
|
||||
def _forward(sig, _frame):
|
||||
proc.send_signal(sig)
|
||||
|
||||
signal.signal(signal.SIGTERM, _forward)
|
||||
signal.signal(signal.SIGINT, _forward)
|
||||
|
||||
try:
|
||||
for raw_line in proc.stdout:
|
||||
line = raw_line.rstrip()
|
||||
if not line:
|
||||
continue
|
||||
event_type, severity, fields = _classify(line)
|
||||
_log(event_type, severity, **fields)
|
||||
finally:
|
||||
proc.wait()
|
||||
_log("shutdown", msg="Conpot ICS honeypot stopped")
|
||||
sys.exit(proc.returncode)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
89
decnet/templates/conpot/syslog_bridge.py
Normal file
89
decnet/templates/conpot/syslog_bridge.py
Normal file
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Shared RFC 5424 syslog helper used by service containers.
|
||||
|
||||
Services call syslog_line() to format an RFC 5424 message, then
|
||||
write_syslog_file() to emit it to stdout — the container runtime
|
||||
captures it, and the host-side collector streams it into the log file.
|
||||
|
||||
RFC 5424 structure:
|
||||
<PRI>1 TIMESTAMP HOSTNAME APP-NAME PROCID MSGID [SD-ELEMENT] MSG
|
||||
|
||||
Facility: local0 (16). SD element ID uses PEN 55555.
|
||||
"""
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
# ─── Constants ────────────────────────────────────────────────────────────────
|
||||
|
||||
_FACILITY_LOCAL0 = 16
|
||||
_SD_ID = "relay@55555"
|
||||
_NILVALUE = "-"
|
||||
|
||||
SEVERITY_EMERG = 0
|
||||
SEVERITY_ALERT = 1
|
||||
SEVERITY_CRIT = 2
|
||||
SEVERITY_ERROR = 3
|
||||
SEVERITY_WARNING = 4
|
||||
SEVERITY_NOTICE = 5
|
||||
SEVERITY_INFO = 6
|
||||
SEVERITY_DEBUG = 7
|
||||
|
||||
_MAX_HOSTNAME = 255
|
||||
_MAX_APPNAME = 48
|
||||
_MAX_MSGID = 32
|
||||
|
||||
# ─── Formatter ────────────────────────────────────────────────────────────────
|
||||
|
||||
def _sd_escape(value: str) -> str:
|
||||
"""Escape SD-PARAM-VALUE per RFC 5424 §6.3.3."""
|
||||
return value.replace("\\", "\\\\").replace('"', '\\"').replace("]", "\\]")
|
||||
|
||||
|
||||
def _sd_element(fields: dict[str, Any]) -> str:
|
||||
if not fields:
|
||||
return _NILVALUE
|
||||
params = " ".join(f'{k}="{_sd_escape(str(v))}"' for k, v in fields.items())
|
||||
return f"[{_SD_ID} {params}]"
|
||||
|
||||
|
||||
def syslog_line(
|
||||
service: str,
|
||||
hostname: str,
|
||||
event_type: str,
|
||||
severity: int = SEVERITY_INFO,
|
||||
timestamp: datetime | None = None,
|
||||
msg: str | None = None,
|
||||
**fields: Any,
|
||||
) -> str:
|
||||
"""
|
||||
Return a single RFC 5424-compliant syslog line (no trailing newline).
|
||||
|
||||
Args:
|
||||
service: APP-NAME (e.g. "http", "mysql")
|
||||
hostname: HOSTNAME (node name)
|
||||
event_type: MSGID (e.g. "request", "login_attempt")
|
||||
severity: Syslog severity integer (default: INFO=6)
|
||||
timestamp: UTC datetime; defaults to now
|
||||
msg: Optional free-text MSG
|
||||
**fields: Encoded as structured data params
|
||||
"""
|
||||
pri = f"<{_FACILITY_LOCAL0 * 8 + severity}>"
|
||||
ts = (timestamp or datetime.now(timezone.utc)).isoformat()
|
||||
host = (hostname or _NILVALUE)[:_MAX_HOSTNAME]
|
||||
appname = (service or _NILVALUE)[:_MAX_APPNAME]
|
||||
msgid = (event_type or _NILVALUE)[:_MAX_MSGID]
|
||||
sd = _sd_element(fields)
|
||||
message = f" {msg}" if msg else ""
|
||||
return f"{pri}1 {ts} {host} {appname} {_NILVALUE} {msgid} {sd}{message}"
|
||||
|
||||
|
||||
def write_syslog_file(line: str) -> None:
|
||||
"""Emit a syslog line to stdout for container log capture."""
|
||||
print(line, flush=True)
|
||||
|
||||
|
||||
def forward_syslog(line: str, log_target: str) -> None:
|
||||
"""No-op stub. TCP forwarding is handled by rsyslog, not by service containers."""
|
||||
pass
|
||||
Reference in New Issue
Block a user