suricata

agnusdei·2025년 1월 4일
post-thumbnail

# First Stage: Builder
FROM docker.io/amd64/almalinux:9 AS builder

# Update and install base tools and repositories
RUN \
    dnf -y update && \
    dnf -y install epel-release dnf-plugins-core && \
    dnf config-manager --set-enabled crb

# Install required development packages
RUN dnf -y install \
    autoconf \
    automake \
    cargo \
    cbindgen \
    diffutils \
    dpdk-devel \
    elfutils-libelf-devel \
    file \
    file-devel \
    gcc \
    gcc-c++ \
    git \
    hiredis-devel \
    jansson-devel \
    jq \
    lua-devel \
    libbpf-devel \
    libtool \
    libyaml-devel \
    libnfnetlink-devel \
    libnetfilter_queue-devel \
    libnet-devel \
    libcap-ng-devel \
    libevent-devel \
    libmaxminddb-devel \
    libpcap-devel \
    libprelude-devel \
    libtool \
    lz4-devel \
    make \
    nspr-devel \
    nss-devel \
    nss-softokn-devel \
    numactl-devel \
    pcre2-devel \
    pkgconfig \
    python3-devel \
    python3-yaml \
    rust \
    which \
    zlib-devel

# Install Hyperscan for x86_64
RUN if [ "$(arch)" = "x86_64" ]; then \
    dnf -y install https://kojipkgs.fedoraproject.org/packages/hyperscan/5.4.0/4.el9/x86_64/hyperscan-5.4.0-4.el9.x86_64.rpm https://kojipkgs.fedoraproject.org/packages/hyperscan/5.4.0/4.el9/x86_64/hyperscan-devel-5.4.0-4.el9.x86_64.rpm; \
fi

# Set version argument and working directory
ARG VERSION
WORKDIR /src

# Clone or download the Suricata source code
RUN if [ "${VERSION}" = "master" ]; then \
    git clone https://github.com/OISF/suricata.git suricata-${VERSION}; \
    git clone https://github.com/OISF/libhtp.git suricata-${VERSION}/libhtp; \
    (cd suricata-${VERSION}/suricata-update && \
    curl -L https://github.com/OISF/suricata-update/archive/master.tar.gz | tar zxf - --strip-components=1); \
    (cd suricata-${VERSION} && ./autogen.sh); \
else \
    curl -OL https://www.openinfosecfoundation.org/download/suricata-${VERSION}.tar.gz; \
    tar zxf suricata-${VERSION}.tar.gz; \
fi

# Change to the Suricata directory
WORKDIR /src/suricata-${VERSION}

# Configure build arguments and compile options
ARG CONFIGURE_ARGS
RUN ./configure \
    --prefix=/usr \
    --disable-shared \
    --disable-gccmarch-native \
    --enable-lua \
    --enable-nfqueue \
    --enable-hiredis \
    --enable-geoip \
    --enable-ebpf \
    --enable-dpdk \
    ${CONFIGURE_ARGS}

# Set number of cores for compilation
ARG CORES=2
RUN make -j "${CORES}"

# Install Suricata to a temporary directory
RUN make install install-conf DESTDIR=/fakeroot

# Clean up unnecessary directories
RUN rm -rf /fakeroot/var

# Second Stage: Runner
FROM docker.io/almalinux/amd64:9-base AS runner

# Update and install runtime dependencies
RUN \
    dnf -y update && \
    dnf -y install epel-release && \
    dnf -y install \
    cronie \
    dpdk \
    elfutils-libelf \
    file \
    findutils \
    hiredis \
    iproute \
    jansson \
    lua-libs \
    libbpf \
    libyaml \
    libnfnetlink \
    libnetfilter_queue \
    libnet \
    libcap-ng \
    libevent \
    libmaxminddb \
    libpcap \
    libprelude \
    logrotate \
    lz4 \
    net-tools \
    nss \
    nss-softokn \
    numactl \
    pcre2 \
    procps-ng \
    python3 \
    python3-yaml \
    tcpdump \
    which \
    zlib && \
    if [ "$(arch)" = "x86_64" ]; then dnf -y install https://kojipkgs.fedoraproject.org/packages/hyperscan/5.4.0/4.el9/x86_64/hyperscan-5.4.0-4.el9.x86_64.rpm; fi && \
    dnf clean all && \
    find /etc/logrotate.d -type f -not -name suricata -delete

# Copy built files from the builder stage
COPY --from=builder /fakeroot /

# Create necessary directories
RUN mkdir -p /var/log/suricata /var/run/suricata /var/lib/suricata

# Copy configuration files
COPY /update.yaml /etc/suricata/update.yaml
COPY /suricata.logrotate /etc/logrotate.d/suricata

# Update Suricata sources and enable features
RUN suricata-update update-sources && \
    suricata-update enable-source oisf/trafficid && \
    suricata-update --no-test --no-reload && \
    /usr/bin/suricata -V

# Create Suricata user and set permissions
RUN useradd --system --create-home suricata && \
    chown -R suricata:suricata /etc/suricata && \
    chown -R suricata:suricata /var/log/suricata && \
    chown -R suricata:suricata /var/lib/suricata && \
    chown -R suricata:suricata /var/run/suricata && \
    cp -a /etc/suricata /etc/suricata.dist && \
    chmod 600 /etc/logrotate.d/suricata

# Define volumes
VOLUME /var/log/suricata
VOLUME /var/lib/suricata
VOLUME /etc/suricata

# Copy the entrypoint script and set as the entrypoint
COPY /docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]

