use crate::{
base::Stix,
common::validation::{is_exact_vocab_value, is_valid_hex},
cyber_observable_objects::vocab::{NetworkSocketAddressFamilyEnum, NetworkSocketTypeEnum},
error::{add_error, return_multiple_errors, StixError as Error},
types::{DictionaryValue, Identifier, StixDictionary},
};
use log::warn;
use serde::{Deserialize, Serialize};
use serde_this_or_that::as_opt_u64;
use serde_with::skip_serializing_none;
use strum::{AsRefStr, EnumIter};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, AsRefStr, EnumIter)]
#[serde(untagged)]
#[strum(serialize_all = "kebab-case")]
pub enum NetworkTrafficExtensions {
IcmpExt(IcmpExtension),
HttpRequestExt(HttpRequestExtension),
SocketExt(SocketExtenion),
TcpExt(TcpExtension),
}
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct IcmpExtension {
pub icmp_type_hex: String,
pub icmp_code_hex: String,
}
impl Stix for IcmpExtension {
fn stix_check(&self) -> Result<(), Error> {
let mut errors = Vec::new();
let icmp_type_hex = &self.icmp_type_hex;
if !is_valid_hex(icmp_type_hex) {
errors.push(Error::ParseHexError(
"icmp_type_hex -- ".to_string() + icmp_type_hex,
))
}
let icmp_code_hex = &self.icmp_code_hex;
if !is_valid_hex(icmp_code_hex) {
errors.push(Error::ParseHexError(
"icmp_code_hex -- ".to_string() + icmp_code_hex,
))
}
return_multiple_errors(errors)
}
}
const HTTP_REQUEST_HEADERS: &[&str] = &[
"Accept",
"Accept-Charset",
"Accept-Encoding",
"Accept-Language",
"Accept-Datetime",
"Authorization",
"Cache-Control",
"Connection",
"Cookie",
"Content-Length",
"Content-MD5",
"Content-Type",
"Date",
"Expect",
"Forwarded",
"From",
"Host",
"If-Match",
"If-Modified-Since",
"If-None-Match",
"If-Range",
"If-Unmodified-Since",
"Max-Forwards",
"Origin",
"Pragma",
"Proxy-Authorization",
"Range",
"Referer",
"TE",
"User-Agent",
"Upgrade",
"Via",
"Warning",
"X-Requested-With",
"DNT",
"X-Forwarded-For",
"X-Forwarded-Host",
"X-Forwarded-Proto",
"Front-End-Https",
"X-Http-Method-Override",
"X-ATT-DeviceId",
"X-Wap-Profile",
"Proxy-Connection",
"X-UIDH",
"X-Csrf-Token",
"X-Request-ID",
"X-Correlation-ID",
];
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HttpRequestExtension {
pub request_method: String,
pub request_value: String,
pub request_version: Option<String>,
pub request_header: Option<StixDictionary<String>>,
#[serde(default, deserialize_with = "as_opt_u64")]
pub message_body_length: Option<u64>,
pub message_body_data_ref: Option<Identifier>,
}
impl Stix for HttpRequestExtension {
fn stix_check(&self) -> Result<(), Error> {
let mut errors = Vec::new();
if let Some(message_body_length) = &self.message_body_length {
add_error(&mut errors, message_body_length.stix_check());
}
if let Some(message_body_data_ref) = &self.message_body_data_ref {
message_body_data_ref.stix_check()?;
if message_body_data_ref.get_type() != "artifact" {
errors.push(Error::ValidationError(format!(
"message_body_data_ref {} must be of type 'artifact'.",
message_body_data_ref.get_type()
)));
}
}
if let Some(request_header) = &self.request_header {
add_error(&mut errors, request_header.stix_check());
for (key, _) in request_header.iter() {
if !HTTP_REQUEST_HEADERS.contains(&key.as_str()) {
errors.push(Error::ValidationError(format!(
"The 'request_header' property contains an invalid HTTP request header ('{}').",
key
)));
}
}
}
return_multiple_errors(errors)
}
}
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SocketExtenion {
pub address_family: Option<String>,
pub is_blocking: Option<bool>,
pub is_listening: Option<bool>,
pub options: Option<StixDictionary<DictionaryValue>>,
pub socket_type: Option<String>,
#[serde(default, deserialize_with = "as_opt_u64")]
pub socket_descriptor: Option<u64>,
#[serde(default, deserialize_with = "as_opt_u64")]
pub socket_handle: Option<u64>,
}
impl Stix for SocketExtenion {
fn stix_check(&self) -> Result<(), Error> {
let mut errors = Vec::new();
if let Some(socket_descriptor) = &self.socket_descriptor {
add_error(&mut errors, socket_descriptor.stix_check());
}
if let Some(address_family) = &self.address_family {
if !is_exact_vocab_value::<NetworkSocketAddressFamilyEnum, _>(address_family) {
errors.push(Error::ValidationError(format!(
"The values of this property MUST come from the network-socket-address-family-enum enumeration. {}.",
address_family,
)));
}
}
if let Some(socket_type) = &self.socket_type {
if !is_exact_vocab_value::<NetworkSocketTypeEnum, _>(socket_type) {
errors.push(Error::ValidationError(format!(
"The values of this property MUST come from the network-socket-type-enum enumeration. {}.",
socket_type,
)));
}
}
if let Some(options) = &self.options {
add_error(&mut errors, options.stix_check());
}
return_multiple_errors(errors)
}
}
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TcpExtension {
pub src_flags_hex: Option<String>,
pub dst_flags_hex: Option<String>,
}
impl Stix for TcpExtension {
fn stix_check(&self) -> Result<(), Error> {
if let Some(src_flags_hex) = &self.src_flags_hex {
warn!("If the start and end times of the traffic are not specified, src_flags_hex {} SHOULD be interpreted as the union of all TCP flags observed over the entirety of the network traffic being reported upon.",src_flags_hex);
}
if let Some(dst_flags_hex) = &self.dst_flags_hex {
warn!("If the start and end times of the traffic are not specified, dst_flags_hex {} SHOULD be interpreted as the union of all TCP flags observed over the entirety of the network traffic being reported upon.",dst_flags_hex);
}
let mut errors = Vec::new();
if let Some(src_flags_hex) = &self.src_flags_hex {
if !is_valid_hex(src_flags_hex) {
errors.push(Error::ParseHexError(
"src_flags_hex -- ".to_string() + src_flags_hex,
))
}
}
if let Some(dst_flags_hex) = &self.dst_flags_hex {
if !is_valid_hex(dst_flags_hex) {
errors.push(Error::ParseHexError(
"dst_flags_hex -- ".to_string() + dst_flags_hex,
))
}
}
return_multiple_errors(errors)
}
}