Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next/699/20250126/v1 #12480

Merged
merged 6 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion doc/userguide/rules/ftp-keywords.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,30 @@ Signature Example:
:example-rule-options:`file.name; content:"file.txt";` \
classtype:bad-unknown; sid:1; rev:1;)

For additional information on the ``file.name`` keyword, see :doc:`file-keywords`.
For additional information on the ``file.name`` keyword, see :doc:`file-keywords`.

ftp.command
-----------

This keyword matches on the command name from a FTP client request. ``ftp.command``
is a sticky buffer and can be used as a fast pattern.

Syntax::

ftp.command; content: <command>;

Signature Example:

.. container:: example-rule

alert ftp any any -> any any (:example-rule-options:`ftp.command; content:"PASS";` sid: 1;)

Examples of commands are:

* USER
* PASS
* PORT
* EPRT
* PASV
* RETR

63 changes: 63 additions & 0 deletions doc/userguide/rules/smtp-keywords.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,69 @@ Signature Example:

For additional information on the ``file.name`` keyword, see :doc:`file-keywords`.


smtp.helo
---------

SMTP helo is the parameter passed to the first HELO command from the client.
This keyword matches per transaction, so it can match more than once per flow,
even if the helo occured only once at the beginning of the flow.

Syntax::

smtp.helo; content:"localhost";

Signature example::

alert smtp any any -> any any (msg:"SMTP helo localhost"; smtp.helo; content:"localhost"; sid:2; rev:1;)

``smtp.helo`` is a 'sticky buffer'.

``smtp.helo`` can be used as ``fast_pattern``.

This keyword maps to the eve.json log field ``smtp.helo``

smtp.mail_from
--------------

SMTP mail from is the parameter passed to the first MAIL FROM command from the client.

Syntax::

smtp.mail_from; content:"spam";

Signature example::

alert smtp any any -> any any (msg:"SMTP mail from spam"; smtp.mail_from; content:"spam"; sid:2; rev:1;)

``smtp.mail_from`` is a 'sticky buffer'.

``smtp.mail_from`` can be used as ``fast_pattern``.

This keyword maps to the eve.json log field ``smtp.mail_from``

smtp.rcpt_to
------------

SMTP rcpt to is the one of the parameters passed to one RCPT TO command from the client.

Syntax::

smtp.rcpt_to; content:"sensitive@target";

Signature example::

alert smtp any any -> any any (msg:"SMTP rcpt to sensitive"; smtp.rcpt_to; content:"sensitive@target"; sid:2; rev:1;)

``smtp.rcpt_to`` is a 'sticky buffer'.

``smtp.rcpt_to`` is a 'multi buffer'.

``smtp.rcpt_to`` can be used as ``fast_pattern``.

This keyword maps to the eve.json log field ``smtp.rcpt_to[]``


Frames
------

Expand Down
2 changes: 1 addition & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ extern crate suricata_derive;
pub mod core;

#[macro_use]
pub(crate) mod debug;
pub mod debug;

pub mod common;
pub mod conf;
Expand Down
6 changes: 3 additions & 3 deletions rust/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

pub fn init() {
unsafe {
let context = super::core::SCGetContext();
super::core::init_ffi(context);
let context = crate::core::SCGetContext();
crate::core::init_ffi(context);

super::debug::SCSetRustLogLevel(super::debug::SCLogGetLogLevel());
crate::debug::LEVEL = crate::debug::SCLogGetLogLevel();
}
}
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ noinst_HEADERS = \
detect-frame.h \
detect-ftpbounce.h \
detect-ftpdata.h \
detect-ftp-command.h \
detect-geoip.h \
detect-gid.h \
detect.h \
Expand Down Expand Up @@ -266,6 +267,7 @@ noinst_HEADERS = \
detect-smb-ntlmssp.h \
detect-smb-share.h \
detect-smb-version.h \
detect-smtp.h \
detect-ssh-hassh.h \
detect-ssh-hassh-server.h \
detect-ssh-hassh-server-string.h \
Expand Down Expand Up @@ -737,6 +739,7 @@ libsuricata_c_a_SOURCES = \
detect-frame.c \
detect-ftpbounce.c \
detect-ftpdata.c \
detect-ftp-command.c \
detect-geoip.c \
detect-gid.c \
detect-hostbits.c \
Expand Down Expand Up @@ -833,6 +836,7 @@ libsuricata_c_a_SOURCES = \
detect-smb-ntlmssp.c \
detect-smb-share.c \
detect-smb-version.c \
detect-smtp.c \
detect-ssh-hassh.c \
detect-ssh-hassh-server.c \
detect-ssh-hassh-server-string.c \
Expand Down
4 changes: 4 additions & 0 deletions src/detect-engine-register.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@