# Verify the Suricata build
RUN /usr/bin/suricata --build-info

1. 첫 번째 단계: Builder

이 단계는 Suricata 소프트웨어를 빌드하는 데 필요한 도구 및 라이브러리를 설치하고 Suricata를 컴파일합니다.

1-1. Base 이미지 설정

FROM docker.io/amd64/almalinux:9 AS builder
  • AlmaLinux 9amd64 아키텍처 기반 이미지를 사용하여 빌드 환경을 생성합니다.
  • "builder"라는 이름의 빌드 스테이지를 정의합니다.

1-2. 시스템 업데이트 및 기본 패키지 설치

RUN \
    dnf -y update && \
    dnf -y install epel-release dnf-plugins-core && \
    dnf config-manager --set-enabled crb
  • dnf update: 시스템 패키지를 최신 버전으로 업데이트.
  • epel-release: 추가 패키지들을 설치할 수 있도록 EPEL(Extra Packages for Enterprise Linux) 저장소를 활성화.
  • crb(CodeReady Builder): 추가 개발 도구를 위한 저장소 활성화.

1-3. 빌드 도구 및 라이브러리 설치

RUN dnf -y install ...
  • Suricata 빌드에 필요한 필수 도구 및 라이브러리를 설치:
    • autoconf, automake: 자동화된 빌드 환경 설정.
    • gcc, gcc-c++: C/C++ 컴파일러.
    • dpdk-devel, libpcap-devel: 네트워크 관련 라이브러리.
    • rust, cargo: Rust 기반 코드 빌드.
    • python3-devel: Python 3 지원.
    • 기타 필요한 패키지들.

1-4. Hyperscan 설치 (x86_64 아키텍처 전용)

RUN if [ "$(arch)" = "x86_64" ]; then \
    dnf -y install https://kojipkgs.fedoraproject.org/...; \
fi
  • Hyperscan은 Suricata의 고속 패턴 매칭 엔진으로 사용됩니다.
  • x86_64 환경에서만 사용 가능하며, 외부 링크에서 다운로드 및 설치됩니다.

1-5. 소스 코드 다운로드 및 준비

ARG VERSION

WORKDIR /src

RUN if [ "${VERSION}" = "master" ]; then \
    git clone https://github.com/OISF/suricata.git ...; \
else \
    curl -OL https://www.openinfosecfoundation.org/...; \
    tar zxf suricata-${VERSION}.tar.gz; \
