use crate::{
base::Stix,
common::validation::{is_exact_vocab_value, validate_refs_are_type},
cyber_observable_objects::vocab::{
WindowsIntegrityEnum, WindowsServiceStartTypeEnum, WindowsServiceStatusEnum,
WindowsServiceTypeEnum,
},
error::{add_error, return_multiple_errors, StixError as Error},
types::{Identifier, StixDictionary},
};
use serde::{Deserialize, Serialize};
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 ProcessExtensions {
WindowsProcessExt(WindowsProcessExtension),
WindowsServiceExt(WindowsServiceExtension),
}
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WindowsProcessExtension {
pub aslr_enabled: Option<bool>,
pub dep_enabled: Option<bool>,
pub priority: Option<String>,
pub owner_sid: Option<String>,
pub window_title: Option<String>,
pub startup_info: Option<StixDictionary<Vec<String>>>,
pub integrity_level: Option<String>,
}
impl Stix for WindowsProcessExtension {
fn stix_check(&self) -> Result<(), Error> {
let mut errors = Vec::new();
if let Some(priority) = &self.priority {
if !priority.ends_with("_CLASS") {
errors.push(Error::ValidationError(format!(
"priority MUST end with '_CLASS'. Got '{}'.",
priority,
)));
}
}
if let Some(integrity_level) = &self.integrity_level {
if !is_exact_vocab_value::<WindowsIntegrityEnum, _>(integrity_level) {
errors.push(Error::ValidationError(format!(
"The values of integrity_level MUST come from the windows-integrity-level-enum enumeration. {}.",
integrity_level,
)));
}
}
if let Some(startup_info) = &self.startup_info {
add_error(&mut errors, startup_info.stix_check());
}
return_multiple_errors(errors)
}
}
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WindowsServiceExtension {
pub service_name: Option<String>,
pub descriptions: Option<Vec<String>>,
pub display_name: Option<String>,
pub group_name: Option<String>,
pub start_type: Option<String>,
pub service_dll_refs: Option<Vec<Identifier>>,
pub service_type: Option<String>,
pub service_status: Option<String>,
}
impl Stix for WindowsServiceExtension {
fn stix_check(&self) -> Result<(), Error> {
let mut errors = Vec::new();
if let Some(service_dll_refs) = &self.service_dll_refs {
add_error(
&mut errors,
validate_refs_are_type(service_dll_refs, &["file"], "service_dll_refs"),
);
}
if let Some(start_type) = &self.start_type {
if !is_exact_vocab_value::<WindowsServiceStartTypeEnum, _>(start_type) {
errors.push(Error::ValidationError(format!(
"The values of start_type MUST come from the windows-service-start-type-enum enumeration.. {}.",
start_type,
)));
}
}
if let Some(service_status) = &self.service_status {
if !is_exact_vocab_value::<WindowsServiceStatusEnum, _>(service_status) {
errors.push(Error::ValidationError(format!(
"The values of service_status MUST come from the windows-service-status-enum enumeration.. {}.",
service_status,
)));
}
}
if let Some(service_type) = &self.service_type {
if !is_exact_vocab_value::<WindowsServiceTypeEnum, _>(service_type) {
errors.push(Error::ValidationError(format!(
"The values of service_type MUST come from the windows-service-type-enum enumeration.. {}.",
service_type,
)));
}
}
return_multiple_errors(errors)
}
}