#include "detect-smb-share.h"
#include "detect-smb-version.h"
#include "detect-smtp.h"

#include "detect-base64-decode.h"
#include "detect-base64-data.h"
Expand Down Expand Up @@ -210,6 +211,7 @@
#include "detect-quic-cyu-hash.h"
#include "detect-quic-cyu-string.h"
#include "detect-ja4-hash.h"
#include "detect-ftp-command.h"

#include "detect-bypass.h"
#include "detect-ftpdata.h"
Expand Down Expand Up @@ -706,6 +708,7 @@ void SigTableSetup(void)
DetectQuicCyuHashRegister();
DetectQuicCyuStringRegister();
DetectJa4HashRegister();
DetectFtpCommandRegister();

DetectBypassRegister();
DetectConfigRegister();
Expand All @@ -730,6 +733,7 @@ void SigTableSetup(void)
DetectVlanIdRegister();
DetectVlanLayersRegister();

SCDetectSMTPRegister();
ScDetectSNMPRegister();
SCDetectDHCPRegister();
ScDetectWebsocketRegister();
Expand Down
2 changes: 2 additions & 0 deletions src/detect-engine-register.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ enum DetectKeywordId {

DETECT_JA4_HASH,

DETECT_FTP_COMMAND,

DETECT_VLAN_ID,
DETECT_VLAN_LAYERS,

Expand Down
103 changes: 103 additions & 0 deletions src/detect-ftp-command.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* Copyright (C) 2025 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

/**
*
* \author Jeff Lucovsky <[email protected]>
*
* Implements the ftp.command sticky buffer
*
*/

#include "suricata-common.h"
#include "detect.h"

#include "detect-parse.h"
#include "detect-engine.h"
#include "detect-engine-mpm.h"
#include "detect-engine-prefilter.h"
#include "detect-content.h"

#include "flow.h"

#include "util-debug.h"

#include "app-layer.h"
#include "app-layer-ftp.h"

#include "detect-ftp-command.h"

#define KEYWORD_NAME "ftp.command"
#define KEYWORD_DOC "ftp-keywords.html#ftp-command"
#define BUFFER_NAME "ftp.command"
#define BUFFER_DESC "ftp command"

static int g_ftp_cmd_buffer_id = 0;

static int DetectFtpCommandSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
{
if (DetectBufferSetActiveList(de_ctx, s, g_ftp_cmd_buffer_id) < 0)
return -1;

if (DetectSignatureSetAppProto(s, ALPROTO_FTP) < 0)
return -1;

return 0;
}

static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
const int list_id)
{
InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
if (buffer->inspect == NULL) {
FTPTransaction *tx = (FTPTransaction *)txv;

if (tx->command_descriptor->command_name == NULL ||
tx->command_descriptor->command_length == 0)
return NULL;

InspectionBufferSetup(det_ctx, list_id, buffer,
(const uint8_t *)tx->command_descriptor->command_name,
tx->command_descriptor->command_length);
InspectionBufferApplyTransforms(buffer, transforms);
}

return buffer;
}

void DetectFtpCommandRegister(void)
{
/* ftp.command sticky buffer */
sigmatch_table[DETECT_FTP_COMMAND].name = KEYWORD_NAME;
sigmatch_table[DETECT_FTP_COMMAND].desc = "sticky buffer to match on the FTP command buffer";
sigmatch_table[DETECT_FTP_COMMAND].url = "/rules/" KEYWORD_DOC;
sigmatch_table[DETECT_FTP_COMMAND].Setup = DetectFtpCommandSetup;
sigmatch_table[DETECT_FTP_COMMAND].flags |= SIGMATCH_NOOPT;

DetectAppLayerInspectEngineRegister(BUFFER_NAME, ALPROTO_FTP, SIG_FLAG_TOSERVER, 0,
DetectEngineInspectBufferGeneric, GetData);

DetectAppLayerMpmRegister(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister,
GetData, ALPROTO_FTP, 1);

DetectBufferTypeSetDescriptionByName(BUFFER_NAME, BUFFER_DESC);

g_ftp_cmd_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);

SCLogDebug("registering " BUFFER_NAME " rule option");
}
29 changes: 29 additions & 0 deletions src/detect-ftp-command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Copyright (C) 2025 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

/**
* \file
*
* \author Jeff Lucovsky <[email protected]>
*/

#ifndef SURICATA_DETECT_FTP_COMMAND_H
#define SURICATA_DETECT_FTP_COMMAND_H

void DetectFtpCommandRegister(void);

#endif /* SURICATA_DETECT_FTP_COMMAND_H */
Loading
Loading