-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: log_message, error_message, security_message
Adds the possibility to store results within a Storage and adds the builtin functions: - log_message, - error_message, - security_message
- Loading branch information
1 parent
605a03d
commit 7b78eeb
Showing
12 changed files
with
335 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use std::sync::{Arc, RwLock}; | ||
|
||
use models::{Protocol, ResultType}; | ||
use nasl_builtin_utils::{Context, ContextType, FunctionErrorKind, Register}; | ||
use nasl_syntax::NaslValue; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
/// The description builtin function | ||
pub struct Reporting { | ||
id: Arc<RwLock<usize>>, | ||
} | ||
|
||
impl Reporting { | ||
fn id(&self) -> usize { | ||
let mut id = self.id.as_ref().write().expect("expected write lock"); | ||
let result = *id; | ||
*id += 1; | ||
result | ||
} | ||
|
||
fn store_result( | ||
&self, | ||
typus: ResultType, | ||
register: &Register, | ||
context: &Context, | ||
) -> Result<NaslValue, FunctionErrorKind> { | ||
let data = register.named("data").map(|x| x.to_string()); | ||
let port = register | ||
.named("port") | ||
.and_then(|x| match x { | ||
ContextType::Value(x) => Some(x.into()), | ||
_ => None, | ||
}) | ||
.map(|x: i64| x as i16); | ||
|
||
let protocol = match register | ||
.named("proto") | ||
.map(|x| x.to_string()) | ||
.as_ref() | ||
.map(|x| x as &str) | ||
{ | ||
Some("udp") => Protocol::UDP, | ||
_ => Protocol::TCP, | ||
}; | ||
// TOO: enhance id | ||
let result = models::Result { | ||
id: self.id(), | ||
r_type: typus, | ||
ip_address: Some(context.target().to_string()), | ||
// TODO: | ||
hostname: None, | ||
oid: Some(context.key().value()), | ||
port, | ||
protocol: Some(protocol), | ||
message: data, | ||
detail: None, | ||
}; | ||
context.dispatcher().retry_dispatch( | ||
5, | ||
context.key(), | ||
storage::Field::Result(result.into()), | ||
)?; | ||
Ok(NaslValue::Null) | ||
} | ||
|
||
/// *void* **log_message**(data: *string*, port:*int* , proto: *string*, uri: *string*); | ||
/// | ||
/// Creates a log result based on the given arguments | ||
/// - data, is the text report | ||
/// - port, optional TCP or UDP port number of the service | ||
/// - proto is the protocol ("tcp" by default; "udp" is the other value). | ||
/// - uri specifies the location of a found product | ||
fn log_message( | ||
&self, | ||
register: &Register, | ||
context: &Context, | ||
) -> Result<NaslValue, FunctionErrorKind> { | ||
self.store_result(ResultType::Log, register, context) | ||
} | ||
|
||
/// *void* **security_message**(data: *string*, port:*int* , proto: *string*, uri: *string*); | ||
/// | ||
/// Creates a alarm result based on the given arguments | ||
/// - data, is the text report | ||
/// - port, optional TCP or UDP port number of the service | ||
/// - proto is the protocol ("tcp" by default; "udp" is the other value). | ||
/// - uri specifies the location of a found product | ||
fn security_message( | ||
&self, | ||
register: &Register, | ||
context: &Context, | ||
) -> Result<NaslValue, FunctionErrorKind> { | ||
self.store_result(ResultType::Alarm, register, context) | ||
} | ||
|
||
/// *void* **error_message**(data: *string*, port:*int* , proto: *string*, uri: *string*); | ||
/// | ||
/// Creates a error result based on the given arguments | ||
/// - data, is the text report | ||
/// - port, optional TCP or UDP port number of the service | ||
/// - proto is the protocol ("tcp" by default; "udp" is the other value). | ||
/// - uri specifies the location of a found product | ||
fn error_message( | ||
&self, | ||
register: &Register, | ||
context: &Context, | ||
) -> Result<NaslValue, FunctionErrorKind> { | ||
self.store_result(ResultType::Error, register, context) | ||
} | ||
} | ||
|
||
impl nasl_builtin_utils::NaslFunctionExecuter for Reporting { | ||
fn nasl_fn_execute( | ||
&self, | ||
name: &str, | ||
register: &Register, | ||
context: &Context, | ||
) -> Option<nasl_builtin_utils::NaslResult> { | ||
match name { | ||
"log_message" => Some(self.log_message(register, context)), | ||
"security_message" => Some(self.security_message(register, context)), | ||
"error_message" => Some(self.error_message(register, context)), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn nasl_fn_defined(&self, name: &str) -> bool { | ||
matches!(name, "log_message" | "security_message" | "error_message") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// SPDX-FileCopyrightText: 2023 Greenbone AG | ||
// | ||
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use nasl_builtin_std::ContextFactory; | ||
use nasl_builtin_utils::Register; | ||
use nasl_interpreter::CodeInterpreter; | ||
|
||
fn verify(function: &str, result_type: models::ResultType) { | ||
let code = format!( | ||
r###" | ||
{function}(data: "test0", port: 12, proto: "udp", uri: "moep"); | ||
{function}(data: "test1", port: 12, proto: "tcp", uri: "moep"); | ||
{function}(data: "test2", port: 12, proto: "nonsense", uri: "moep"); | ||
{function}(data: "test3"); | ||
"### | ||
); | ||
let register = Register::default(); | ||
let binding = ContextFactory::default(); | ||
let context = binding.build(Default::default(), Default::default()); | ||
|
||
let mut parser = CodeInterpreter::new(&code, register, &context); | ||
let no_error = parser.find_map(|x| x.err()); | ||
assert_eq!( | ||
no_error, None, | ||
"there should be no error when creating log_messages" | ||
); | ||
let results = context | ||
.retriever() | ||
.results(context.key()) | ||
.unwrap() | ||
.collect::<Vec<_>>(); | ||
assert_eq!( | ||
results.len(), | ||
4, | ||
"expected the same results as log_message calls" | ||
); | ||
|
||
let create_expected = |id, port, protocol| models::Result { | ||
id, | ||
r_type: result_type.clone(), | ||
ip_address: Some(context.target().to_string()), | ||
hostname: None, | ||
oid: Some(context.key().value()), | ||
port, | ||
protocol: Some(protocol), | ||
message: Some(format!("test{id}")), | ||
detail: None, | ||
}; | ||
let udp = context | ||
.retriever() | ||
.result(context.key(), 0) | ||
.expect("expected udp result of first call") | ||
.unwrap(); | ||
let expected = create_expected(0, Some(12), models::Protocol::UDP); | ||
assert_eq!(udp, expected); | ||
|
||
let tcp = context | ||
.retriever() | ||
.result(context.key(), 1) | ||
.expect("expected udp result of first call") | ||
.unwrap(); | ||
let expected = create_expected(1, Some(12), models::Protocol::TCP); | ||
assert_eq!(tcp, expected); | ||
let defaults_to_tcp = context | ||
.retriever() | ||
.result(context.key(), 2) | ||
.expect("expected udp result of first call") | ||
.unwrap(); | ||
let expected = create_expected(2, Some(12), models::Protocol::TCP); | ||
assert_eq!(defaults_to_tcp, expected); | ||
let default = context | ||
.retriever() | ||
.result(context.key(), 3) | ||
.expect("expected udp result of first call") | ||
.unwrap(); | ||
let expected = create_expected(3, None, models::Protocol::TCP); | ||
assert_eq!(default, expected); | ||
} | ||
|
||
#[test] | ||
fn log_message() { | ||
verify("log_message", models::ResultType::Log) | ||
} | ||
|
||
#[test] | ||
fn security_message() { | ||
verify("security_message", models::ResultType::Alarm) | ||
} | ||
|
||
#[test] | ||
fn error_message() { | ||
verify("error_message", models::ResultType::Error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.