1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//! Nessus Vulnerability Scanner API client
//!
//! ```no_run
//! extern crate nessus;
//!
//! use std::time::Duration;
//! use nessus::VulnScanner;
//!
//! fn main() {
//!     let scan_id = 31337;
//!     let client = nessus::Client::new("https://nessus.example.com", "yourtoken", "secrettoken").unwrap();
//!
//!     let scan = client.launch_scan(scan_id).unwrap();
//!     scan.wait(&client, Duration::from_secs(60), Some(30)).unwrap();
//!
//!     let export = client.export_scan(scan_id).unwrap();
//!     export.wait(&client, Duration::from_secs(3), Some(40)).unwrap();
//!
//!     let report = export.download(&client).unwrap();
//!     println!("download: {:?}", report);
//! }
//! ```
extern crate roadrunner;
extern crate tokio_core;
extern crate serde;
#[macro_use] extern crate serde_derive;
extern crate serde_json;
extern crate serde_xml_rs;
extern crate regex;
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate log;
extern crate hyper;
extern crate url;
#[macro_use] extern crate error_chain;

use std::collections::HashMap;
use std::time::Duration;
use std::thread::sleep;

use serde::Serialize;
use serde::de::DeserializeOwned;

use url::Url;
use roadrunner::RestClient;
use roadrunner::RestClientMethods;

/// Nessus reports parser module
pub mod parser;
/// Various structs
pub mod structs;

mod errors {
    use parser;
    use url;
    use roadrunner::{self, Response};

    error_chain! {
        errors {
            Status(r: Response) {
                description("invalid status code")
                display("server sent error: '{:?}'", r)
            }
            WaitTimeout {
                description("wait timeout occured")
                display("task took too long to finish")
            }
        }

        foreign_links {
            RoadRunner(roadrunner::Error);
            Url(url::ParseError);
            Parser(parser::Error);
        }
    }
}
pub use errors::*;

/// Nessus API client
#[derive(Debug)]
pub struct Client {
    host: Url,
    token: String,
    secret: String,
}

pub trait VulnScanner {
    fn list_policies(&self) -> Result<structs::PolicyReponse>;

    fn create_scan(&self, template_uuid: &str, settings: structs::ScanSettings) -> Result<structs::CreateScanResponse>;

    fn configure_scan(&self, scan_id: u64, template_uuid: Option<String>, settings: structs::ScanSettingsUpdate) -> Result<structs::UpdateScanResponse>;

    fn delete_history(&self, scan_id: u64, history_id: u64) -> Result<()>;

    fn delete_scan(&self, scan_id: u64) -> Result<()>;

    fn launch_scan(&self, id: u64) -> Result<structs::ScanLaunchResponse>;

    fn stop_scan(&self, id: u64) -> Result<()>;

    fn pause_scan(&self, id: u64) -> Result<()>;

    fn resume_scan(&self, id: u64) -> Result<()>;

    fn scan_details(&self, id: u64) -> Result<structs::ScanDetails>;

    fn list_scans(&self) -> Result<structs::ScanListResponse>;

    fn list_scan_folder(&self, id: u64) -> Result<structs::ScanListResponse>;

    fn export_scan(&self, scan_id: u64) -> Result<structs::ExportToken>;

    fn export_status(&self, scan_id: u64, file_id: u64) -> Result<structs::ExportStatus>;

    fn download_export_raw(&self, scan_id: u64, file_id: u64) -> Result<String>;

    fn download_export(&self, scan_id: u64, file_id: u64) -> Result<parser::NessusClientDatav2>;
}

impl Client {
    pub fn new<I: Into<String>>(host: &str, token: I, secret: I) -> Result<Client> {
        Ok(Client {
            host: Url::parse(host)?,
            token: token.into(),
            secret: secret.into(),
        })
    }

    #[inline]
    fn deserialize<T: DeserializeOwned>(&self, response: roadrunner::Response) -> Result<T> {
        info!("Response: {:?}", response);

        let obj = response.content().as_typed()?;
        Ok(obj)
    }

    #[inline]
    fn assure_ok(response: roadrunner::Response) -> Result<roadrunner::Response> {
        let is_ok = {
            let status = response.status();
            *status == hyper::StatusCode::Ok || *status == hyper::StatusCode::Created
        };

        if ! is_ok {
            Err(Error::from(ErrorKind::Status(response)))
        } else {
            Ok(response)
        }
    }

    #[inline]
    fn mkurl(&self, path: &str) -> Result<Url> {
        Ok(self.host.join(path)?)
    }

    #[inline]
    fn raw_get(&self, path: &str) -> Result<roadrunner::Response> {
        let mut core = tokio_core::reactor::Core::new().unwrap();

        let response = RestClient::get(self.mkurl(path)?.as_str())
            .header_append_raw("X-ApiKeys", format!("accessKey={}; secretKey={}", self.token, self.secret))
            .execute_on(&mut core)?;

        Client::assure_ok(response)
    }

