diff --git a/rust/Cargo.lock b/rust/Cargo.lock index f8b3ad3e0..a305a4409 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "addr2line" -version = "0.21.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" dependencies = [ "gimli", ] @@ -123,9 +123,9 @@ dependencies = [ [[package]] name = "anstyle-query" -version = "1.0.3" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" dependencies = [ "windows-sys 0.52.0", ] @@ -163,7 +163,7 @@ checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca" dependencies = [ "proc-macro2", "quote", - "syn 2.0.65", + "syn 2.0.66", ] [[package]] @@ -180,9 +180,9 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "aws-lc-rs" -version = "1.7.1" +version = "1.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8487b59d62764df8231cb371c459314df895b41756df457a1fb1243d65c89195" +checksum = "bf7d844e282b4b56750b2d4e893b2205581ded8709fddd2b6aa5418c150ca877" dependencies = [ "aws-lc-sys", "mirai-annotations", @@ -192,9 +192,9 @@ dependencies = [ [[package]] name = "aws-lc-sys" -version = "0.16.0" +version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c15eb61145320320eb919d9bab524617a7aa4216c78d342fae3a758bc33073e4" +checksum = "c3a2c29203f6bf296d01141cc8bb9dbd5ecd4c27843f2ee0767bcd5985a927da" dependencies = [ "bindgen", "cc", @@ -207,9 +207,9 @@ dependencies = [ [[package]] name = "backtrace" -version = "0.3.71" +version = "0.3.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" +checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" dependencies = [ "addr2line", "cc", @@ -257,7 +257,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.65", + "syn 2.0.66", "which", ] @@ -378,9 +378,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.98" +version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f" +checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695" dependencies = [ "jobserver", "libc", @@ -478,9 +478,9 @@ dependencies = [ [[package]] name = "clang-sys" -version = "1.7.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67523a3b4be3ce1989d607a828d036249522dd9c1c8de7f4dd2dae43a37369d1" +checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" dependencies = [ "glob", "libc", @@ -489,9 +489,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.4" +version = "4.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +checksum = "5db83dced34638ad474f39f250d7fea9598bdd239eaced1bdf45d597da0f433f" dependencies = [ "clap_builder", "clap_derive", @@ -499,9 +499,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.2" +version = "4.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +checksum = "f7e204572485eb3fbf28f871612191521df159bc3e15a9f5064c66dba3a8c05f" dependencies = [ "anstream", "anstyle", @@ -511,21 +511,21 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.4" +version = "4.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6" dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.65", + "syn 2.0.66", ] [[package]] name = "clap_lex" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" +checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" [[package]] name = "cmac" @@ -565,9 +565,9 @@ dependencies = [ [[package]] name = "configparser" -version = "3.0.4" +version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ec6d3da8e550377a85339063af6e3735f4b1d9392108da4e083a1b3b9820288" +checksum = "e57e3272f0190c3f1584272d613719ba5fc7df7f4942fe542e63d949cf3a649b" [[package]] name = "core-foundation" @@ -798,6 +798,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "displaydoc" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + [[package]] name = "dunce" version = "1.0.4" @@ -891,7 +902,7 @@ dependencies = [ "serde", "sha2", "storage", - "toml 0.8.13", + "toml 0.8.14", "tracing", ] @@ -1021,7 +1032,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.65", + "syn 2.0.66", ] [[package]] @@ -1098,9 +1109,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.28.1" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" [[package]] name = "glob" @@ -1243,12 +1254,12 @@ dependencies = [ [[package]] name = "http-body-util" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d" +checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" dependencies = [ "bytes", - "futures-core", + "futures-util", "http 1.1.0", "http-body 1.0.0", "pin-project-lite", @@ -1256,9 +1267,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.8.0" +version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" +checksum = "d0e7a4dd27b9476dc40cb050d3632d3bba3a70ddbff012285f7f8559a1e7e545" [[package]] name = "httpdate" @@ -1268,9 +1279,9 @@ checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" [[package]] name = "hyper" -version = "0.14.28" +version = "0.14.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" +checksum = "f361cde2f109281a220d4307746cdfd5ee3f410da58a70377762396775634b33" dependencies = [ "bytes", "futures-channel", @@ -1319,7 +1330,7 @@ checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" dependencies = [ "futures-util", "http 0.2.12", - "hyper 0.14.28", + "hyper 0.14.29", "rustls 0.21.12", "tokio", "tokio-rustls 0.24.1", @@ -1327,16 +1338,16 @@ dependencies = [ [[package]] name = "hyper-rustls" -version = "0.27.1" +version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "908bb38696d7a037a01ebcc68a00634112ac2bbf8ca74e30a2c3d2f4f021302b" +checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155" dependencies = [ "futures-util", "http 1.1.0", "hyper 1.3.1", "hyper-util", "log", - "rustls 0.23.7", + "rustls 0.23.10", "rustls-native-certs", "rustls-pki-types", "tokio", @@ -1346,9 +1357,9 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.3" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa" +checksum = "7b875924a60b96e5d7b9ae7b066540b1dd1cbd90d1828f54c92e02a283351c56" dependencies = [ "bytes", "futures-channel", @@ -1387,6 +1398,124 @@ dependencies = [ "cc", ] +[[package]] +name = "icu_collections" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db2fa452206ebee18c4b5c2274dbf1de17008e874b4dc4f0aea9d01ca79e4526" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locid" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13acbb8371917fc971be86fc8057c41a64b521c184808a698c02acc242dbf637" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_locid_transform" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01d11ac35de8e40fdeda00d9e1e9d92525f3f9d887cdd7aa81d727596788b54e" +dependencies = [ + "displaydoc", + "icu_locid", + "icu_locid_transform_data", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_locid_transform_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdc8ff3388f852bede6b579ad4e978ab004f139284d7b28715f773507b946f6e" + +[[package]] +name = "icu_normalizer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19ce3e0da2ec68599d193c93d088142efd7f9c5d6fc9b803774855747dc6a84f" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "utf16_iter", + "utf8_iter", + "write16", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8cafbf7aa791e9b22bec55a167906f9e1215fd475cd22adfcf660e03e989516" + +[[package]] +name = "icu_properties" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f8ac670d7422d7f76b32e17a5db556510825b29ec9154f235977c9caba61036" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locid_transform", + "icu_properties_data", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67a8effbc3dd3e4ba1afa8ad918d5684b8868b3b26500753effea8d2eed19569" + +[[package]] +name = "icu_provider" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ed421c8a8ef78d3e2dbc98a973be2f3770cb42b606e3ab18d6237c4dfde68d9" +dependencies = [ + "displaydoc", + "icu_locid", + "icu_provider_macros", + "stable_deref_trait", + "tinystr", + "writeable", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_provider_macros" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + [[package]] name = "idna" version = "0.5.0" @@ -1397,6 +1526,18 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "idna" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4716a3a0933a1d01c2f72450e89596eb51dd34ef3c211ccd875acdf1f8fe47ed" +dependencies = [ + "icu_normalizer", + "icu_properties", + "smallvec", + "utf8_iter", +] + [[package]] name = "indexmap" version = "2.2.6" @@ -1535,7 +1676,7 @@ dependencies = [ "lalrpop-util", "petgraph", "regex", - "regex-syntax 0.8.3", + "regex-syntax 0.8.4", "string_cache", "term", "tiny-keccak", @@ -1549,7 +1690,7 @@ version = "0.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "507460a910eb7b32ee961886ff48539633b788a36b65692b95f225b844c82553" dependencies = [ - "regex-automata 0.4.6", + "regex-automata 0.4.7", ] [[package]] @@ -1572,7 +1713,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.65", + "syn 2.0.66", ] [[package]] @@ -1653,9 +1794,9 @@ dependencies = [ [[package]] name = "libz-sys" -version = "1.1.16" +version = "1.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e143b5e666b2695d28f6bca6497720813f699c9602dd7f5cac91008b8ada7f9" +checksum = "c15da26e5af7e25c90b37a2d75cdbf940cf4a55316de9d84c679c9b8bfabf82e" dependencies = [ "cc", "libc", @@ -1669,6 +1810,12 @@ version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" +[[package]] +name = "litemap" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "643cb0b8d4fcc284004d5fd0d67ccf61dfffadb7f75e1e71bc420f4688a3a704" + [[package]] name = "lock_api" version = "0.4.12" @@ -1733,9 +1880,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.7.2" +version = "2.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" +checksum = "6d0d8b92cd8358e8d229c11df9358decae64d137c5be540952c5ca7b25aea768" [[package]] name = "memsec" @@ -1743,6 +1890,12 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fa0916b001582d253822171bd23f4a0229d32b9507fae236f5da8cad515ba7c" +[[package]] +name = "memsec" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c797b9d6bb23aab2fc369c65f871be49214f5c759af65bde26ffaaa2b646b492" + [[package]] name = "mime" version = "0.3.17" @@ -1847,7 +2000,7 @@ dependencies = [ "nasl-builtin-utils", "nasl-interpreter", "nasl-syntax", - "rustls 0.23.7", + "rustls 0.23.10", "rustls-pemfile 2.1.2", "storage", "tokio", @@ -2054,9 +2207,9 @@ dependencies = [ [[package]] name = "object" -version = "0.32.2" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +checksum = "576dfe1fc8f9df304abb159d767a29d0476f7750fbf8aa7ad07816004a207434" dependencies = [ "memchr", ] @@ -2102,7 +2255,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.65", + "syn 2.0.66", ] [[package]] @@ -2113,9 +2266,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-src" -version = "300.3.0+3.3.0" +version = "300.3.1+3.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eba8804a1c5765b18c4b3f907e6897ebabeedebc9830e1a0046c4a4cf44663e1" +checksum = "7259953d42a81bf137fbbd73bd30a8e1914d6dce43c2b90ed575783a22608b91" dependencies = [ "cc", ] @@ -2163,7 +2316,7 @@ dependencies = [ "http-body 1.0.0", "http-body-util", "hyper 1.3.1", - "hyper-rustls 0.27.1", + "hyper-rustls 0.27.2", "hyper-util", "infisto", "lazy_static", @@ -2211,9 +2364,9 @@ checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" [[package]] name = "parking_lot" -version = "0.12.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e4af0ca4f6caed20e900d564c242b8e5d4903fdacf31d3daf527b66fe6f42fb" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" dependencies = [ "lock_api", "parking_lot_core", @@ -2317,7 +2470,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.65", + "syn 2.0.66", ] [[package]] @@ -2503,14 +2656,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" dependencies = [ "proc-macro2", - "syn 2.0.65", + "syn 2.0.66", ] [[package]] name = "proc-macro2" -version = "1.0.83" +version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b33eb56c327dec362a9e55b3ad14f9d2f0904fb5a5b03b513ab5465399e9f43" +checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23" dependencies = [ "unicode-ident", ] @@ -2632,14 +2785,14 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.4" +version = "1.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.4.6", - "regex-syntax 0.8.3", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", ] [[package]] @@ -2653,13 +2806,13 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.8.3", + "regex-syntax 0.8.4", ] [[package]] @@ -2670,9 +2823,9 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "regex-syntax" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" [[package]] name = "reqwest" @@ -2688,7 +2841,7 @@ dependencies = [ "h2 0.3.26", "http 0.2.12", "http-body 0.4.6", - "hyper 0.14.28", + "hyper 0.14.29", "hyper-rustls 0.24.2", "ipnet", "js-sys", @@ -2814,9 +2967,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.7" +version = "0.23.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebbbdb961df0ad3f2652da8f3fdc4b36122f568f968f45ad3316f26c025c677b" +checksum = "05cff451f60db80f490f3c182b77c35260baace73209e9cdbbe526bfe3a4d402" dependencies = [ "aws-lc-rs", "log", @@ -2925,7 +3078,7 @@ dependencies = [ "serde", "serde_json", "storage", - "toml 0.8.13", + "toml 0.8.14", "tracing", "tracing-subscriber 0.3.18", "walkdir", @@ -2997,7 +3150,7 @@ dependencies = [ "lalrpop-util", "lazy_static", "libc", - "memsec", + "memsec 0.7.0", "rand", "sequoia-openpgp", "socket2 0.4.10", @@ -3020,17 +3173,17 @@ dependencies = [ "chrono", "dyn-clone", "getrandom", - "idna", + "idna 0.5.0", "lalrpop", "lalrpop-util", "lazy_static", "libc", - "memsec", + "memsec 0.6.3", "once_cell", "openssl", "openssl-sys", "regex", - "regex-syntax 0.8.3", + "regex-syntax 0.8.4", "sha1collisiondetection", "thiserror", "xxhash-rust", @@ -3038,22 +3191,22 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.202" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.202" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" dependencies = [ "proc-macro2", "quote", - "syn 2.0.65", + "syn 2.0.66", ] [[package]] @@ -3211,6 +3364,12 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + [[package]] name = "storage" version = "0.1.0" @@ -3259,9 +3418,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.65" +version = "2.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2863d96a84c6439701d7a38f9de935ec562c8832cc55d1dde0f513b52fad106" +checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" dependencies = [ "proc-macro2", "quote", @@ -3274,6 +3433,17 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "synstructure" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + [[package]] name = "sysinfo" version = "0.30.12" @@ -3350,7 +3520,7 @@ checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" dependencies = [ "proc-macro2", "quote", - "syn 2.0.65", + "syn 2.0.66", ] [[package]] @@ -3402,6 +3572,16 @@ dependencies = [ "crunchy", ] +[[package]] +name = "tinystr" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9117f5d4db391c1cf6927e7bea3db74b9a1c1add8f7eda9ffd5364f40f57b82f" +dependencies = [ + "displaydoc", + "zerovec", +] + [[package]] name = "tinytemplate" version = "1.2.1" @@ -3429,9 +3609,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.37.0" +version = "1.38.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" +checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" dependencies = [ "backtrace", "bytes", @@ -3448,13 +3628,13 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.65", + "syn 2.0.66", ] [[package]] @@ -3484,7 +3664,7 @@ version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" dependencies = [ - "rustls 0.23.7", + "rustls 0.23.10", "rustls-pki-types", "tokio", ] @@ -3517,14 +3697,14 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.13" +version = "0.8.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4e43f8cc456c9704c851ae29c67e17ef65d2c30017c17a9765b89c382dc8bba" +checksum = "6f49eb2ab21d2f26bd6db7bf383edc527a7ebaee412d17af4d40fdccd442f335" dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.13", + "toml_edit 0.22.14", ] [[package]] @@ -3551,15 +3731,15 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.13" +version = "0.22.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c127785850e8c20836d49732ae6abfa47616e60bf9d9f57c43c250361a9db96c" +checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38" dependencies = [ "indexmap", "serde", "serde_spanned", "toml_datetime", - "winnow 0.6.8", + "winnow 0.6.13", ] [[package]] @@ -3575,7 +3755,6 @@ dependencies = [ "tokio", "tower-layer", "tower-service", - "tracing", ] [[package]] @@ -3596,7 +3775,6 @@ version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ - "log", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -3610,7 +3788,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.65", + "syn 2.0.66", ] [[package]] @@ -3775,12 +3953,12 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "url" -version = "2.5.0" +version = "2.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" +checksum = "f7c25da092f0a868cdf09e8674cd3b7ef3a7d92a24253e663a2fb85e2496de56" dependencies = [ "form_urlencoded", - "idna", + "idna 1.0.0", "percent-encoding", ] @@ -3790,11 +3968,23 @@ version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" +[[package]] +name = "utf16_iter" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246" + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + [[package]] name = "utf8parse" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "uuid" @@ -3871,7 +4061,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.65", + "syn 2.0.66", "wasm-bindgen-shared", ] @@ -3905,7 +4095,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.65", + "syn 2.0.66", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -4187,9 +4377,9 @@ dependencies = [ [[package]] name = "winnow" -version = "0.6.8" +version = "0.6.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3c52e9c97a68071b23e836c9380edae937f17b9c4667bd021973efc689f618d" +checksum = "59b5e5f6c299a3c7890b876a2a587f3115162487e704907d9b6cd29473052ba1" dependencies = [ "memchr", ] @@ -4204,14 +4394,107 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "write16" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936" + +[[package]] +name = "writeable" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" + [[package]] name = "xxhash-rust" version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "927da81e25be1e1a2901d59b81b37dd2efd1fc9c9345a55007f09bf5a2d3ee03" +[[package]] +name = "yoke" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c5b1314b079b0930c31e3af543d8ee1757b1951ae1e1565ec704403a7240ca5" +dependencies = [ + "serde", + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28cc31741b18cb6f1d5ff12f5b7523e3d6eb0852bbbad19d73905511d9849b95" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ec111ce797d0e0784a1116d0ddcdbea84322cd79e5d5ad173daeba4f93ab55" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ea7b4a3637ea8669cedf0f1fd5c286a17f3de97b8dd5a70a6c167a1730e63a5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", + "synstructure", +] + [[package]] name = "zeroize" -version = "1.7.0" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + +[[package]] +name = "zerovec" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb2cc8827d6c0994478a15c53f374f46fbd41bea663d809b14744bc42e6b109c" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97cf56601ee5052b4417d90c8755c6683473c926039908196cf35d99f893ebe7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] diff --git a/rust/nasl-builtin-raw-ip/Cargo.toml b/rust/nasl-builtin-raw-ip/Cargo.toml index 4922f937f..2a8f26825 100644 --- a/rust/nasl-builtin-raw-ip/Cargo.toml +++ b/rust/nasl-builtin-raw-ip/Cargo.toml @@ -14,7 +14,7 @@ storage = {path = "../storage"} pcap = "1.0.0" pnet_base = "0.33.0" pnet = "0.33.0" -socket2 = {version = "0.5.2", features = ["all"]} +socket2 = {version = "0.5.7", features = ["all"]} pnet_macros = "0.33.0" pnet_macros_support = "0.33.0" diff --git a/rust/nasl-builtin-raw-ip/src/packet_forgery.rs b/rust/nasl-builtin-raw-ip/src/packet_forgery.rs index 40beaf9b3..88d8abcdb 100644 --- a/rust/nasl-builtin-raw-ip/src/packet_forgery.rs +++ b/rust/nasl-builtin-raw-ip/src/packet_forgery.rs @@ -5,7 +5,7 @@ //! Defines NASL packet forgery functions use std::{ - net::{Ipv4Addr, SocketAddr}, + net::{Ipv4Addr, Ipv6Addr, SocketAddr}, str::FromStr, }; @@ -23,8 +23,15 @@ use pnet::packet::{ self, ethernet::EthernetPacket, icmp::*, + icmpv6::{ + ndp::{ + MutableNeighborAdvertPacket, MutableNeighborSolicitPacket, MutableRouterAdvertPacket, + }, + Icmpv6Packet, Icmpv6Types, + }, ip::{IpNextHeaderProtocol, IpNextHeaderProtocols}, ipv4::{checksum, MutableIpv4Packet}, + ipv6::MutableIpv6Packet, tcp::{TcpOption, TcpOptionNumbers, TcpPacket, *}, udp::UdpPacket, Packet, PrimitiveValues, @@ -483,7 +490,7 @@ fn dump_ip_packet(register: &Register, configs: &Context) -> Result, + configs: &Context, +) -> Result<(), FunctionErrorKind> { + match tcp { + Some(pkt) => { + configs.logger().info(&"------\n"); + configs + .logger() + .info(&format!("\tth_sport : {:?}", pkt.get_source())); + configs + .logger() + .info(&format!("\tth_dport : {:?}", pkt.get_destination())); + configs + .logger() + .info(&format!("\tth_seq : {:?}", pkt.get_sequence())); + configs + .logger() + .info(&format!("\tth_ack : {:?}", pkt.get_acknowledgement())); + configs + .logger() + .info(&format!("\tth_x2 : {:?}", pkt.get_reserved())); + configs + .logger() + .info(&format!("\tth_off : {:?}", pkt.get_data_offset())); + + let flags = pkt.get_flags(); + let mut f = String::new(); + f.push_str("\tth_flags : "); + if flags & pnet::packet::tcp::TcpFlags::FIN == pnet::packet::tcp::TcpFlags::FIN { + f.push_str("TH_FIN"); + } + if flags & pnet::packet::tcp::TcpFlags::SYN == pnet::packet::tcp::TcpFlags::SYN { + if !f.is_empty() { + f.push('|') + }; + f.push_str("TH_SYN"); + } + if flags & pnet::packet::tcp::TcpFlags::RST == pnet::packet::tcp::TcpFlags::RST { + if !f.is_empty() { + f.push('|') + }; + f.push_str("TH_RST"); + } + if flags & pnet::packet::tcp::TcpFlags::PSH == pnet::packet::tcp::TcpFlags::PSH { + if !f.is_empty() { + f.push('|') + }; + f.push_str("TH_PSH"); + } + if flags & pnet::packet::tcp::TcpFlags::ACK == pnet::packet::tcp::TcpFlags::ACK { + if !f.is_empty() { + f.push('|') + }; + f.push_str("TH_ACK"); + } + if flags & pnet::packet::tcp::TcpFlags::URG == pnet::packet::tcp::TcpFlags::URG { + if !f.is_empty() { + f.push('|') + }; + f.push_str("TH_URG"); + } + if f.is_empty() { + f.push_str(" 0"); + } else { + f.push_str(&format!(" {:?}", flags)); + }; + configs.logger().info(&f); + configs + .logger() + .info(&format!("\tth_win : {:?}", pkt.get_window())); + configs + .logger() + .info(&format!("\tth_sum : {:?}", pkt.get_checksum())); + configs + .logger() + .info(&format!("\tth_urp : {:?}", pkt.get_urgent_ptr())); + println!("\tTCP Options:"); + for o in pkt.get_options_iter() { + let n = o.get_number(); + let p = o.payload(); + if n == TcpOptionNumbers::MSS { + configs + .logger() + .info(&format!("\t\tTCPOPT_MAXSEG: {:?}", p)); + } + if n == TcpOptionNumbers::WSCALE { + configs + .logger() + .info(&format!("\t\tTCPOPT_WINDOW: {:?}", p)); + } + + if n == TcpOptionNumbers::SACK_PERMITTED { + configs + .logger() + .info(&format!("\t\tTCPOPT_SACK_PERMITTED: {:?}", p)); + } + if n == TcpOptionNumbers::TIMESTAMPS { + configs + .logger() + .info(&format!("\t\tTCPOPT_TIMESTAMP TSval: {:?}", p)); + } + } + Ok(()) + } + None => Err(FunctionErrorKind::WrongArgument( + "Invalid TPC packet".to_string(), + )), + } +} + /// Receive a list of IPv4 datagrams and print their TCP part in a readable format in the screen. fn dump_tcp_packet(register: &Register, configs: &Context) -> Result { let positional = register.positional(); @@ -1185,125 +1303,9 @@ fn dump_tcp_packet(register: &Register, configs: &Context) -> Result { - configs.logger().info(&"------\n"); - configs - .logger() - .info(&format!("\tth_sport : {:?}", pkt.get_source())); - configs - .logger() - .info(&format!("\tth_dport : {:?}", pkt.get_destination())); - configs - .logger() - .info(&format!("\tth_seq : {:?}", pkt.get_sequence())); - configs - .logger() - .info(&format!("\tth_ack : {:?}", pkt.get_acknowledgement())); - configs - .logger() - .info(&format!("\tth_x2 : {:?}", pkt.get_reserved())); - configs - .logger() - .info(&format!("\tth_off : {:?}", pkt.get_data_offset())); - - let flags = pkt.get_flags(); - let mut f = String::new(); - f.push_str("\tth_flags : "); - if flags & pnet::packet::tcp::TcpFlags::FIN - == pnet::packet::tcp::TcpFlags::FIN - { - f.push_str("TH_FIN"); - } - if flags & pnet::packet::tcp::TcpFlags::SYN - == pnet::packet::tcp::TcpFlags::SYN - { - if !f.is_empty() { - f.push('|') - }; - f.push_str("TH_SYN"); - } - if flags & pnet::packet::tcp::TcpFlags::RST - == pnet::packet::tcp::TcpFlags::RST - { - if !f.is_empty() { - f.push('|') - }; - f.push_str("TH_RST"); - } - if flags & pnet::packet::tcp::TcpFlags::PSH - == pnet::packet::tcp::TcpFlags::PSH - { - if !f.is_empty() { - f.push('|') - }; - f.push_str("TH_PSH"); - } - if flags & pnet::packet::tcp::TcpFlags::ACK - == pnet::packet::tcp::TcpFlags::ACK - { - if !f.is_empty() { - f.push('|') - }; - f.push_str("TH_ACK"); - } - if flags & pnet::packet::tcp::TcpFlags::URG - == pnet::packet::tcp::TcpFlags::URG - { - if !f.is_empty() { - f.push('|') - }; - f.push_str("TH_URG"); - } - if f.is_empty() { - f.push_str(" 0"); - } else { - f.push_str(&format!(" {:?}", flags)); - }; - configs.logger().info(&f); - configs - .logger() - .info(&format!("\tth_win : {:?}", pkt.get_window())); - configs - .logger() - .info(&format!("\tth_sum : {:?}", pkt.get_checksum())); - configs - .logger() - .info(&format!("\tth_urp : {:?}", pkt.get_urgent_ptr())); - println!("\tTCP Options:"); - for o in pkt.get_options_iter() { - let n = o.get_number(); - let p = o.payload(); - if n == TcpOptionNumbers::MSS { - configs - .logger() - .info(&format!("\t\tTCPOPT_MAXSEG: {:?}", p)); - } - if n == TcpOptionNumbers::WSCALE { - configs - .logger() - .info(&format!("\t\tTCPOPT_WINDOW: {:?}", p)); - } - - if n == TcpOptionNumbers::SACK_PERMITTED { - configs - .logger() - .info(&format!("\t\tTCPOPT_SACK_PERMITTED: {:?}", p)); - } - if n == TcpOptionNumbers::TIMESTAMPS { - configs - .logger() - .info(&format!("\t\tTCPOPT_TIMESTAMP TSval: {:?}", p)); - } - } - display_packet(configs.logger(), data); - } - None => { - return Err(FunctionErrorKind::WrongArgument( - "Invalid TPC packet".to_string(), - )); - } - } + let pkt = packet::tcp::TcpPacket::new(ip.payload()); + print_tcp_packet(&pkt, configs)?; + display_packet(configs.logger(), data); } _ => { return Err(FunctionErrorKind::WrongArgument( @@ -1522,6 +1524,37 @@ fn set_udp_elements( Ok(NaslValue::Data(pkt.packet().to_vec())) } +fn dump_udp( + datagram: &Option, + data: &[u8], + configs: &Context, +) -> Result { + match datagram { + Some(pkt) => { + configs.logger().info(&"------\n"); + configs + .logger() + .info(&format!("\tuh_sport : {:?}", pkt.get_source())); + configs + .logger() + .info(&format!("\tuh_dport : {:?}", pkt.get_destination())); + configs + .logger() + .info(&format!("\tuh_len : {:?}", pkt.get_length())); + configs + .logger() + .info(&format!("\tuh_sum : {:?}", pkt.get_checksum())); + display_packet(configs.logger(), data); + } + None => { + return Err(FunctionErrorKind::WrongArgument( + "Invalid UDP packet".to_string(), + )); + } + } + Ok(NaslValue::Null) +} + /// Receive a list of IPv4 datagrams and print their UDP part in a readable format in the screen. fn dump_udp_packet(register: &Register, configs: &Context) -> Result { let positional = register.positional(); @@ -1543,29 +1576,8 @@ fn dump_udp_packet(register: &Register, configs: &Context) -> Result { - configs.logger().info(&"------\n"); - configs - .logger() - .info(&format!("\tuh_sport : {:?}", pkt.get_source())); - configs - .logger() - .info(&format!("\tuh_dport : {:?}", pkt.get_destination())); - configs - .logger() - .info(&format!("\tuh_len : {:?}", pkt.get_length())); - configs - .logger() - .info(&format!("\tuh_sum : {:?}", pkt.get_checksum())); - display_packet(configs.logger(), data); - } - None => { - return Err(FunctionErrorKind::WrongArgument( - "Invalid UDP packet".to_string(), - )); - } - } + let datagram = packet::udp::UdpPacket::new(ip.payload()); + dump_udp(&datagram, data, configs)?; } _ => { return Err(FunctionErrorKind::WrongArgument( @@ -1578,6 +1590,23 @@ fn dump_udp_packet(register: &Register, configs: &Context) -> Result Result { + match register.named("element") { + Some(ContextType::Value(NaslValue::String(el))) => match el.as_str() { + "uh_sport" => Ok(NaslValue::Number(udp.get_source() as i64)), + "uh_dport" => Ok(NaslValue::Number(udp.get_destination() as i64)), + "uh_len" => Ok(NaslValue::Number(udp.get_length() as i64)), + "uh_sum" => Ok(NaslValue::Number(udp.get_checksum() as i64)), + "data" => Ok(NaslValue::Data(udp.payload().to_vec())), + _ => Err(FunctionErrorKind::WrongArgument("element".to_string())), + }, + _ => Err(FunctionErrorKind::WrongArgument("element".to_string())), + } +} + /// Get an UDP element from a IP datagram. It returns a data block or an integer, according to the type of the element. Its arguments are: /// - udp: is the IP datagram. /// - element: is the name of the field to get @@ -1602,21 +1631,12 @@ fn get_udp_element( let ip = packet::ipv4::Ipv4Packet::new(&buf).ok_or_else(|| { FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) })?; + let udp = packet::udp::UdpPacket::new(ip.payload()).ok_or_else(|| { FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) })?; - match register.named("element") { - Some(ContextType::Value(NaslValue::String(el))) => match el.as_str() { - "uh_sport" => Ok(NaslValue::Number(udp.get_source() as i64)), - "uh_dport" => Ok(NaslValue::Number(udp.get_destination() as i64)), - "uh_len" => Ok(NaslValue::Number(udp.get_length() as i64)), - "uh_sum" => Ok(NaslValue::Number(udp.get_checksum() as i64)), - "data" => Ok(NaslValue::Data(udp.payload().to_vec())), - _ => Err(FunctionErrorKind::WrongArgument("element".to_string())), - }, - _ => Err(FunctionErrorKind::WrongArgument("element".to_string())), - } + get_udp_element_from_datagram(udp, register) } /// Fills an IP datagram with ICMP data. Note that the ip_p field is not updated. It returns the modified IP datagram. Its arguments are: @@ -1884,6 +1904,7 @@ pub mod igmp { } /// Represents a generic IGMP packet. + #[allow(dead_code)] #[packet] pub struct Igmp { #[construct_with(u8)] @@ -2033,6 +2054,21 @@ fn new_raw_socket() -> Result { } } + +fn new_raw_ipv6_socket() -> Result { + match Socket::new( + Domain::IPV6, // 10 + socket2::Type::RAW, // 3 + Some(Protocol::from(IPPROTO_RAW)), // 255 + ) { + Ok(s) => Ok(s), + Err(e) => Err(FunctionErrorKind::Dirty(format!( + "new_raw_ipv6_socket: Not possible to create a raw socket: {}", + e + ))), + } +} + /// This function tries to open a TCP connection and sees if anything comes back (SYN/ACK or RST). /// /// Its argument is: @@ -2367,6 +2403,1998 @@ fn nasl_send_capture( } } +// IP v6 functions + +/// Forge an IP datagram inside the block of data. It takes following arguments: +/// +/// The arguments are: +/// - data: Data payload +/// - ip6_v: Version. 6 by default. +/// - ip6_tc: Traffic class. 0 by default. +/// - ip6_fl: Flow label. 0 by default. +/// - ip6_p: IP protocol. 0 by default. +/// - ip6_hlim: Hop limit. Max. 255. 64 by default. +/// - ip6_src: Source address. +/// - ip6_dst: Destination address. +/// +/// Return an IPv6 datagram or Null on error. +fn forge_ip_v6_packet( + register: &Register, + configs: &Context, +) -> Result { + let dst_addr = get_host_ip(configs)?; + + if dst_addr.is_ipv4() { + return Err(FunctionErrorKind::WrongArgument( + "forge_ip_v6_packet: No valid dst_addr could be determined via call to get_host_ip()" + .to_string(), + )); + } + + let data = match register.named("data") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + Some(ContextType::Value(NaslValue::String(d))) => d.as_bytes().to_vec(), + Some(ContextType::Value(NaslValue::Number(d))) => d.to_be_bytes().to_vec(), + _ => Vec::::new(), + }; + + let total_length = 40 + data.len(); + let mut buf = vec![0; total_length]; + let mut pkt = packet::ipv6::MutableIpv6Packet::new(&mut buf).ok_or_else(|| { + FunctionErrorKind::Diagnostic( + "No possible to create a packet from buffer".to_string(), + None, + ) + })?; + + pkt.set_payload_length(data.len() as u16); + + if !data.is_empty() { + pkt.set_payload(&data); + } + + let ip_v = match register.named("ip6_v") { + Some(ContextType::Value(NaslValue::Number(x))) => *x as u8, + _ => 6_u8, + }; + pkt.set_version(ip_v); + + let ip_tc = match register.named("ip6_tc") { + Some(ContextType::Value(NaslValue::Number(x))) => *x as u8, + _ => 0_u8, + }; + pkt.set_traffic_class(ip_tc); + + let ip_flow = match register.named("ip6_fl") { + Some(ContextType::Value(NaslValue::Number(x))) => *x as u32, + _ => 0_u32, + }; + pkt.set_flow_label(ip_flow); + + let ip_p = match register.named("ip6_p") { + Some(ContextType::Value(NaslValue::Number(x))) => *x as u8, + _ => 0_u8, + }; + pkt.set_next_header(IpNextHeaderProtocol::new(ip_p)); + + let ip_hlim = match register.named("ip6_hlim") { + Some(ContextType::Value(NaslValue::Number(x))) => *x as u8, + _ => 64_u8, + }; + pkt.set_hop_limit(ip_hlim); + + match register.named("ip6_src") { + Some(ContextType::Value(NaslValue::String(x))) => { + match x.parse::() { + Ok(ip) => { + pkt.set_source(ip); + } + Err(e) => { + return Err(FunctionErrorKind::WrongArgument(format!( + "Invalid ip_src: {}", + e + ))); + } + }; + x.to_string() + } + _ => String::new(), + }; + + match register.named("ip6_dst") { + Some(ContextType::Value(NaslValue::String(x))) => { + match x.parse::() { + Ok(ip) => { + pkt.set_destination(ip); + } + Err(e) => { + return Err(FunctionErrorKind::WrongArgument(format!( + "Invalid ip_dst: {}", + e + ))); + } + }; + x.to_string() + } + _ => { + match dst_addr.to_string().parse::() { + Ok(ip) => { + pkt.set_destination(ip); + } + Err(e) => { + return Err(FunctionErrorKind::WrongArgument(format!( + "Invalid ip: {}", + e + ))); + } + }; + dst_addr.to_string() + } + }; + + // There is no checksum for ipv6. Only upper layer + // calculates a checksum using pseudoheader + + Ok(NaslValue::Data(buf)) +} + +/// Get an IP element from a IP v6 datagram. It returns a data block or an integer, according to the type of the element. Its arguments are: +/// - ip6: is the IP v6 datagram. +/// - element: is the name of the field to get +/// +/// Valid IP elements to get are: +/// - ip6_v +/// - ip6_tc +/// - ip6_fl +/// - ip6_plen +/// - ip6_nxt +/// - ip6_hlim +/// - ip6_src +/// - ip6_dst +fn get_ip_v6_element( + register: &Register, + _configs: &Context, +) -> Result { + let buf = match register.named("ip6") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => { + return Err(FunctionErrorKind::from("ip6")); + } + }; + + let pkt = packet::ipv6::Ipv6Packet::new(&buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + match register.named("element") { + Some(ContextType::Value(NaslValue::String(e))) => match e.as_str() { + "ip6_v" => Ok(NaslValue::Number(pkt.get_version() as i64)), + "ip6_tc" => Ok(NaslValue::Number(pkt.get_traffic_class() as i64)), + "ip6_fl" => Ok(NaslValue::Number(pkt.get_flow_label() as i64)), + "ip6_plen" => Ok(NaslValue::Number(pkt.get_payload_length() as i64)), + "ip6_nxt" => Ok(NaslValue::Number(i64::from( + pkt.get_next_header().to_primitive_values().0, + ))), + "ip6_src" => Ok(NaslValue::String(pkt.get_source().to_string())), + "ip6_dst" => Ok(NaslValue::String(pkt.get_destination().to_string())), + _ => Err(FunctionErrorKind::WrongArgument( + "Invalid element".to_string(), + )), + }, + // Missingarguments + _ => Err(FunctionErrorKind::from("element")), + } +} + +/// Set an IP element from a IP v6 datagram. It returns a data block or an integer, according to the type of the element. Its arguments are: +/// - ip6: is the IP v6 datagram. +/// - element: is the name of the field to get +/// +/// Valid IP elements to get are: +/// - ip6_plen +/// - ip6_nxt +/// - ip6_hlim +/// - ip6_src +fn set_ip_v6_elements( + register: &Register, + _configs: &Context, +) -> Result { + let mut buf = match register.named("ip6") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => { + // Missingarguments + return Err(FunctionErrorKind::from("ip")); + } + }; + + let mut pkt = packet::ipv6::MutableIpv6Packet::new(&mut buf).ok_or_else(|| { + FunctionErrorKind::Diagnostic( + "No possible to create a packet from buffer".to_string(), + None, + ) + })?; + + let ip6_plen = match register.named("ip6_plen") { + Some(ContextType::Value(NaslValue::Number(x))) => *x as u16, + _ => pkt.get_payload_length(), + }; + pkt.set_payload_length(ip6_plen); + + let ip6_nxt = match register.named("ip6_nxt") { + Some(ContextType::Value(NaslValue::Number(x))) => *x as u8, + _ => pkt.get_next_header().0, + }; + pkt.set_next_header(IpNextHeaderProtocol::new(ip6_nxt)); + + if let Some(ContextType::Value(NaslValue::String(x))) = register.named("ip6_src") { + match x.parse::() { + Ok(ip) => { + pkt.set_source(ip); + } + Err(e) => { + return Err(FunctionErrorKind::WrongArgument(format!( + "Invalid ip_src: {}", + e + ))); + } + }; + }; + + Ok(NaslValue::Data(buf)) +} + +/// Adds an IPv6 option to the datagram. +/// +/// - ip6 IPv6 packet. +/// - data Data payload. +/// - code Code of option. +/// - length Length of value. +/// - value Value of the option. +/// +/// Return the modified datagram. +/// +fn insert_ip_v6_options( + register: &Register, + _configs: &Context, +) -> Result { + let buf = match register.named("ip6") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => { + return Err(FunctionErrorKind::from("ip6")); + } + }; + + let code = match register.named("code") { + Some(ContextType::Value(NaslValue::Number(x))) => *x, + _ => { + return Err(FunctionErrorKind::from("code")); + } + }; + let length = match register.named("length") { + Some(ContextType::Value(NaslValue::Number(x))) => *x as usize, + _ => { + return Err(FunctionErrorKind::from("length")); + } + }; + let value = match register.named("value") { + Some(ContextType::Value(NaslValue::String(x))) => x.as_bytes(), + Some(ContextType::Value(NaslValue::Data(x))) => x, + _ => { + return Err(FunctionErrorKind::from("value")); + } + }; + + // The pnet library does not have the implementation for create/modify TcpOptions. + // The TcpOption struct members are even private. Therefore, creating it manually. + // This is not possible: + //let opt = Ipv4Option{ + // copied: 1, + // class: 0, + // number: Ipv4OptionNumber(3), + // length: opt_len.to_vec(), + // data: opt_buf.to_vec(), + //}; + + // Get the first byte from an i64 + let codebyte = code.to_le_bytes()[0]; + // Length is 2 bytes. Get the 2 bytes from the i64 + let opt_len = &length.to_le_bytes()[..2]; + let mut opt_buf = vec![0u8; length]; + + //opt_buf[..1].copy_from_slice(&vec![codebyte][..]); + safe_copy_from_slice(&mut opt_buf[..], 0, 1, &[codebyte], 0, 1)?; + //opt_buf[1..3].copy_from_slice(opt_len); + safe_copy_from_slice(&mut opt_buf[..], 1, 3, opt_len, 0, opt_len.len())?; + //opt_buf[3..].copy_from_slice(value); + safe_copy_from_slice(&mut opt_buf[..], 3, length, value, 0, value.len())?; + + let hl_valid_data = 40 + opt_buf.len(); + let padding = 32 - hl_valid_data % 32; + let hl = hl_valid_data + padding; + let mut new_buf = vec![0u8; hl]; + //new_buf[..40].copy_from_slice(&buf[..40]); + safe_copy_from_slice(&mut new_buf[..], 0, 40, &buf, 0, 40)?; + //new_buf[40..hl_valid_data].copy_from_slice(&opt_buf[..opt_buf.len()]); + safe_copy_from_slice( + &mut new_buf[..], + 40, + hl_valid_data, + &opt_buf, + 0, + opt_buf.len(), + )?; + + let mut new_pkt = MutableIpv6Packet::new(&mut new_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + new_pkt.set_payload_length((hl / 4) as u16); + Ok(NaslValue::Data(new_pkt.packet().to_vec())) +} + +/// Receive a list of IP v6 packets and print them in a readable format in the screen. +fn dump_ip_v6_packet( + register: &Register, + configs: &Context, +) -> Result { + let positional = register.positional(); + if positional.is_empty() { + return Err(FunctionErrorKind::MissingPositionalArguments { + expected: 1, + got: 0, + }); + } + + for ip in positional.iter() { + match ip { + NaslValue::Data(data) => { + let pkt = packet::ipv6::Ipv6Packet::new(data).ok_or_else(|| { + FunctionErrorKind::Dirty( + "No possible to create a packet from buffer".to_string(), + ) + })?; + configs.logger().info(&"------\n"); + configs + .logger() + .info(&format!("\tip6_v : {:?}", pkt.get_version())); + configs + .logger() + .info(&format!("\tip6_tc : {:?}", pkt.get_traffic_class())); + configs + .logger() + .info(&format!("\tip6_fl : {:?}", pkt.get_flow_label())); + configs + .logger() + .info(&format!("\tip6_plen : {:?}", pkt.get_payload_length())); + configs + .logger() + .info(&format!("\tip6_hlim : {:?}", pkt.get_hop_limit())); + + match pkt.get_next_header() { + IpNextHeaderProtocols::Tcp => configs.logger().info(&format!( + "\tip6_nxt : IPPROTO_TCP ({:?})", + pkt.get_next_header().to_primitive_values().0 + )), + IpNextHeaderProtocols::Udp => configs.logger().info(&format!( + "\tip6_nxt : IPPROTO_UDP ({:?})", + pkt.get_next_header().to_primitive_values().0 + )), + IpNextHeaderProtocols::Icmpv6 => configs.logger().info(&format!( + "\tip6_nxt : IPPROTO_ICMP ({:?})", + pkt.get_next_header().to_primitive_values().0 + )), + _ => configs.logger().info(&format!( + "\tip6_nxt : {:?}", + pkt.get_next_header().to_primitive_values().0 + )), + }; + + configs + .logger() + .info(&format!("\tip6_src : {:?}", pkt.get_source().to_string())); + configs.logger().info(&format!( + "\tip6_dst : {:?}", + pkt.get_destination().to_string() + )); + display_packet(configs.logger(), data); + } + _ => { + return Err(FunctionErrorKind::WrongArgument( + "Invalid ip packet".to_string(), + )); + } + } + } + + Ok(NaslValue::Null) +} + +// TCP over IPv6 + +/// Fills an IP datagram with TCP data. Note that the ip_p field is not updated. It returns the modified IP datagram. Its arguments are: +/// +/// - ip6: is the IP datagram to be filled. +/// - data: is the TCP data payload. +/// - th_sport: is the source port. NASL will convert it into network order if necessary. 0 by default. +/// - th_dport: is the destination port. NASL will convert it into network order if necessary. 0 by default. +/// - th_seq: is the TCP sequence number. NASL will convert it into network order if necessary. Random by default. +/// - th_ack: is the acknowledge number. NASL will convert it into network order if necessary. 0 by default. +/// - th_x2: is a reserved field and should probably be left unchanged. 0 by default. +/// - th_off: is the size of the TCP header in 32 bits words. By default, 5. +/// - th_flags: are the TCP flags. 0 by default. +/// - th_win: is the TCP window size. NASL will convert it into network order if necessary. 0 by default. +/// - th_sum: is the TCP checksum. By default, the right value is computed. +/// - th_urp: is the urgent pointer. 0 by default. +/// - update_ip_len: is a flag (TRUE by default). If set, NASL will recompute the size field of the IP datagram. +/// +/// The modified IP datagram or NULL on error. +fn forge_tcp_v6_packet( + register: &Register, + _configs: &Context, +) -> Result { + let mut ip_buf = match register.named("ip6") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => { + // Missingarguments + return Err(FunctionErrorKind::from("ip6")); + } + }; + let original_ip_len = ip_buf.len(); + + let data = match register.named("data") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + Some(ContextType::Value(NaslValue::String(d))) => d.as_bytes().to_vec(), + Some(ContextType::Value(NaslValue::Number(d))) => d.to_be_bytes().to_vec(), + _ => Vec::::new(), + }; + + //tcp length + data length + let total_length = 20 + data.len(); + let mut buf = vec![0; total_length]; + let mut tcp_seg = packet::tcp::MutableTcpPacket::new(&mut buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + if !data.is_empty() { + tcp_seg.set_payload(&data); + } + + match register.named("th_sport") { + Some(ContextType::Value(NaslValue::Number(x))) => tcp_seg.set_source(*x as u16), + _ => tcp_seg.set_source(0_u16), + }; + match register.named("th_dport") { + Some(ContextType::Value(NaslValue::Number(x))) => tcp_seg.set_destination(*x as u16), + _ => tcp_seg.set_destination(0_u16), + }; + + match register.named("th_seq") { + Some(ContextType::Value(NaslValue::Number(x))) => tcp_seg.set_sequence(*x as u32), + _ => tcp_seg.set_sequence(random_impl()? as u32), + }; + match register.named("th_ack") { + Some(ContextType::Value(NaslValue::Number(x))) => tcp_seg.set_acknowledgement(*x as u32), + _ => tcp_seg.set_acknowledgement(0_u32), + }; + match register.named("th_x2") { + Some(ContextType::Value(NaslValue::Number(x))) => tcp_seg.set_reserved(*x as u8), + _ => tcp_seg.set_reserved(0_u8), + }; + match register.named("th_off") { + Some(ContextType::Value(NaslValue::Number(x))) => tcp_seg.set_data_offset(*x as u8), + _ => tcp_seg.set_data_offset(5_u8), + }; + match register.named("th_flags") { + Some(ContextType::Value(NaslValue::Number(x))) => tcp_seg.set_flags(*x as u16), + _ => tcp_seg.set_flags(0_u16), + }; + match register.named("th_win") { + Some(ContextType::Value(NaslValue::Number(x))) => tcp_seg.set_window(*x as u16), + _ => tcp_seg.set_window(0_u16), + }; + match register.named("th_urp") { + Some(ContextType::Value(NaslValue::Number(x))) => tcp_seg.set_urgent_ptr(*x as u16), + _ => tcp_seg.set_urgent_ptr(0_u16), + }; + + let chksum = match register.named("th_sum") { + Some(ContextType::Value(NaslValue::Number(x))) if *x != 0 => (*x as u16).to_be(), + _ => { + let pkt = packet::ipv6::Ipv6Packet::new(&ip_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + let tcp_aux = TcpPacket::new(tcp_seg.packet()).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + pnet::packet::tcp::ipv6_checksum(&tcp_aux, &pkt.get_source(), &pkt.get_destination()) + } + }; + + let mut tcp_seg = packet::tcp::MutableTcpPacket::new(&mut buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + tcp_seg.set_checksum(chksum); + + ip_buf.append(&mut buf); + let l = ip_buf.len(); + let mut pkt = packet::ipv6::MutableIpv6Packet::new(&mut ip_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + pkt.set_payload_length(l as u16); + match register.named("update_ip_len") { + Some(ContextType::Value(NaslValue::Boolean(l))) if !(*l) => { + pkt.set_payload_length(original_ip_len as u16); + } + _ => (), + }; + + Ok(NaslValue::Data(ip_buf)) +} + +/// Get an TCP element from a IP datagram. It returns a data block or an integer, according to the type of the element. Its arguments are: +/// - tcp: is the IP datagram. +/// - element: is the name of the field to get +/// +/// Valid IP elements to get are: +/// - th_sport +/// - th_dsport +/// - th_seq +/// - th_ack +/// - th_x2 +/// - th_off +/// - th_flags +/// - th_win +/// - th_sum +/// - th_urp +/// - data +/// +/// Returns an TCP element from a IP datagram. +fn get_tcp_v6_element( + register: &Register, + _configs: &Context, +) -> Result { + let buf = match register.named("tcp") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => { + return Err(FunctionErrorKind::from("tcp")); + } + }; + + let ip = packet::ipv6::Ipv6Packet::new(&buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + let tcp = packet::tcp::TcpPacket::new(ip.payload()).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + match register.named("element") { + Some(ContextType::Value(NaslValue::String(el))) => match el.as_str() { + "th_sport" => Ok(NaslValue::Number(tcp.get_source() as i64)), + "th_dport" => Ok(NaslValue::Number(tcp.get_destination() as i64)), + "th_seq" => Ok(NaslValue::Number(tcp.get_sequence() as i64)), + "th_ack" => Ok(NaslValue::Number(tcp.get_acknowledgement() as i64)), + "th_x2" => Ok(NaslValue::Number(tcp.get_reserved() as i64)), + "th_off" => Ok(NaslValue::Number(tcp.get_data_offset() as i64)), + "th_flags" => Ok(NaslValue::Number(tcp.get_flags() as i64)), + "th_win" => Ok(NaslValue::Number(tcp.get_window() as i64)), + "th_sum" => Ok(NaslValue::Number(tcp.get_checksum() as i64)), + "th_urp" => Ok(NaslValue::Number(tcp.get_urgent_ptr() as i64)), + "th_data" => Ok(NaslValue::Data(tcp.payload().to_vec())), + _ => Err(FunctionErrorKind::WrongArgument("element".to_string())), + }, + _ => Err(FunctionErrorKind::from("element")), + } +} + +/// Get a TCP option from a IPv6 datagram. Its arguments are: +/// - tcp: is the IP datagram. +/// - option: is the name of the field to get +/// +/// Valid IP options to get are: +/// - 2: TCPOPT_MAXSEG, values between 536 and 65535 +/// - 3: TCPOPT_WINDOW, with values between 0 and 14 +/// - 4: TCPOPT_SACK_PERMITTED, no value required. +/// - 8: TCPOPT_TIMESTAMP, 8 bytes value for timestamp and echo timestamp, 4 bytes each one. +/// +/// The returned option depends on the given *option* parameter. It is either an int for option 2, 3 and 4 or an array containing the two values for option 8. +fn get_tcp_v6_option( + register: &Register, + _configs: &Context, +) -> Result { + let buf = match register.named("tcp") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => { + return Err(FunctionErrorKind::from("tcp")); + } + }; + + let ip = packet::ipv6::Ipv6Packet::new(&buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + let tcp = packet::tcp::TcpPacket::new(ip.payload()).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + let mut max_seg: i64 = 0; + let mut window: i64 = 0; + let mut sack_permitted: i64 = 0; + let mut timestamps: Vec = vec![NaslValue::Number(0), NaslValue::Number(0)]; + for opt in tcp.get_options_iter() { + if opt.get_number() == TcpOptionNumbers::MSS { + let mut val = [0u8; 2]; + //val[..2].copy_from_slice(&opt.payload()[..2]); + safe_copy_from_slice(&mut val, 0, 2, opt.payload(), 0, 2)?; + max_seg = i16::from_be_bytes(val) as i64; + } + if opt.get_number() == TcpOptionNumbers::WSCALE { + let mut val = [0u8; 1]; + //val[..1].copy_from_slice(&opt.payload()[..1]); + safe_copy_from_slice(&mut val, 0, 1, opt.payload(), 0, 1)?; + window = val[0] as i64; + } + if opt.get_number() == TcpOptionNumbers::SACK_PERMITTED { + sack_permitted = 1; + } + if opt.get_number() == TcpOptionNumbers::TIMESTAMPS { + let mut t1 = [0u8; 4]; + let mut t2 = [0u8; 4]; + //t1[..4].copy_from_slice(&opt.payload()[..4]); + safe_copy_from_slice(&mut t1, 0, 4, opt.payload(), 0, 4)?; + //t2[..4].copy_from_slice(&opt.payload()[4..]); + safe_copy_from_slice(&mut t2, 0, 4, opt.payload(), 4, opt.payload().len())?; + let t1_val = i32::from_be_bytes(t1) as i64; + let t2_val = i32::from_be_bytes(t2) as i64; + + timestamps = vec![NaslValue::Number(t1_val), NaslValue::Number(t2_val)]; + } + } + + match register.named("option") { + Some(ContextType::Value(NaslValue::Number(el))) => match el { + 2 => Ok(NaslValue::Number(max_seg)), + 3 => Ok(NaslValue::Number(window)), + 4 => Ok(NaslValue::Number(sack_permitted)), + 8 => Ok(NaslValue::Array(timestamps)), + _ => Err(FunctionErrorKind::WrongArgument( + "Invalid option".to_string(), + )), + }, + _ => Err(FunctionErrorKind::from("option")), + } +} + +/// This function modifies the TCP fields of an IP datagram. Its arguments are: +/// +/// - data: is the TCP data payload. +/// - tcp: is the IP datagram to be filled. +/// - th_ack: is the acknowledge number. NASL will convert it into network order if necessary. 0 by default. +/// - th_dport: is the destination port. NASL will convert it into network order if necessary. 0 by default. +/// - th_flags: are the TCP flags. 0 by default. +/// - th_off: is the size of the TCP header in 32 bits words. By default, 5. +/// - th_seq: is the TCP sequence number. NASL will convert it into network order if necessary. Random by default. +/// - th_sport: is the source port. NASL will convert it into network order if necessary. 0 by default. +/// - th_sum: is the TCP checksum. By default, the right value is computed. +/// - th_urp: is the urgent pointer. 0 by default. +/// - th_win: is the TCP window size. NASL will convert it into network order if necessary. 0 by default. +/// - th_x2: is a reserved field and should probably be left unchanged. 0 by default. +/// - update_ip_len: is a flag (TRUE by default). If set, NASL will recompute the size field of the IP datagram. +fn set_tcp_v6_elements( + register: &Register, + _configs: &Context, +) -> Result { + let buf = match register.named("tcp") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => { + return Err(FunctionErrorKind::from("tcp")); + } + }; + + let ip = packet::ipv6::Ipv6Packet::new(&buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + let iph_len = ip.get_payload_length() as usize * 4; // the header length is given in 32-bits words + + let data = match register.named("data") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + Some(ContextType::Value(NaslValue::String(d))) => d.as_bytes().to_vec(), + Some(ContextType::Value(NaslValue::Number(d))) => d.to_be_bytes().to_vec(), + _ => Vec::::new(), + }; + + let ori_tcp_buf = <&[u8]>::clone(&ip.payload()).to_owned(); + let mut ori_tcp: packet::tcp::MutableTcpPacket; + + let mut new_buf: Vec; + let tcp_total_length: usize; + if !data.is_empty() { + //Prepare a new buffer with new size, copy the tcp header and set the new data + tcp_total_length = 20 + data.len(); + new_buf = vec![0u8; tcp_total_length]; + //new_buf[..20].copy_from_slice(&ori_tcp_buf[..20]); + safe_copy_from_slice(&mut new_buf[..], 0, 20, &ori_tcp_buf, 0, 20)?; + ori_tcp = packet::tcp::MutableTcpPacket::new(&mut new_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + ori_tcp.set_payload(&data); + } else { + // Copy the original tcp buffer into the new buffer + tcp_total_length = ip.payload().len(); + new_buf = vec![0u8; tcp_total_length]; + //new_buf[..].copy_from_slice(&ori_tcp_buf); + safe_copy_from_slice( + &mut new_buf[..], + 0, + tcp_total_length, + &ori_tcp_buf, + 0, + ori_tcp_buf.len(), + )?; + ori_tcp = packet::tcp::MutableTcpPacket::new(&mut new_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + } + + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("th_sport") { + ori_tcp.set_source(*x as u16); + }; + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("th_dport") { + ori_tcp.set_destination(*x as u16); + }; + + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("th_seq") { + ori_tcp.set_sequence(*x as u32); + }; + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("th_ack") { + ori_tcp.set_acknowledgement(*x as u32); + }; + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("th_x2") { + ori_tcp.set_reserved(*x as u8); + }; + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("th_off") { + ori_tcp.set_data_offset(*x as u8); + }; + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("th_flags") { + ori_tcp.set_flags(*x as u16); + }; + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("th_win") { + ori_tcp.set_window(*x as u16); + }; + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("th_urp") { + ori_tcp.set_urgent_ptr(*x as u16); + }; + + // Set the checksum for the tcp segment + let chksum = match register.named("th_sum") { + Some(ContextType::Value(NaslValue::Number(x))) if *x != 0 => (*x as u16).to_be(), + _ => { + let pkt = packet::ipv4::Ipv4Packet::new(&buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + let tcp_aux = TcpPacket::new(ori_tcp.packet()).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + pnet::packet::tcp::ipv4_checksum(&tcp_aux, &pkt.get_source(), &pkt.get_destination()) + } + }; + ori_tcp.set_checksum(chksum); + + // Create a owned copy of the final tcp segment, which will be appended as payload to the IP packet. + let mut fin_tcp_buf: Vec = vec![0u8; tcp_total_length]; + let buf_aux = <&[u8]>::clone(&ori_tcp.packet()).to_owned(); + fin_tcp_buf.clone_from_slice(&buf_aux); + + // Create a new IP packet with the original IP header, and the new TCP payload + let mut new_ip_buf = vec![0u8; iph_len]; + //new_ip_buf[..].copy_from_slice(&buf[..iph_len]); + safe_copy_from_slice(&mut new_ip_buf[..], 0, iph_len, &buf, 0, iph_len)?; + + new_ip_buf.append(&mut fin_tcp_buf.to_vec()); + + let l = new_ip_buf.len(); + let mut pkt = packet::ipv6::MutableIpv6Packet::new(&mut new_ip_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + // pnet will panic if the total length set in the ip datagram field does not much with the total length. + // Therefore, the total length is set to the right one before setting the payload. + // By default it was always updated, but if desired, the original length is set again after setting the payload. + let original_ip_len = pkt.get_payload_length(); + pkt.set_payload_length(l as u16); + pkt.set_payload(&fin_tcp_buf); + match register.named("update_ip_len") { + Some(ContextType::Value(NaslValue::Boolean(l))) if !(*l) => { + pkt.set_payload_length(original_ip_len); + } + _ => (), + }; + + Ok(NaslValue::Data(pkt.packet().to_vec())) +} + +/// This function adds TCP options to a IP datagram. The options are given as key value(s) pair with the positional argument list. The first positional argument is the identifier of the option, the next positional argument is the value for the option. For the option TCPOPT_TIMESTAMP (8) two values must be given. +/// +/// Available options are: +/// +/// - 2: TCPOPT_MAXSEG, values between 536 and 65535 +/// - 3: TCPOPT_WINDOW, with values between 0 and 14 +/// - 4: TCPOPT_SACK_PERMITTED, no value required. +/// - 8: TCPOPT_TIMESTAMP, 8 bytes value for timestamp and echo timestamp, 4 bytes each one. +fn insert_tcp_v6_options( + register: &Register, + _configs: &Context, +) -> Result { + let buf = match register.named("tcp") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => { + return Err(FunctionErrorKind::Dirty( + "insert_tcp_options: missing field".to_string(), + )); + } + }; + + let ip = packet::ipv4::Ipv4Packet::new(&buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + let iph_len = ip.get_header_length() as usize * 4; // the header length is given in 32-bits words + let ori_tcp_buf = <&[u8]>::clone(&ip.payload()).to_owned(); + let mut ori_tcp: packet::tcp::MutableTcpPacket; + + // Get the new data or use the existing one. + let data = match register.named("data") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + Some(ContextType::Value(NaslValue::String(d))) => d.as_bytes().to_vec(), + Some(ContextType::Value(NaslValue::Number(d))) => d.to_be_bytes().to_vec(), + _ => { + let tcp = TcpPacket::new(&ori_tcp_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + tcp.payload().to_vec() + } + }; + + // Forge the options field + let positional = register.positional(); + if positional.is_empty() { + return Err(FunctionErrorKind::Dirty( + "Missing optional arguments. At least one optional porsitional argument followed by its value must be given".to_string() + )); + } + + let mut opts: Vec = vec![]; + let mut opts_len = 0; + let mut opts_iter = positional.iter(); + loop { + match opts_iter.next() { + Some(NaslValue::Number(o)) if *o == 2 => { + if let Some(NaslValue::Number(val)) = opts_iter.next() { + let v = *val as u16; + opts.push(TcpOption::mss(v)); + opts_len += 4; + } else { + return Err(FunctionErrorKind::WrongArgument( + "Invalid value for tcp option TCPOPT_MAXSEG".to_string(), + )); + } + } + Some(NaslValue::Number(o)) if *o == 3 => { + if let Some(NaslValue::Number(val)) = opts_iter.next() { + let v = *val as u8; + opts.push(TcpOption::wscale(v)); + opts_len += 3; + } else { + return Err(FunctionErrorKind::WrongArgument( + "Invalid value for tcp option TCPOPT_WINDOW".to_string(), + )); + } + } + + Some(NaslValue::Number(o)) if *o == 4 => { + opts.push(TcpOption::sack_perm()); + opts_len += 2; + } + Some(NaslValue::Number(o)) if *o == 8 => { + if let Some(NaslValue::Number(val1)) = opts_iter.next() { + if let Some(NaslValue::Number(val2)) = opts_iter.next() { + let v1 = *val1 as u32; + let v2 = *val2 as u32; + opts.push(TcpOption::timestamp(v1, v2)); + opts_len += 10; + } else { + return Err(FunctionErrorKind::WrongArgument( + "Invalid value for tcp option TCPOPT_TIMESTAMP".to_string(), + )); + } + } else { + return Err(FunctionErrorKind::WrongArgument( + "Invalid value for tcp option TCPOPT_TIMESTAMP".to_string(), + )); + } + } + None => break, + _ => { + return Err(FunctionErrorKind::WrongArgument( + "insert_tcp_options: invalid tcp option".to_string(), + )); + } + } + } + + // Padding for completing a 32-bit word + if opts_len > 0 { + //Since there are options, we add 1 for the EOL + opts_len += 1; + let padding = 4 - (opts_len % 4); + for _i in 0..padding { + opts.push(TcpOption::nop()); + opts_len += 1; + } + } + + assert_eq!(opts_len % 4, 0); + + let mut new_buf: Vec; + //Prepare a new buffer with new size, copy the tcp header and set the new data + let tcp_total_length = 20 + opts_len + data.len(); + new_buf = vec![0u8; tcp_total_length]; + //new_buf[..20].copy_from_slice(&ori_tcp_buf[..20]); + safe_copy_from_slice(&mut new_buf[..], 0, 20, &ori_tcp_buf, 0, 20)?; + + ori_tcp = packet::tcp::MutableTcpPacket::new(&mut new_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + // At this point, opts len is a 4bytes multiple and the ofset is expressed in 32bits words + ori_tcp.set_data_offset(5 + opts_len as u8 / 4); + if !opts.is_empty() { + ori_tcp.set_options(&opts); + } + if !data.is_empty() { + ori_tcp.set_payload(&data); + } + + // Set the checksum for the tcp segment + let chksum = match register.named("th_sum") { + Some(ContextType::Value(NaslValue::Number(x))) if *x != 0 => (*x as u16).to_be(), + _ => { + let pkt = packet::ipv4::Ipv4Packet::new(&buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + let tcp_aux = TcpPacket::new(ori_tcp.packet()).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + pnet::packet::tcp::ipv4_checksum(&tcp_aux, &pkt.get_source(), &pkt.get_destination()) + } + }; + ori_tcp.set_checksum(chksum); + + // Create a owned copy of the final tcp segment, which will be appended as payload to the IP packet. + let mut fin_tcp_buf: Vec = vec![0u8; tcp_total_length]; + let buf_aux = <&[u8]>::clone(&ori_tcp.packet()).to_owned(); + fin_tcp_buf.clone_from_slice(&buf_aux); + + // Create a new IP packet with the original IP header, and the new TCP payload + let mut new_ip_buf = vec![0u8; iph_len]; + //new_ip_buf[..].copy_from_slice(&buf[..iph_len]); + safe_copy_from_slice(&mut new_ip_buf[..], 0, iph_len, &buf, 0, iph_len)?; + new_ip_buf.append(&mut fin_tcp_buf.to_vec()); + + let l = new_ip_buf.len(); + let mut pkt = packet::ipv4::MutableIpv4Packet::new(&mut new_ip_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + // pnet will panic if the total length set in the ip datagram field does not much with the total length. + // Therefore, the total length is set to the right one before setting the payload. + // By default it was always updated, but if desired, the original length is set again after setting the payload. + let original_ip_len = pkt.get_total_length(); + pkt.set_total_length(l as u16); + pkt.set_payload(&fin_tcp_buf); + match register.named("update_ip_len") { + Some(ContextType::Value(NaslValue::Boolean(l))) if !(*l) => { + pkt.set_total_length(original_ip_len); + } + _ => (), + }; + + // New IP checksum + let chksum = checksum(&pkt.to_immutable()); + pkt.set_checksum(chksum); + + Ok(NaslValue::Data(pkt.packet().to_vec())) +} + +/// Receive a list of IPv6 datagrams and print their TCP part in a readable format in the screen. +fn dump_tcp_v6_packet( + register: &Register, + configs: &Context, +) -> Result { + let positional = register.positional(); + if positional.is_empty() { + return Err(FunctionErrorKind::Dirty( + "Missing arguments. It needs at least one tcp packet".to_string(), + )); + } + + for tcp_seg in positional.iter() { + match tcp_seg { + NaslValue::Data(data) => { + let ip = match packet::ipv6::Ipv6Packet::new(data) { + Some(ip) => ip, + None => { + return Err(FunctionErrorKind::WrongArgument( + "Invalid TCP packet".to_string(), + )); + } + }; + + let pkt = packet::tcp::TcpPacket::new(ip.payload()); + print_tcp_packet(&pkt, configs)?; + display_packet(configs.logger(), data); + } + _ => { + return Err(FunctionErrorKind::WrongArgument( + "Invalid ip packet".to_string(), + )); + } + } + } + Ok(NaslValue::Null) +} + +// UDP over IPv6 + +/// Fills an IP v6 packet with UDP datagram. Note that the ip_p field is not updated. It returns the modified IP datagram. Its arguments are: +/// +/// - data: is the payload. +/// - ip6: is the IP datagram to be filled. +/// - uh_dport: is the destination port. NASL will convert it into network order if necessary. 0 by default. +/// - uh_sport: is the source port. NASL will convert it into network order if necessary. 0 by default. +/// - uh_sum: is the UDP checksum. Although it is not compulsory, the right value is computed by default. +/// - uh_ulen: is the data length. By default it is set to the length the data argument plus the size of the UDP header. +/// - update_ip6_len: is a flag (TRUE by default). If set, NASL will recompute the size field of the IP datagram. + +/// Returns the modified IP datagram or NULL on error. +fn forge_udp_v6_packet( + register: &Register, + _configs: &Context, +) -> Result { + let mut ip_buf = match register.named("ip6") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => return Err(FunctionErrorKind::from("ip6")), + }; + let original_ip_len = ip_buf.len(); + + let data = match register.named("data") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + Some(ContextType::Value(NaslValue::String(d))) => d.as_bytes().to_vec(), + Some(ContextType::Value(NaslValue::Number(d))) => d.to_be_bytes().to_vec(), + _ => Vec::::new(), + }; + + //udp length + data length + let total_length = 8 + data.len(); + let mut buf = vec![0; total_length]; + let mut udp_datagram = packet::udp::MutableUdpPacket::new(&mut buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + if !data.is_empty() { + udp_datagram.set_payload(&data); + } + + match register.named("uh_sport") { + Some(ContextType::Value(NaslValue::Number(x))) => udp_datagram.set_source(*x as u16), + _ => udp_datagram.set_source(0_u16), + }; + match register.named("uh_dport") { + Some(ContextType::Value(NaslValue::Number(x))) => udp_datagram.set_destination(*x as u16), + _ => udp_datagram.set_destination(0_u16), + }; + + match register.named("uh_len") { + Some(ContextType::Value(NaslValue::Number(x))) => udp_datagram.set_length(*x as u16), + _ => udp_datagram.set_length(8u16), + }; + + let chksum = match register.named("th_sum") { + Some(ContextType::Value(NaslValue::Number(x))) if *x != 0 => (*x as u16).to_be(), + _ => { + let pkt = packet::ipv6::Ipv6Packet::new(&ip_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + let udp_aux = UdpPacket::new(udp_datagram.packet()).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + pnet::packet::udp::ipv6_checksum(&udp_aux, &pkt.get_source(), &pkt.get_destination()) + } + }; + + let mut udp_datagram = packet::udp::MutableUdpPacket::new(&mut buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + udp_datagram.set_checksum(chksum); + + ip_buf.append(&mut buf); + let l = ip_buf.len(); + let mut pkt = packet::ipv6::MutableIpv6Packet::new(&mut ip_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + pkt.set_payload_length(l as u16); + match register.named("update_ip_len") { + Some(ContextType::Value(NaslValue::Boolean(l))) if !(*l) => { + pkt.set_payload_length(original_ip_len as u16); + } + _ => (), + }; + + Ok(NaslValue::Data(ip_buf)) +} + +/// Get an UDP element from a IP packet. It returns a data block or an integer, according to the type of the element. Its arguments are: +/// - udp: is the IP datagram. +/// - element: is the name of the field to get +/// +/// Valid IP elements to get are: +/// - uh_sport +/// - uh_dport +/// - uh_ulen +/// - uh_sum +/// - data +fn get_udp_v6_element( + register: &Register, + _configs: &Context, +) -> Result { + let buf = match register.named("udp") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => { + return Err(FunctionErrorKind::from("udp")); + } + }; + + let ip = packet::ipv6::Ipv6Packet::new(&buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + let udp = packet::udp::UdpPacket::new(ip.payload()).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + get_udp_element_from_datagram(udp, register) +} + +/// This function modifies the UDP fields of an IPv6 packet. Its arguments are: +/// +/// - udp: is the IP v6 packet to be filled. +/// - data: is the payload. +/// - uh_dport: is the destination port. NASL will convert it into network order if necessary. 0 by default. +/// - uh_sport: is the source port. NASL will convert it into network order if necessary. 0 by default. +/// - uh_sum: is the UDP checksum. Although it is not compulsory, the right value is computed by default. +/// - uh_ulen: is the data length. By default it is set to the length the data argument plus the size of the UDP header. +fn set_udp_v6_elements( + register: &Register, + _configs: &Context, +) -> Result { + let buf = match register.named("udp") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => { + return Err(FunctionErrorKind::from("udp")); + } + }; + + let ip = packet::ipv6::Ipv6Packet::new(&buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + let iph_len = ip.get_payload_length() as usize * 4; // the header length is given in 32-bits words + + let data = match register.named("data") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + Some(ContextType::Value(NaslValue::String(d))) => d.as_bytes().to_vec(), + Some(ContextType::Value(NaslValue::Number(d))) => d.to_be_bytes().to_vec(), + _ => Vec::::new(), + }; + + let ori_udp_buf = <&[u8]>::clone(&ip.payload()).to_owned(); + let mut ori_udp: packet::udp::MutableUdpPacket; + + let mut new_buf: Vec; + let udp_total_length: usize; + if !data.is_empty() { + //Prepare a new buffer with new size, copy the udp header and set the new data + udp_total_length = 8 + data.len(); + new_buf = vec![0u8; udp_total_length]; + //new_buf[..8].copy_from_slice(&ori_udp_buf[..8]); + safe_copy_from_slice(&mut new_buf[..], 0, 8, &ori_udp_buf, 0, 8)?; + + ori_udp = packet::udp::MutableUdpPacket::new(&mut new_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + ori_udp.set_payload(&data); + } else { + // Copy the original udp buffer into the new buffer + udp_total_length = ip.payload().len(); + new_buf = vec![0u8; udp_total_length]; + //new_buf[..].copy_from_slice(&ori_udp_buf); + safe_copy_from_slice( + &mut new_buf[..], + 0, + udp_total_length, + &ori_udp_buf, + 0, + ori_udp_buf.len(), + )?; + ori_udp = packet::udp::MutableUdpPacket::new(&mut new_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + } + + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("uh_sport") { + ori_udp.set_source(*x as u16); + }; + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("uh_dport") { + ori_udp.set_destination(*x as u16); + }; + + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("uh_len") { + ori_udp.set_length(*x as u16); + }; + + // Set the checksum for the udp segment + let chksum = match register.named("uh_sum") { + Some(ContextType::Value(NaslValue::Number(x))) if *x != 0 => (*x as u16).to_be(), + _ => { + let pkt = packet::ipv6::Ipv6Packet::new(&buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + let udp_aux = UdpPacket::new(ori_udp.packet()).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + pnet::packet::udp::ipv6_checksum(&udp_aux, &pkt.get_source(), &pkt.get_destination()) + } + }; + ori_udp.set_checksum(chksum); + + // Create a owned copy of the final udp segment, which will be appended as payload to the IP packet. + let mut fin_udp_buf: Vec = vec![0u8; udp_total_length]; + let buf_aux = <&[u8]>::clone(&ori_udp.packet()).to_owned(); + fin_udp_buf.clone_from_slice(&buf_aux); + + // Create a new IP packet with the original IP header, and the new UDP payload + let mut new_ip_buf = vec![0u8; iph_len]; + //new_ip_buf[..].copy_from_slice(&buf[..iph_len]); + safe_copy_from_slice(&mut new_ip_buf[..], 0, iph_len, &buf, 0, iph_len)?; + new_ip_buf.append(&mut fin_udp_buf.to_vec()); + + let l = new_ip_buf.len(); + let mut pkt = packet::ipv6::MutableIpv6Packet::new(&mut new_ip_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + pkt.set_payload_length(l as u16); + pkt.set_payload(&fin_udp_buf); + + Ok(NaslValue::Data(pkt.packet().to_vec())) +} + +/// Receive a list of IPv4 datagrams and print their UDP part in a readable format in the screen. +fn dump_udp_v6_packet( + register: &Register, + configs: &Context, +) -> Result { + let positional = register.positional(); + if positional.is_empty() { + return Err(FunctionErrorKind::Dirty( + "Missing arguments. It needs at least one UDP packet".to_string(), + )); + } + + for udp_datagram in positional.iter() { + match udp_datagram { + NaslValue::Data(data) => { + let ip = match packet::ipv6::Ipv6Packet::new(data) { + Some(ip) => ip, + None => { + return Err(FunctionErrorKind::WrongArgument( + "Invalid UDP packet".to_string(), + )); + } + }; + + let datagram = packet::udp::UdpPacket::new(ip.payload()); + dump_udp(&datagram, data, configs)?; + } + _ => { + return Err(FunctionErrorKind::WrongArgument( + "Invalid UDP packet".to_string(), + )); + } + } + } + + Ok(NaslValue::Null) +} + +// ICMP6 + +/// Fills an IPv6 packet with ICMPv6 data. Note that the ip_p field is not updated. It returns the modified IP datagram. Its arguments are: +/// - *ip6*: IP datagram that is updated. +/// - *data*: Payload. +/// - *icmp_cksum*: Checksum, computed by default. +/// - *icmp_code*: ICMP code. 0 by default. +/// - *icmp_id*: ICMP ID. 0 by default. +/// - *icmp_seq*: ICMP sequence number. +/// - *icmp_type*: ICMP type. 0 by default. +/// - *reachable_time*: +/// - *retransmit_time*: +/// - *flags*: +/// - *target*: +/// - *update_ip_len*: If this flag is set, NASL will recompute the size field of the IP datagram. Default: True. +fn forge_icmp_v6_packet( + register: &Register, + _configs: &Context, +) -> Result { + let mut ip_buf = match register.named("ip6") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => { + return Err(FunctionErrorKind::from("icmp6")); + } + }; + + let original_ip_len = ip_buf.len(); + // to extract the max hop limit, ip6_dst. + let pkt_aux = packet::ipv6::Ipv6Packet::new(&ip_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + let data = match register.named("data") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + Some(ContextType::Value(NaslValue::String(d))) => d.as_bytes().to_vec(), + Some(ContextType::Value(NaslValue::Number(d))) => d.to_be_bytes().to_vec(), + _ => Vec::::new(), + }; + + let icmp_code = match register.named("icmp_code") { + Some(ContextType::Value(NaslValue::Number(x))) => packet::icmpv6::Icmpv6Code::new(*x as u8), + _ => packet::icmpv6::Icmpv6Code::new(0u8), + }; + + let total_length: usize; + let icmp_pkt_size: usize; + let mut icmp_buf: Vec; + + let icmp_type = match register.named("icmp_type") { + Some(ContextType::Value(NaslValue::Number(x))) => { + if *x < 0 || *x > 255 { + return Err(FunctionErrorKind::Dirty(format!( + "forge_icmp_v6_packet: illegal type {}", + x + ))); + } + packet::icmpv6::Icmpv6Type::new(*x as u8) + } + _ => { + return Err(FunctionErrorKind::Dirty( + "forge_icmp_v6_packet: illegal type".to_string(), + )); + } + }; + + match icmp_type { + Icmpv6Types::EchoRequest => { + icmp_pkt_size = 8; + total_length = 8 + data.len(); + icmp_buf = vec![0; total_length]; + let mut icmp_pkt = + packet::icmpv6::MutableIcmpv6Packet::new(&mut icmp_buf).ok_or_else(|| { + FunctionErrorKind::Dirty( + "No possible to create a packet from buffer".to_string(), + ) + })?; + + icmp_pkt.set_icmpv6_type(icmp_type); + icmp_pkt.set_icmpv6_code(icmp_code); + + if !data.is_empty() { + //buf[8..].copy_from_slice(&data[0..]); + safe_copy_from_slice(&mut icmp_buf, 8, total_length, &data[..], 0, data.len())?; + } + + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("icmp_id") { + //buf[4..6].copy_from_slice(&x.to_le_bytes()[0..2]); + safe_copy_from_slice(&mut icmp_buf, 4, 6, &x.to_le_bytes()[..], 0, 2)?; + } + + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("icmp_seq") { + //buf[6..8].copy_from_slice(&x.to_le_bytes()[0..2]); + safe_copy_from_slice(&mut icmp_buf, 6, 8, &x.to_le_bytes()[..], 0, 2)?; + } + } + Icmpv6Types::RouterSolicit => { + icmp_pkt_size = 8; + total_length = icmp_pkt_size + data.len(); + icmp_buf = vec![0; total_length]; + let mut icmp_pkt = packet::icmpv6::ndp::MutableRouterSolicitPacket::new(&mut icmp_buf) + .ok_or_else(|| { + FunctionErrorKind::Dirty( + "No possible to create a packet from buffer".to_string(), + ) + })?; + + icmp_pkt.set_icmpv6_type(icmp_type); + icmp_pkt.set_icmpv6_code(icmp_code); + + if !data.is_empty() { + //buf[8..].copy_from_slice(&data[0..]); + safe_copy_from_slice(&mut icmp_buf, 8, total_length, &data[..], 0, data.len())?; + } + } + Icmpv6Types::RouterAdvert => { + icmp_pkt_size = MutableRouterAdvertPacket::minimum_packet_size(); + total_length = icmp_pkt_size + data.len(); + icmp_buf = vec![0; total_length]; + let mut icmp_pkt = MutableRouterAdvertPacket::new(&mut icmp_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + icmp_pkt.set_icmpv6_type(icmp_type); + icmp_pkt.set_icmpv6_code(icmp_code); + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("reachable_time") + { + icmp_pkt.set_reachable_time(*x as u32); + } + + if let Some(ContextType::Value(NaslValue::Number(x))) = + register.named("retransmit_time") + { + icmp_pkt.set_retrans_time(*x as u32); + } + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("flags") { + icmp_pkt.set_flags(*x as u8); + } + + icmp_pkt.set_hop_limit(pkt_aux.get_hop_limit()); + + if !data.is_empty() { + //buf[8..].copy_from_slice(&data[0..]); + safe_copy_from_slice( + &mut icmp_buf, + icmp_pkt_size, + total_length, + &data[..], + 0, + data.len(), + )?; + } + } + Icmpv6Types::NeighborSolicit => { + icmp_pkt_size = MutableNeighborSolicitPacket::minimum_packet_size(); + total_length = icmp_pkt_size + data.len(); + icmp_buf = vec![0; total_length]; + let mut icmp_pkt = + MutableNeighborSolicitPacket::new(&mut icmp_buf).ok_or_else(|| { + FunctionErrorKind::Dirty( + "No possible to create a packet from buffer".to_string(), + ) + })?; + + icmp_pkt.set_icmpv6_type(icmp_type); + icmp_pkt.set_icmpv6_code(icmp_code); + icmp_pkt.set_target_addr(pkt_aux.get_destination()); + + if !data.is_empty() { + //buf[8..].copy_from_slice(&data[0..]); + safe_copy_from_slice( + &mut icmp_buf, + icmp_pkt_size, + total_length, + &data[..], + 0, + data.len(), + )?; + } + } + Icmpv6Types::NeighborAdvert => { + icmp_pkt_size = MutableNeighborAdvertPacket::minimum_packet_size(); + total_length = icmp_pkt_size + data.len(); + icmp_buf = vec![0; total_length]; + let mut icmp_pkt = + MutableNeighborAdvertPacket::new(&mut icmp_buf).ok_or_else(|| { + FunctionErrorKind::Dirty( + "No possible to create a packet from buffer".to_string(), + ) + })?; + + icmp_pkt.set_icmpv6_type(icmp_type); + icmp_pkt.set_icmpv6_code(icmp_code); + + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("flags") { + icmp_pkt.set_flags(*x as u8); + if (*x as u8) & 0b10000000_u8 == 0b10000000 { + icmp_pkt.set_target_addr(pkt_aux.get_source()); + } else if let Some(ContextType::Value(NaslValue::String(x))) = + register.named("target") + { + if let Ok(ip) = Ipv6Addr::from_str(x) { + icmp_pkt.set_target_addr(ip); + } + } else { + return Err( + FunctionErrorKind::Dirty( + "forge_icmp_v6_package: missing 'target' parameter required for constructing response to a Neighbor Solicitation".to_string())); + } + } + } + _ => { + icmp_pkt_size = 8; + total_length = icmp_pkt_size + data.len(); + icmp_buf = vec![0; total_length]; + let mut icmp_pkt = + packet::icmpv6::MutableIcmpv6Packet::new(&mut icmp_buf).ok_or_else(|| { + FunctionErrorKind::Dirty( + "No possible to create a packet from buffer".to_string(), + ) + })?; + + icmp_pkt.set_icmpv6_type(icmp_type); + icmp_pkt.set_icmpv6_code(icmp_code); + + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("icmp_id") { + //buf[4..6].copy_from_slice(&x.to_le_bytes()[0..2]); + safe_copy_from_slice(&mut icmp_buf, 4, 6, &x.to_le_bytes()[..], 0, 2)?; + } + + if let Some(ContextType::Value(NaslValue::Number(x))) = register.named("icmp_seq") { + //buf[6..8].copy_from_slice(&x.to_le_bytes()[0..2]); + safe_copy_from_slice(&mut icmp_buf, 6, 8, &x.to_le_bytes()[..], 0, 2)?; + } + } + } + + if !data.is_empty() { + //buf[8..].copy_from_slice(&data[0..]); + safe_copy_from_slice( + &mut icmp_buf, + icmp_pkt_size, + total_length, + &data[..], + 0, + data.len(), + )?; + } + + ip_buf.append(&mut icmp_buf); + let l = ip_buf.len(); + let mut pkt = packet::ipv6::MutableIpv6Packet::new(&mut ip_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + let chksum = match register.named("icmp_cksum") { + Some(ContextType::Value(NaslValue::Number(x))) if *x != 0 => (*x as u16).to_be(), + _ => { + let icmp_aux = Icmpv6Packet::new(&icmp_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + pnet::packet::icmpv6::checksum(&icmp_aux, &pkt.get_source(), &pkt.get_destination()) + } + }; + + let mut icmp_pkt = + packet::icmpv6::MutableIcmpv6Packet::new(&mut icmp_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + icmp_pkt.set_checksum(chksum); + + pkt.set_payload_length(l as u16); + match register.named("update_ip_len") { + Some(ContextType::Value(NaslValue::Boolean(l))) if !(*l) => { + pkt.set_payload_length(original_ip_len as u16); + } + _ => (), + }; + + Ok(NaslValue::Data(ip_buf)) +} + +/// Get an ICMPv6 element from a IPv6 packet. It returns a data block or an integer, according to the type of the element. Its arguments are: +/// - icmp: is the IP datagram (not the ICMP part only). +/// - element: is the name of the field to get +/// +/// Valid ICMP elements to get are: +/// - icmp_id +/// - icmp_code +/// - icmp_type +/// - icmp_seq +/// - icmp_chsum +/// - icmp_data +fn get_icmp_v6_element( + register: &Register, + _configs: &Context, +) -> Result { + let buf = match register.named("icmp") { + Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), + _ => { + return Err(FunctionErrorKind::from("icmp")); + } + }; + + let ip = packet::ipv6::Ipv6Packet::new(&buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + let icmp = packet::icmpv6::Icmpv6Packet::new(ip.payload()).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + match register.named("element") { + Some(ContextType::Value(NaslValue::String(el))) => match el.as_str() { + "icmp_code" => Ok(NaslValue::Number(icmp.get_icmpv6_code().0 as i64)), + "icmp_type" => Ok(NaslValue::Number(icmp.get_icmpv6_type().0 as i64)), + "icmp_cksum" => Ok(NaslValue::Number(icmp.get_checksum() as i64)), + "icmp_id" => { + if icmp.payload().len() >= 4 { + let pl = icmp.payload(); + let mut id = [0u8; 8]; + //id[..2].copy_from_slice(&pl[..2]); + safe_copy_from_slice(&mut id, 0, 2, pl, 0, 2)?; + Ok(NaslValue::Number(i64::from_le_bytes(id))) + } else { + Ok(NaslValue::Number(0)) + } + } + "icmp_seq" => { + if icmp.payload().len() >= 4 { + let pl = icmp.payload(); + let mut seq = [0u8; 8]; + //seq[0..2].copy_from_slice(&pl[2..4]); + safe_copy_from_slice(&mut seq, 0, 2, pl, 2, 4)?; + + Ok(NaslValue::Number(i64::from_le_bytes(seq))) + } else { + Ok(NaslValue::Number(0)) + } + } + "data" => { + if icmp.payload().len() > 4 { + let buf = icmp.payload(); + Ok(NaslValue::Data(buf[4..].to_vec())) + } else { + Ok(NaslValue::Null) + } + } + _ => Ok(NaslValue::Null), + }, + _ => Err(FunctionErrorKind::from("element")), + } +} +/// Receive a list of IPv4 ICMP packets and print them in a readable format in the screen. +fn dump_icmp_v6_packet( + register: &Register, + configs: &Context, +) -> Result { + let positional = register.positional(); + if positional.is_empty() { + return Err(FunctionErrorKind::from("icmp")); + } + + for icmp_pkt in positional.iter() { + let buf = match icmp_pkt { + NaslValue::Data(d) => d.clone(), + _ => { + continue; + } + }; + + let ip = packet::ipv6::Ipv6Packet::new(&buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + let icmp = packet::icmp::IcmpPacket::new(ip.payload()).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + let mut icmp_seq = 0; + if icmp.payload().len() >= 4 { + let pl = icmp.payload(); + let mut seq = [0u8; 8]; + //seq[0..2].copy_from_slice(&pl[2..4]); + safe_copy_from_slice(&mut seq, 0, 2, pl, 2, 4)?; + icmp_seq = i64::from_le_bytes(seq); + } + + let mut icmp_id = 0; + if icmp.payload().len() >= 4 { + let pl = icmp.payload(); + let mut id = [0u8; 8]; + //id[..2].copy_from_slice(&pl[..2]); + safe_copy_from_slice(&mut id, 0, 2, pl, 0, 2)?; + icmp_id = i64::from_le_bytes(id); + } + + let mut data = vec![]; + if icmp.payload().len() > 4 { + let buf = icmp.payload(); + data = buf[4..].to_vec(); + } + + configs.logger().info(&"------"); + configs + .logger() + .info(&format!("\ticmp6_id : {:?}", icmp_id)); + configs + .logger() + .info(&format!("\ticmp6_code : {:?}", icmp.get_icmp_code())); + configs + .logger() + .info(&format!("\ticmp_type : {:?}", icmp.get_icmp_type())); + configs + .logger() + .info(&format!("\ticmp6_seq : {:?}", icmp_seq)); + configs + .logger() + .info(&format!("\ticmp6_cksum : {:?}", icmp.get_checksum())); + configs.logger().info(&format!("\tData : {:?}", data)); + configs.logger().info(&"\n"); + } + Ok(NaslValue::Null) +} + +fn forge_igmp_v6_packet( + _register: &Register, + _configs: &Context, +) -> Result { + // TODO: not implemented. Multicast management on IPv6 networks is handled by Multicast + // Listener Discovery (MLD) which is a part of ICMPv6 in contrast to IGMP's bare IP encapsulation. + // Currently, pnet does not support MDL. + Ok(NaslValue::Null) +} + +/// This function tries to open a TCP connection and sees if anything comes back (SYN/ACK or RST). +/// +/// Its argument is: +/// - port: port for the ping +fn nasl_tcp_v6_ping( + register: &Register, + configs: &Context, +) -> Result { + let rnd_tcp_port = || -> u16 { (random_impl().unwrap_or(0) % 65535 + 1024) as u16 }; + + let sports_ori: Vec = vec![ + 0, 0, 0, 0, 0, 1023, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0, 20, 0, 25, 0, 0, 0, + ]; + let mut sports: Vec = vec![]; + let ports = [ + 139, 135, 445, 80, 22, 515, 23, 21, 6000, 1025, 25, 111, 1028, 9100, 1029, 79, 497, 548, + 5000, 1917, 53, 161, 9001, 65535, 443, 113, 993, 8080, + ]; + + for p in sports_ori.iter() { + if *p == 0u16 { + sports.push(rnd_tcp_port()); + } else { + sports.push(*p); + } + } + + let soc = new_raw_ipv6_socket()?; + if let Err(e) = soc.set_header_included(true) { + return Err(FunctionErrorKind::Dirty(format!( + "Not possible to create a raw socket: {}", + e + ))); + }; + + // Get the iface name, to set the capture device. + let target_ip = get_host_ip(configs)?; + let local_ip = get_source_ip(target_ip, 50000u16)?; + let iface = get_interface_by_local_ip(local_ip)?; + + let port = match register.named("port") { + Some(ContextType::Value(NaslValue::Number(x))) => *x, + None => 0, //TODO: implement plug_get_host_open_port() + _ => return Err(("Number", "Invalid length value").into()), + }; + + if islocalhost(target_ip) { + return Ok(NaslValue::Number(1)); + } + + let mut capture_dev = match Capture::from_device(iface) { + Ok(c) => match c.promisc(true).timeout(100).open() { + Ok(capture) => capture, + Err(e) => return custom_error!("send_packet: {}", e), + }, + Err(e) => return custom_error!("send_packet: {}", e), + }; + let filter = format!("ip and src host {}", target_ip); + + let mut ip_buf = [0u8; 40]; + let mut ip = MutableIpv6Packet::new(&mut ip_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + ip.set_flow_label(0); + ip.set_traffic_class(0); + ip.set_version(6); + ip.set_next_header(IpNextHeaderProtocol(6)); + ip.set_payload_length(40); + let ipv6_src = Ipv6Addr::from_str(&local_ip.to_string()) + .map_err(|_| FunctionErrorKind::WrongArgument("invalid IP".to_string()))?; + ip.set_source(ipv6_src); + + let ipv6_dst = Ipv6Addr::from_str(&target_ip.to_string()) + .map_err(|_| FunctionErrorKind::WrongArgument("invalid IP".to_string()))?; + ip.set_destination(ipv6_dst); + + let mut tcp_buf = [0u8; 20]; + let mut tcp = MutableTcpPacket::new(&mut tcp_buf).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + tcp.set_flags(0x02); //TH_SYN + tcp.set_sequence(random_impl()? as u32); + tcp.set_acknowledgement(0); + tcp.set_data_offset(5); + tcp.set_window(2048); + tcp.set_urgent_ptr(0); + + for (i, _) in sports.iter().enumerate() { + // TODO: the port is fixed since the function to get open ports is not implemented. + let mut sport = rnd_tcp_port(); + let mut dport = port as u16; + if port == 0 { + sport = sports[i]; + dport = ports[i] as u16; + } + + tcp.set_source(sport); + tcp.set_destination(dport); + let chksum = pnet::packet::tcp::ipv6_checksum( + &tcp.to_immutable(), + &ip.get_source(), + &ip.get_destination(), + ); + tcp.set_checksum(chksum); + ip.set_payload(tcp.packet()); + + let sockaddr = socket2::SockAddr::from(SocketAddr::new(target_ip, 0)); + match soc.send_to(ip.packet(), &sockaddr) { + Ok(b) => { + configs.logger().debug(&format!("Sent {} bytes", b)); + } + Err(e) => { + return Err(FunctionErrorKind::Dirty(format!("send_packet: {}", e))); + } + } + + let p = match capture_dev.filter(&filter, true) { + Ok(_) => capture_dev.next_packet(), + Err(e) => Err(pcap::Error::PcapError(e.to_string())), + }; + + if p.is_ok() { + return Ok(NaslValue::Number(1)); + } + } + + Ok(NaslValue::Null) +} + +/// Send a list of packets, passed as unnamed arguments, with the option to listen to the answers. +/// +/// The arguments are: +/// - Any number of packets to send +/// - length: default length of each every packet, if a packet does not fit, its actual size is taken instead +/// - pcap_active: option to capture the answers, TRUE by default +/// - pcap_filter: BPF filter used for the answers +/// - pcap_timeout: time to wait for the answers in seconds, 5 by default +/// - allow_broadcast: default FALSE +fn nasl_send_v6packet( + register: &Register, + configs: &Context, +) -> Result { + let use_pcap = match register.named("pcap_active") { + Some(ContextType::Value(NaslValue::Boolean(x))) => *x, + None => true, + _ => return Err(("Boolean", "Invalid pcap_active value").into()), + }; + + let filter = match register.named("pcap_filter") { + Some(ContextType::Value(NaslValue::String(x))) => x.to_string(), + None => String::new(), + _ => return Err(("String", "Invalid pcap_filter value").into()), + }; + + let timeout = match register.named("pcap_timeout") { + Some(ContextType::Value(NaslValue::Number(x))) => *x as i32 * 1000i32, // to milliseconds + None => DEFAULT_TIMEOUT, + _ => return Err(("Integer", "Invalid timeout value").into()), + }; + + let positional = register.positional(); + if positional.is_empty() { + return Ok(NaslValue::Null); + } + + let soc = new_raw_socket()?; + + if let Err(e) = soc.set_header_included(true) { + return Err(FunctionErrorKind::Dirty(format!( + "Not possible to create a raw socket: {}", + e + ))); + }; + + let _dflt_packet_sz = match register.named("length") { + Some(ContextType::Value(NaslValue::Number(x))) => *x, + None => 0, + _ => return Err(("Number", "Invalid length value").into()), + }; + + // Get the iface name, to set the capture device. + let target_ip = get_host_ip(configs)?; + let local_ip = get_source_ip(target_ip, 50000u16)?; + let iface = get_interface_by_local_ip(local_ip)?; + + let mut capture_dev = match Capture::from_device(iface) { + Ok(c) => match c.promisc(true).timeout(timeout).open() { + Ok(capture) => capture, + Err(e) => return custom_error!("send_packet: {}", e), + }, + Err(e) => return custom_error!("send_packet: {}", e), + }; + + for pkt in positional.iter() { + let packet_raw = match pkt { + NaslValue::Data(data) => data as &[u8], + _ => return Err(("Data", "Invalid packet").into()), + }; + let packet = packet::ipv6::Ipv6Packet::new(packet_raw).ok_or_else(|| { + FunctionErrorKind::Dirty("No possible to create a packet from buffer".to_string()) + })?; + + // No broadcast destination and dst ip address inside the IP packet + // differs from target IP, is consider a malicious or buggy script. + if packet.get_destination() != target_ip { + return Err(FunctionErrorKind::Dirty( + format!("send_packet: malicious or buggy script is trying to send packet to {} instead of designated target {}", + packet.get_destination(), target_ip) + )); + } + + let sock_str = format!("{}:{}", &packet.get_destination().to_string().as_str(), 0); + let sockaddr = match SocketAddr::from_str(&sock_str) { + Ok(addr) => socket2::SockAddr::from(addr), + Err(e) => { + return Err(FunctionErrorKind::Diagnostic( + format!("send_packet: {}", e), + Some(NaslValue::Null), + )); + } + }; + + match soc.send_to(packet_raw, &sockaddr) { + Ok(b) => { + configs.logger().debug(&format!("Sent {} bytes", b)); + } + Err(e) => { + return Err(FunctionErrorKind::Diagnostic( + format!("send_packet: {}", e), + Some(NaslValue::Null), + )); + } + } + + if use_pcap { + let p = match capture_dev.filter(&filter, true) { + Ok(_) => capture_dev.next_packet(), + Err(e) => Err(pcap::Error::PcapError(e.to_string())), + }; + + match p { + Ok(packet) => return Ok(NaslValue::Data(packet.data.to_vec())), + Err(_) => return Ok(NaslValue::Null), + }; + } + } + Ok(NaslValue::Null) +} + /// Returns a NaslVars with all predefined variables which must be expose to nasl script pub fn expose_vars() -> NaslVars<'static> { let builtin_vars: NaslVars = [ @@ -2446,6 +4474,28 @@ pub fn lookup(key: &str) -> Option { "send_packet" => Some(nasl_send_packet), "pcap_next" => Some(nasl_pcap_next), "send_capture" => Some(nasl_send_capture), + //IPv6 + "forge_ip_v6_packet" => Some(forge_ip_v6_packet), + "get_ip_v6_element" => Some(get_ip_v6_element), + "set_ip_v6_elements" => Some(set_ip_v6_elements), + "insert_ip_v6_options" => Some(insert_ip_v6_options), + "dump_ip_v6_packet" => Some(dump_ip_v6_packet), + "forge_tcp_v6_packet" => Some(forge_tcp_v6_packet), + "get_tcp_v6_element" => Some(get_tcp_v6_element), + "get_tcp_v6_option" => Some(get_tcp_v6_option), + "set_tcp_v6_elements" => Some(set_tcp_v6_elements), + "insert_tcp_v6_options" => Some(insert_tcp_v6_options), + "dump_tcp_v6_packet" => Some(dump_tcp_v6_packet), + "tcp_v6_ping" => Some(nasl_tcp_v6_ping), + "forge_udp_v6_packet" => Some(forge_udp_v6_packet), + "get_udp_v6_element" => Some(get_udp_v6_element), + "set_udp_v6_elements" => Some(set_udp_v6_elements), + "dump_udp_v6_packet" => Some(dump_udp_v6_packet), + "forge_icmp_v6_packet" => Some(forge_icmp_v6_packet), + "get_icmp_v6_element" => Some(get_icmp_v6_element), + "dump_icmp_v6_packet" => Some(dump_icmp_v6_packet), + "forge_igmp_v6_packet" => Some(forge_igmp_v6_packet), + "send_v6packet" => Some(nasl_send_v6packet), _ => None, } }