Skip to content

Commit 16d553c

Browse files
authored
Add: File type script preferences related functions and other nasl builtin (#1965)
* Fix: builtin_coverage.nasl after fixing the make_array() nasl function, this nasl script didn't run anymore. * Add: safe_checks and get_script_oid() nasl functions * Add: function to get file type script preferences and also thefunctions to store the preference in a temporary file * make function visible to nasl * fix some stuff after rebase
1 parent 3cd7b36 commit 16d553c

File tree

8 files changed

+249
-82
lines changed

8 files changed

+249
-82
lines changed

rust/Cargo.lock

Lines changed: 23 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/doc/misc/builtin_coverage.nasl

Lines changed: 156 additions & 53 deletions
Large diffs are not rendered by default.

rust/src/nasl/builtin/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ pub enum BuiltinError {
5151
#[cfg(feature = "nasl-builtin-raw-ip")]
5252
#[error("{0}")]
5353
RawIp(RawIpError),
54+
#[error("{0}")]
55+
Preference(String),
5456
}
5557

5658
macro_rules! builtin_error_variant (

rust/src/nasl/builtin/misc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
- defined_func
1616
- gettimeofday
1717
- dump_ctxt
18+
- vendor_version

rust/src/nasl/builtin/misc/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,20 @@ fn vendor_version(context: &ScanCtx<'_>) -> Result<NaslValue, MiscError> {
290290
}
291291
}
292292

293+
/// Are safe checks enabled ?
294+
#[nasl_function]
295+
fn safe_checks(context: &ScanCtx<'_>) -> bool {
296+
context
297+
.scan_preferences
298+
.get_preference_bool("safe_checks")
299+
.unwrap_or(false)
300+
}
301+
302+
#[nasl_function]
303+
fn get_script_oid(context: &ScanCtx<'_>) -> Option<Vec<u8>> {
304+
context.nvt().clone().map(|nvt| nvt.oid.into_bytes())
305+
}
306+
293307
pub struct Misc;
294308

295309
function_set! {
@@ -311,6 +325,8 @@ function_set! {
311325
gettimeofday,
312326
dump_ctxt,
313327
vendor_version,
328+
safe_checks,
329+
get_script_oid,
314330
)
315331
}
316332

rust/src/nasl/builtin/preferences/mod.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
use crate::models::PreferenceValue;
55
use crate::nasl::prelude::*;
66
use crate::scanner::preferences::preference::PREFERENCES;
7-
#[nasl_function(named(id))]
8-
fn script_get_preference(
7+
use base64::Engine as _;
8+
9+
fn script_get_preference_shared(
910
register: &Register,
1011
config: &ScanCtx,
1112
name: Option<String>,
@@ -42,6 +43,39 @@ fn script_get_preference(
4243
None
4344
}
4445

46+
fn script_get_preference_file_content_shared(
47+
register: &Register,
48+
config: &ScanCtx,
49+
name: Option<String>,
50+
id: Option<usize>,
51+
) -> Option<Vec<u8>> {
52+
let content = script_get_preference_shared(register, config, name, id)?;
53+
let content = content.as_string().unwrap();
54+
base64::engine::general_purpose::STANDARD
55+
.decode(content)
56+
.ok()
57+
}
58+
59+
#[nasl_function(named(id))]
60+
fn script_get_preference_file_content(
61+
register: &Register,
62+
config: &ScanCtx,
63+
name: Option<String>,
64+
id: Option<usize>,
65+
) -> Option<Vec<u8>> {
66+
script_get_preference_file_content_shared(register, config, name, id)
67+
}
68+
69+
#[nasl_function(named(id))]
70+
fn script_get_preference(
71+
register: &Register,
72+
config: &ScanCtx,
73+
name: Option<String>,
74+
id: Option<usize>,
75+
) -> Option<NaslValue> {
76+
script_get_preference_shared(register, config, name, id)
77+
}
78+
4579
#[nasl_function]
4680
fn get_preference(config: &ScanCtx, name: String) -> Option<NaslValue> {
4781
let val = if let Some(pref) = config.scan_params().find(|p| p.id == name) {
@@ -76,5 +110,6 @@ function_set! {
76110
(
77111
script_get_preference,
78112
get_preference,
113+
script_get_preference_file_content,
79114
)
80115
}

rust/src/nasl/utils/scan_ctx.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::models::{AliveTestMethods, Port, PortRange, Protocol, ScanPreference}
1111
use crate::nasl::builtin::{KBError, NaslSockets};
1212
use crate::nasl::syntax::Loader;
1313
use crate::nasl::{FromNaslValue, WithErrorInfo};
14-
use crate::scanner::preferences::preference::ScanPrefs;
14+
use crate::scanner::preferences::preference::{ScanPrefs, pref_is_true};
1515
use crate::storage::error::StorageError;
1616
use crate::storage::infisto::json::JsonStorage;
1717
use crate::storage::inmemory::InMemoryStorage;
@@ -568,10 +568,8 @@ impl<'a> ScanCtx<'a> {
568568
}
569569

570570
fn get_preference_bool(&self, key: &str) -> Option<bool> {
571-
self.scan_preferences
572-
.iter()
573-
.find(|x| x.id == key)
574-
.map(|x| matches!(x.value.as_str(), "true" | "1" | "yes"))
571+
let prefs = &self.scan_preferences;
572+
pref_is_true(prefs, key)
575573
}
576574

577575
pub fn get_port_state(&self, port: u16, protocol: Protocol) -> Result<bool, FnError> {

rust/src/scanner/preferences/preference.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ lazy_static! {
233233
pub static ref PREFERENCES_JSON: String = serde_json::to_string(&PREFERENCES).unwrap();
234234
}
235235

236+
pub fn pref_is_true(prefs: &ScanPrefs, key: &str) -> Option<bool> {
237+
prefs
238+
.iter()
239+
.find(|x| x.id == key)
240+
.map(|x| matches!(x.value.as_str(), "true" | "1" | "yes"))
241+
}
242+
236243
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq)]
237244
#[serde(untagged)]
238245
pub enum ScanPrefValue {
@@ -384,6 +391,10 @@ impl ScanPrefs {
384391
self.0.iter().find(|x| x.id == key).map(|x| x.value.clone())
385392
}
386393

394+
pub fn get_preference_bool(&self, key: &str) -> Option<bool> {
395+
pref_is_true(self, key)
396+
}
397+
387398
pub fn iter(&self) -> impl Iterator<Item = &ScanPreference> {
388399
self.0.iter()
389400
}

0 commit comments

Comments
 (0)