Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Result collector #1574

Merged
merged 10 commits into from
Feb 27, 2024
Merged

Add: Result collector #1574

merged 10 commits into from
Feb 27, 2024

Conversation

jjnicola
Copy link
Member

@jjnicola jjnicola commented Feb 20, 2024

What:
Add: retrieve from redis the host progress/status and scan results
Jira: SC-1002

use std::sync::{Arc, Mutex};

use openvas::{
    openvas_redis::RedisHelper, result_collector::ResultHelper,
};
use redis_storage::{NameSpaceSelector, RedisCtx};

#[tokio::main]
async fn main() -> Result<(), std::fmt::Error> {
    let redis_url = "unix:///run/redis-openvas/redis.sock";
    let mut rctx = RedisCtx::open(redis_url, &[NameSpaceSelector::Fix(2)]).unwrap();
    let cache = RedisCtx::open(redis_url, &[NameSpaceSelector::Key("nvticache")]).unwrap();

    let rc = RedisHelper::<RedisCtx>::new(Arc::new(Mutex::new(cache)), Arc::new(Mutex::new(rctx)));

    let mut p = ResultHelper::init(rc);
    p.results().await.unwrap();
    if let Ok(r) = Arc::as_ref(&p.results).lock() {
        println!("{:?}", r);
    }
    Ok(())
}

Why:

How:

Checklist:

  • Tests
  • PR merge commit message adjusted

Also, improve the creating the RedisHelper, since at this point the nvti cache and a main kb for the scan should have been created.

Usage example. (Needs a running redis instance with an up-to-date nvticache)

```

```use std::sync::{Arc, Mutex};

use models::{AliveTestMethods, Credential, PortRange, Port, Scan, Parameter};
use openvas::{pref_handler::PreferenceHandler, openvas_redis::RedisHelper};
use redis_storage::{NameSpaceSelector, RedisCtx};

async fn main() -> Result<(), std::fmt::Error> {

    // Create an scan config
    let mut scan = Scan::default();
    scan.scan_id = Some("123-456".to_string());
    scan.target.alive_test_methods = vec![AliveTestMethods::Icmp, AliveTestMethods::TcpSyn];
    scan.target.credentials = vec![Credential {
        service: models::Service::SSH,
        port: Some(22),
        credential_type: models::CredentialType::UP {
            username: "user".to_string(),
            password: "pass".to_string(),
            privilege_credential: None,
        },
    }];
    scan.vts.push(models::VT {
        oid: "1.3.6.1.4.1.25623.1.0.112771".to_string(),
        parameters: vec![Parameter {
            id: 1,
            value: "llala".to_string()

        }]
    });
    scan.target.excluded_hosts = vec!["127.0.0.1".to_string()];
    scan.target.hosts = vec!["127.0.0.2".to_string()];
    scan.target.ports = vec![Port {
        protocol: Some(models::Protocol::TCP),
        range: vec![
            PortRange {
                start: 22,
                end: Some(25),
            },
            PortRange {
                start: 80,
                end: None,
            },
        ],
    }];

    let redis_url = "unix:///run/redis-openvas/redis.sock";
    // In this example, a fix db is taken,but the next free can be taken, using the name space selector NameSpaceSelector::Free
    let mut rctx = RedisCtx::open(redis_url, &[NameSpaceSelector::Fix(6)]).unwrap();
    rctx.delete_namespace().unwrap();
    // Create a redis connection to the nvticache, and a connection to the mainkb().
    let cache = RedisCtx::open(redis_url, &[NameSpaceSelector::Key("nvticache")]).unwrap();

    // New redis helper with access to the main kb for storing the preferences, and access to the nvticache
    // for getting info to build preferences.
    let rc = RedisHelper::<RedisCtx>::init(Arc::new(Mutex::new(cache)),Arc::new(Mutex::new(rctx))).unwrap();

    let mut p = PreferenceHandler::new(scan, rc);
    Ok(p.prepare_preferences_for_openvas().await.expect("aaa"))

}
@jjnicola
Copy link
Member Author

Depends on #1572

@jjnicola jjnicola changed the title Result collector Add: Result collector Feb 20, 2024
@jjnicola jjnicola force-pushed the result-collector branch 4 times, most recently from e43354c to 64f4351 Compare February 20, 2024 13:41
Collects results from redis and return a structure with a list of results and the amount of dead host and total amount of alive hosts
@jjnicola jjnicola marked this pull request as ready for review February 21, 2024 14:35
@jjnicola jjnicola requested a review from a team as a code owner February 21, 2024 14:35
nichtsfrei
nichtsfrei previously approved these changes Feb 22, 2024
@jjnicola jjnicola enabled auto-merge (squash) February 27, 2024 07:11
@jjnicola jjnicola merged commit 703e7da into main Feb 27, 2024
18 checks passed
@jjnicola jjnicola deleted the result-collector branch February 27, 2024 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants