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
use crate::util::unwrap_try_into;
use chrono::prelude::*;
use derive_builder::Builder;
use failure::Fail;
use reqwest::header::HeaderValue;
use serde::Deserialize;
use std::convert::TryInto;
use crate::datetime::days_ago;
use crate::{GenericResult, SampleHash};
pub struct AlienVaultOTXClient {
apikey: String,
}
impl Default for AlienVaultOTXClient {
fn default() -> Self {
AlienVaultOTXClient {
apikey: std::env::var("OTX_APIKEY")
.expect("please set AlienVault OTX API key to environment var $OTX_APIKEY"),
}
}
}
#[derive(Debug, Fail)]
pub enum AlienVaultOTXError {
#[fail(display = "invalid setting")]
InvalidSetting(String),
#[fail(display = "request failed")]
RequestFailed,
}
#[derive(Debug)]
pub enum QueryType {
General,
Analysis,
}
impl std::fmt::Display for QueryType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
QueryType::General => write!(f, "general"),
QueryType::Analysis => write!(f, "analysis"),
}
}
}
impl AlienVaultOTXClient {
pub fn new(apikey: String) -> Self {
AlienVaultOTXClient { apikey }
}
pub fn pulses_from(&self, datetime: DateTime<Utc>) -> GenericResult<Vec<Pulse>> {
Ok(PulsesBuilder::default()
.api_key(self.apikey.clone())
.modified_since(datetime)
.build()
.map_err(AlienVaultOTXError::InvalidSetting)?
.get_all())
}
pub fn pulses_for(&self, days: i64) -> GenericResult<Vec<Pulse>> {
self.pulses_from(days_ago(days))
}
fn indicator_url(&self, hash: impl AsRef<str>, section: QueryType) -> String {
format!(
"https://otx.alienvault.com/api/v1/indicators/file/{}/{}",
hash.as_ref(),
section
)
}
fn make_get_request(&self, url: impl AsRef<str>) -> reqwest::RequestBuilder {
reqwest::Client::new()
.get(url.as_ref())
.header("X-OTX-API-KEY", self.apikey.as_str())
}
pub fn get_raw_json(
&self,
hash: impl TryInto<SampleHash>,
section: QueryType,
) -> GenericResult<String> {
let hash = unwrap_try_into(hash)?;
let mut res = self
.make_get_request(self.indicator_url(hash, section))
.send()?;
if !res.status().is_success() {
return Err(AlienVaultOTXError::RequestFailed.into());
}
Ok(res.text()?)
}
pub fn query<T>(&self, hash: impl TryInto<SampleHash>, section: QueryType) -> GenericResult<T>
where
T: serde::de::DeserializeOwned,
{
let hash = unwrap_try_into(hash)?;
let mut res = self
.make_get_request(self.indicator_url(hash, section))
.send()?;
if !res.status().is_success() {
return Err(AlienVaultOTXError::RequestFailed.into());
}
Ok(res.json()?)
}
}
#[derive(Builder, Debug)]
pub struct Pulses {
#[builder(
default = "std::env::var(\"OTX_APIKEY\").map_err(|_err| \"could not get OTX APIKEY\".to_owned())?"
)]
api_key: String,
#[builder(default = "50")]
limit: u32,
#[builder(default = "1")]
page: u32,
#[builder(default = "days_ago(7)")]
modified_since: DateTime<Utc>,
#[builder(default = "false")]
has_done: bool,
}
#[derive(Debug, Deserialize)]
pub enum IndicatorType {
IPv4,
IPv6,
#[serde(rename = "domain")]
Domain,
#[serde(rename = "hostname")]
Hostname,
#[serde(rename = "email")]
Email,
URL,
URI,
#[serde(rename = "FileHash-MD5")]
MD5,
#[serde(rename = "FileHash-SHA1")]
SHA1,
#[serde(rename = "FileHash-SHA256")]
SHA256,
#[serde(rename = "FileHash-PEHASH")]
PEHash,
#[serde(rename = "FileHash-IMPHASH")]
IMPHash,
CIDR,
FilePath,
Mutex,
CVE,
YARA,
#[serde(other)]
Unknown,
}
#[derive(Debug, Deserialize)]
pub struct Indicator {
pub id: i64,
pub indicator: String,
pub description: Option<String>,
#[serde(rename = "type")]
pub _type: IndicatorType,
}
#[derive(Debug, Deserialize)]
pub struct Pulse {
pub id: String,
pub name: String,
pub author_name: String,
pub description: Option<String>,
pub created: Option<String>,
pub modified: String,
pub references: Option<Vec<String>>,
pub tags: Option<Vec<String>>,
pub targeted_countries: Vec<String>,
pub indicators: Option<Vec<Indicator>>,
pub revision: Option<i64>,
pub adversary: Option<String>,
}
impl Into<Vec<SampleHash>> for Pulse {
fn into(self) -> Vec<SampleHash> {
self.indicators
.unwrap_or_default()
.into_iter()
.map(|x| SampleHash::new(x.indicator))
.flat_map(|x| x)
.collect()
}
}
pub fn hashes_in(pulses: Vec<Pulse>) -> Vec<SampleHash> {
pulses
.into_iter()
.map(|x| x.into())
.flat_map(|x: Vec<SampleHash>| x)
.collect()
}
#[derive(Debug, Deserialize)]
pub struct Response {
pub count: i64,
pub next: Option<String>,
pub previous: Option<String>,
pub results: Vec<Pulse>,
}
impl Pulses {
fn request(&mut self) -> GenericResult<Response> {
let res: Response = reqwest::Client::new()
.get(format!(
"https://otx.alienvault.com/api/v1/pulses/subscribed?limit={}&page={}&modified_since={}",
self.limit,
self.page,
self.modified_since.to_rfc3339()).as_str()
)
.header("X-OTX-API-KEY", HeaderValue::from_str(self.api_key.as_str()).unwrap())
.send()?
.json()?;
self.page += 1;
if res.next == None {
self.has_done = true;
}
Ok(res)
}
pub fn get_all(self) -> Vec<Pulse> {
self.flat_map(|x| x).collect()
}
pub fn get_all_hashes(self) -> Vec<SampleHash> {
hashes_in(self.get_all())
}
}
impl Iterator for Pulses {
type Item = Vec<Pulse>;
fn next(&mut self) -> Option<Self::Item> {
if self.has_done {
None
} else {
if self.page != 1 {
std::thread::sleep(std::time::Duration::from_secs(1));
}
self.request().and_then(|x| Ok(x.results)).ok()
}
}
}