    #[inline]
    fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
        let x = self.raw_get(path)?;
        self.deserialize(x)
    }

    #[inline]
    fn post_empty<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
        let mut core = tokio_core::reactor::Core::new().unwrap();

        let response = RestClient::post(self.mkurl(path)?.as_str())
            .header_append_raw("X-ApiKeys", format!("accessKey={}; secretKey={}", self.token, self.secret))
            .execute_on(&mut core)?;

        self.deserialize(Client::assure_ok(response)?)
    }

    #[inline]
    fn post<T: Serialize, R: DeserializeOwned>(&self, path: &str, msg: T) -> Result<R> {
        let mut core = tokio_core::reactor::Core::new().unwrap();

        let response = RestClient::post(self.mkurl(path)?.as_str())
            .header_append_raw("X-ApiKeys", format!("accessKey={}; secretKey={}", self.token, self.secret))
            .json_body_typed(&msg)
            .execute_on(&mut core)?;

        self.deserialize(Client::assure_ok(response)?)
    }

    #[inline]
    fn put<T: Serialize, R: DeserializeOwned>(&self, path: &str, msg: T) -> Result<R> {
        let mut core = tokio_core::reactor::Core::new().unwrap();

        let response = RestClient::put(self.mkurl(path)?.as_str())
            .header_append_raw("X-ApiKeys", format!("accessKey={}; secretKey={}", self.token, self.secret))
            .json_body_typed(&msg)
            .execute_on(&mut core)?;

        self.deserialize(Client::assure_ok(response)?)
    }

    #[inline]
    fn raw_delete(&self, path: &str) -> Result<roadrunner::Response> {
        let mut core = tokio_core::reactor::Core::new().unwrap();

        let response = RestClient::get(self.mkurl(path)?.as_str())
            .header_append_raw("X-ApiKeys", format!("accessKey={}; secretKey={}", self.token, self.secret))
            .execute_on(&mut core)?;

        Client::assure_ok(response)
    }
}

impl VulnScanner for Client {
    fn list_policies(&self) -> Result<structs::PolicyReponse> {
        self.get("/editor/policy/templates")
    }

    fn create_scan(&self, template_uuid: &str, settings: structs::ScanSettings) -> Result<structs::CreateScanResponse> {
        let request = structs::CreateScanRequest {
            uuid: template_uuid.into(),
            settings: settings,
        };
        let scan: structs::CreateScanResponse = self.post("/scans", request)?;
        Ok(scan)
    }

    fn configure_scan(&self, scan_id: u64, template_uuid: Option<String>, settings: structs::ScanSettingsUpdate) -> Result<structs::UpdateScanResponse> {
        let request = structs::UpdateScanRequest {
            uuid: template_uuid,
            settings: settings,
        };

        let scan: structs::UpdateScanResponse = self.put(&format!("/scans/{}", scan_id), request)?;
        Ok(scan)
    }

    fn delete_history(&self, scan_id: u64, history_id: u64) -> Result<()> {
        self.raw_delete(&format!("/scans/{}/history/{}", scan_id, history_id))?;
        Ok(())
    }

    fn delete_scan(&self, scan_id: u64) -> Result<()> {
        self.raw_delete(&format!("/scans/{}", scan_id))?;
        Ok(())
    }

    fn launch_scan(&self, id: u64) -> Result<structs::ScanLaunchResponse> {
        let mut launch: structs::ScanLaunchResponse = self.post_empty(&format!("/scans/{}/launch", id))?;

        launch.scan_id = Some(id);
        Ok(launch)
    }

    fn stop_scan(&self, id: u64) -> Result<()> {
        self.post_empty(&format!("/scans/{}/stop", id))
    }

    fn pause_scan(&self, id: u64) -> Result<()> {
        self.post_empty(&format!("/scans/{}/pause", id))
    }

    fn resume_scan(&self, id: u64) -> Result<()> {
        self.post_empty(&format!("/scans/{}/resume", id))
    }

    fn scan_details(&self, id: u64) -> Result<structs::ScanDetails> {
        self.get(&format!("/scans/{}", id))
    }

    fn list_scans(&self) -> Result<structs::ScanListResponse> {
        self.get("/scans")
    }

    fn list_scan_folder(&self, id: u64) -> Result<structs::ScanListResponse> {
        // TODO: use ?folder_id=
        let response = self.list_scans()?;

        let folders = response.folders.into_iter()
                        .filter(|x| x.id == id)
                        .collect();
        let scans = response.scans.into_iter()
                        .filter(|x| x.folder_id == id)
                        .collect();

        Ok(structs::ScanListResponse {
            folders,
            scans,
            timestamp: response.timestamp,
        })
    }

    fn export_scan(&self, scan_id: u64) -> Result<structs::ExportToken> {
        let mut x = HashMap::new();
        x.insert("format", "nessus");

        let mut token: structs::ExportToken = self.post(&format!("/scans/{}/export", scan_id), x)?;
        token.scan_id = Some(scan_id);
        Ok(token)
    }

    fn export_status(&self, scan_id: u64, file_id: u64) -> Result<structs::ExportStatus> {
        self.get(&format!("/scans/{}/export/{}/status", scan_id, file_id))
    }

    fn download_export_raw(&self, scan_id: u64, file_id: u64) -> Result<String> {
        let response = self.raw_get(&format!("/scans/{}/export/{}/download", scan_id, file_id))?;
        let content = response.content();
        let string = content.as_ref_string().to_owned();
        Ok(string)
    }

    fn download_export(&self, scan_id: u64, file_id: u64) -> Result<parser::NessusClientDatav2> {
        let response = self.download_export_raw(scan_id, file_id)?;
        let report = parser::parse(response)?;
        Ok(report)
    }
}

pub trait Waitable<V: VulnScanner> {
    fn is_pending(&self, client: &V) -> Result<bool>;

    fn wait(&self, client: &V, interval: Duration, mut max_attempts: Option<u64>) -> Result<()> {
        loop {
            if ! self.is_pending(&client)? {
                return Ok(());
            }

            if let Some(ref mut left) = max_attempts {
                if *left <= 0 {
                    break;
                }

                *left -= 1;
            }

            sleep(interval);
        }

        return Err(Error::from(ErrorKind::WaitTimeout));
    }
}


#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
    }
}