|
|
|
/* Copyright (C) 2007-2022 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "packet.h"
|
|
|
|
#include "pkt-var.h"
|
|
|
|
#include "flow.h"
|
|
|
|
#include "host.h"
|
|
|
|
#include "util-profiling.h"
|
|
|
|
#include "util-validate.h"
|
|
|
|
#include "action-globals.h"
|
|
|
|
#include "app-layer-events.h"
|
|
|
|
|
|
|
|
/** \brief issue drop action
|
|
|
|
*
|
|
|
|
* Set drop (+reject) flags in both current and root packet.
|
|
|
|
*
|
|
|
|
* \param action action bit flags. Must be limited to ACTION_DROP_REJECT|ACTION_ALERT
|
|
|
|
*/
|
|
|
|
void PacketDrop(Packet *p, const uint8_t action, enum PacketDropReason r)
|
|
|
|
{
|
|
|
|
DEBUG_VALIDATE_BUG_ON((action & ~(ACTION_DROP_REJECT | ACTION_ALERT)) != 0);
|
|
|
|
|
|
|
|
if (p->drop_reason == PKT_DROP_REASON_NOT_SET)
|
|
|
|
p->drop_reason = (uint8_t)r;
|
|
|
|
|
|
|
|
if (p->root) {
|
|
|
|
p->root->action |= action;
|
|
|
|
if (p->root->drop_reason == PKT_DROP_REASON_NOT_SET) {
|
|
|
|
p->root->drop_reason = PKT_DROP_REASON_INNER_PACKET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p->action |= action;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PacketCheckAction(const Packet *p, const uint8_t a)
|
|
|
|
{
|
|
|
|
if (likely(p->root == NULL)) {
|
|
|
|
return (p->action & a) != 0;
|
|
|
|
} else {
|
|
|
|
/* check against both */
|
|
|
|
const uint8_t actions = p->action | p->root->action;
|
|
|
|
return (actions & a) != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Initialize a packet structure for use.
|
|
|
|
*/
|
|
|
|
void PacketInit(Packet *p)
|
|
|
|
{
|
|
|
|
SCSpinInit(&p->persistent.tunnel_lock, 0);
|
|
|
|
p->alerts.alerts = PacketAlertCreate();
|
|
|
|
p->livedev = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PacketReleaseRefs(Packet *p)
|
|
|
|
{
|
|
|
|
FlowDeReference(&p->flow);
|
|
|
|
HostDeReference(&p->host_src);
|
|
|
|
HostDeReference(&p->host_dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Recycle a packet structure for reuse.
|
|
|
|
*/
|
|
|
|
void PacketReinit(Packet *p)
|
|
|
|
{
|
|
|
|
/* clear the address structure by setting all fields to 0 */
|
|
|
|
#define CLEAR_ADDR(a) \
|
|
|
|
do { \
|
|
|
|
(a)->family = 0; \
|
|
|
|
(a)->addr_data32[0] = 0; \
|
|
|
|
(a)->addr_data32[1] = 0; \
|
|
|
|
(a)->addr_data32[2] = 0; \
|
|
|
|
(a)->addr_data32[3] = 0; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
CLEAR_ADDR(&p->src);
|
|
|
|
CLEAR_ADDR(&p->dst);
|
|
|
|
p->sp = 0;
|
|
|
|
p->dp = 0;
|
|
|
|
p->proto = 0;
|
|
|
|
p->recursion_level = 0;
|
|
|
|
PACKET_FREE_EXTDATA(p);
|
|
|
|
p->app_update_direction = 0;
|
|
|
|
p->sig_mask = 0;
|
|
|
|
p->pkt_hooks = 0;
|
|
|
|
p->flags = 0;
|
|
|
|
p->flowflags = 0;
|
|
|
|
p->pkt_src = 0;
|
|
|
|
p->vlan_id[0] = 0;
|
|
|
|
p->vlan_id[1] = 0;
|
|
|
|
p->vlan_idx = 0;
|
|
|
|
p->ttype = PacketTunnelNone;
|
|
|
|
SCTIME_INIT(p->ts);
|
|
|
|
p->datalink = 0;
|
|
|
|
p->drop_reason = 0;
|
|
|
|
#define PACKET_RESET_ACTION(p) (p)->action = 0
|
|
|
|
PACKET_RESET_ACTION(p);
|
|
|
|
if (p->pktvar != NULL) {
|
|
|
|
PktVarFree(p->pktvar);
|
|
|
|
p->pktvar = NULL;
|
|
|
|
}
|
|
|
|
PacketClearL2(p);
|
|
|
|
PacketClearL3(p);
|
|
|
|
PacketClearL4(p);
|
|
|
|
p->payload = NULL;
|
|
|
|
p->payload_len = 0;
|
|
|
|
p->BypassPacketsFlow = NULL;
|
|
|
|
#define RESET_PKT_LEN(p) ((p)->pktlen = 0)
|
|
|
|
RESET_PKT_LEN(p);
|
|
|
|
p->alerts.cnt = 0;
|
|
|
|
p->alerts.discarded = 0;
|
|
|
|
p->alerts.suppressed = 0;
|
|
|
|
p->alerts.drop.action = 0;
|
datajson: introduce feature
This patch introduces new option to dataset keyword.
Where regular dataset allows match from sets, dataset with json
format allows the same but also adds JSON data to the alert
event. This data is coming from the set definition it self.
For example, an ipv4 set will look like:
[{"ip": "10.16.1.11", "test": "success","context":3}]
The syntax is a JSON array but it can also be a JSON object
with an array inside. The idea is to directly used data coming
from the API of a threat intel management software.
The syntax of the keyword is the following:
dataset:isset,src_ip,type ip,load src.lst,format json, \
enrichment_key src_ip, value_key ip;
Compare to dataset, it just have a supplementary option key
that is used to indicate in which subobject the JSON value
should be added.
The information is added in the even under the alert.extra
subobject:
"alert": {
"extra": {
"src_ip": {
"ip": "10.6.1.11",
"test": "success",
"context": 3
},
The main interest of the feature is to be able to contextualize
a match. For example, if you have an IOC source, you can do
[
{"buffer": "value1", "actor":"APT28","Country":"FR"},
{"buffer": "value2", "actor":"APT32","Country":"NL"}
]
This way, a single dataset is able to produce context to the
event where it was not possible before and multiple signatures
had to be used.
The format introduced in datajson is an evolution of the
historical datarep format. This has some limitations. For example,
if a user fetch IOCs from a threat intel server there is a large
change that the format will be JSON or XML. Suricata has no support
for the second but can support the first one.
Keeping the key value may seem redundant but it is useful to have it
directly accessible in the extra data to be able to query it
independantly of the signature (where it can be multiple metadata
or even be a transformed metadata).
In some case, when interacting with data (mostly coming from
threat intel servers), the JSON array containing the data
to use is not at the root of the object and it is ncessary
to access a subobject.
This patch implements this with support of key in level1.level2.
This is done via the `array_key` option that contains the path
to the data.
Ticket: #7372
5 months ago
|
|
|
PacketAlertRecycle(p->alerts.alerts);
|
|
|
|
p->pcap_cnt = 0;
|
|
|
|
p->tunnel_rtv_cnt = 0;
|
|
|
|
p->tunnel_tpr_cnt = 0;
|
|
|
|
p->events.cnt = 0;
|
|
|
|
AppLayerDecoderEventsResetEvents(p->app_layer_events);
|
|
|
|
p->next = NULL;
|
|
|
|
p->prev = NULL;
|
|
|
|
p->tunnel_verdicted = false;
|
|
|
|
p->root = NULL;
|
|
|
|
p->livedev = NULL;
|
|
|
|
PACKET_PROFILING_RESET(p);
|
|
|
|
p->tenant_id = 0;
|
|
|
|
p->nb_decoded_layers = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PacketRecycle(Packet *p)
|
|
|
|
{
|
|
|
|
PacketReleaseRefs(p);
|
|
|
|
PacketReinit(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Cleanup a packet so that we can free it. No memset needed..
|
|
|
|
*/
|
|
|
|
void PacketDestructor(Packet *p)
|
|
|
|
{
|
|
|
|
PacketReleaseRefs(p);
|
|
|
|
if (p->pktvar != NULL) {
|
|
|
|
PktVarFree(p->pktvar);
|
|
|
|
}
|
|
|
|
PacketAlertFree(p->alerts.alerts);
|
|
|
|
PACKET_FREE_EXTDATA(p);
|
|
|
|
SCSpinDestroy(&p->persistent.tunnel_lock);
|
|
|
|
AppLayerDecoderEventsFreeEvents(&p->app_layer_events);
|
|
|
|
PACKET_PROFILING_RESET(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SCPacketSetReleasePacket(Packet *p, void (*ReleasePacket)(Packet *p))
|
|
|
|
{
|
|
|
|
p->ReleasePacket = ReleasePacket;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SCPacketSetLiveDevice(Packet *p, LiveDevice *device)
|
|
|
|
{
|
|
|
|
p->livedev = device;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SCPacketSetDatalink(Packet *p, int datalink)
|
|
|
|
{
|
|
|
|
p->datalink = datalink;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SCPacketSetTime(Packet *p, SCTime_t ts)
|
|
|
|
{
|
|
|
|
p->ts = ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SCPacketSetSource(Packet *p, enum PktSrcEnum source)
|
|
|
|
{
|
|
|
|
p->pkt_src = (uint8_t)source;
|
|
|
|
}
|