fi
  • Suricata의 소스 코드를 다운로드합니다.
    • master 브랜치를 사용할 경우, Git 리포지토리에서 최신 소스를 클론.
    • 특정 버전을 사용할 경우, 해당 버전의 tarball 파일을 다운로드 및 압축 해제.

1-6. 빌드 및 설치 준비

RUN ./configure \
    --prefix=/usr \
    --disable-shared ...
  • ./configure: Suricata 빌드 환경을 설정합니다.
    • 주요 옵션:
      • --prefix: 설치 경로 지정.
      • --enable-lua: Lua 스크립팅 지원.
      • --enable-dpdk: DPDK(고속 패킷 처리) 지원.

1-7. 컴파일 및 설치

ARG CORES=2

RUN make -j "${CORES}"

RUN make install install-conf DESTDIR=/fakeroot
  • make: 소스 코드를 빌드합니다.
  • make install: 빌드된 파일을 /fakeroot 디렉토리에 임시로 설치합니다.

1-8. 불필요한 데이터 제거

RUN rm -rf /fakeroot/var
  • Docker 환경에서는 /var 디렉토리 관련 문제가 발생할 수 있으므로 이를 제거합니다.

2. 두 번째 단계: Runner

이 단계는 Suricata 실행 환경을 생성합니다.

2-1. Base 이미지 설정

FROM docker.io/almalinux/amd64:9-base AS runner
  • Suricata를 실행하기 위한 AlmaLinux 9 Base 이미지를 사용합니다.

2-2. 실행에 필요한 패키지 설치

RUN \
    dnf -y update && \
    dnf -y install ...
  • Suricata 실행에 필요한 라이브러리 및 도구를 설치:
    • 네트워크 관련 툴(tcpdump, iproute 등).
    • 로그 관리 툴(logrotate).
    • Hyperscan 설치(x86_64 환경).

2-3. 빌드 결과 복사

COPY --from=builder /fakeroot /
  • 빌드 단계에서 생성된 Suricata 파일을 /fakeroot에서 실행 환경으로 복사.

2-4. 디렉토리 생성

RUN mkdir -p /var/log/suricata /var/run/suricata /var/lib/suricata
  • Suricata가 실행될 때 사용하는 디렉토리를 생성합니다.

2-5. 설정 파일 복사

COPY /update.yaml /etc/suricata/update.yaml
COPY /suricata.logrotate /etc/logrotate.d/suricata
  • Suricata 설정 및 로그 회전을 위한 파일을 이미지에 복사합니다.

2-6. Suricata 업데이트 및 사용자 설정

RUN suricata-update update-sources ...
RUN useradd --system --create-home suricata ...
  • Suricata 업데이트:
    • suricata-update: 서명 데이터베이스를 업데이트.
  • Suricata 사용자 생성:
    • useradd: Suricata 실행 전용 시스템 사용자 생성.

2-7. 볼륨 선언

VOLUME /var/log/suricata
VOLUME /var/lib/suricata
VOLUME /etc/suricata
  • Suricata가 데이터를 저장하는 디렉토리를 컨테이너 외부와 연결하기 위해 볼륨을 선언.

2-8. 엔트리포인트 설정

COPY /docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]
  • 컨테이너 시작 시 실행할 스크립트를 설정.

2-9. 빌드 정보 확인

RUN /usr/bin/suricata --build-info
  • Suricata의 빌드 정보를 출력하여 설치가 정상적으로 완료되었는지 확인.

요약

이 Dockerfile은 빌드 단계실행 단계로 나뉘며, AlmaLinux를 기반으로 Suricata를 빌드 및 실행할 수 있는 컨테이너 이미지를 만듭니다.
빌드 단계에서 소스를 컴파일하고, 실행 단계에서 필요한 환경을 설정합니다.

profile
DevSecOps, Pentest, Cloud(OpenStack), Develop, Data Engineering, AI-Agent

0개의 댓글