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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
use crate::container::network::NetworkTargetId;
use crate::container::{
ContainerSearchItem, ContainerSearchItemKind, ContainerSearchQuery, ContainerSearchResponse,
SourceId, SourcePath, SourceTag,
};
use reqwest::blocking::Client;
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION};
use reqwest::StatusCode;
use serde::Deserialize;
use serde_json::json;
use std::collections::HashMap;
use std::str::FromStr;
use uuid::Uuid;
use warp::chunk::ChunkKind;
use warp::r#type::guid::TypeGUID;
use warp::r#type::{ComputedType, Type};
use warp::signature::function::{Function, FunctionGUID};
use warp::target::Target;
use warp::WarpFile;
/// Responsible for sending and receiving data from the server.
///
/// NOTE: **All requests are blocking**.
#[derive(Clone, Debug)]
pub struct NetworkClient {
client: Client,
pub server_url: String,
}
impl NetworkClient {
pub fn new(
server_url: String,
server_token: Option<String>,
https_proxy: Option<String>,
) -> reqwest::Result<Self> {
let version_info = binaryninja::version_info();
// TODO: IIRC we had a user agent format already for some other thing.
let client_agent = format!(
"Binary Ninja/{}.{}.{}",
version_info.major, version_info.minor, version_info.build
);
// TODO: This might want to be kept for the request header?
let mut headers = HeaderMap::new();
if let Some(token) = &server_token {
headers.insert(
AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {}", token)).unwrap(),
);
}
// TODO: Configurable timeout?
let mut client_builder = Client::builder()
.connect_timeout(std::time::Duration::from_secs(10))
.default_headers(headers)
.user_agent(client_agent);
if let Some(https_proxy) = https_proxy {
client_builder = client_builder.proxy(reqwest::Proxy::all(&https_proxy)?);
}
Ok(Self {
client: client_builder.build()?,
server_url,
})
}
/// Check to see the status of the server.
///
/// This is useful if you want to fail early and prevent constructing a network container to a
/// server that is unresponsive.
///
/// Route: `api/v1/status`
pub fn status(&self) -> reqwest::Result<StatusCode> {
let status_url = format!("{}/api/v1/status", self.server_url);
let resp = self.client.get(&status_url).send()?;
Ok(resp.status())
}
/// Query the logged in user.
///
/// NOTE: **THIS IS BLOCKING**
///
/// Route: `api/v1/users/me` (TODO: Comment about the query)
pub fn current_user(&self) -> reqwest::Result<(i32, String)> {
let current_user_url = format!("{}/api/v1/users/me", self.server_url);
#[derive(Deserialize)]
struct CurrentUser {
username: String,
id: i32,
}
let resp = self
.client
.get(¤t_user_url)
.send()?
.error_for_status()?;
let user: CurrentUser = resp.json()?;
Ok((user.id, user.username))
}
/// Query the logged in user.
///
/// NOTE: **THIS IS BLOCKING**
///
/// Route: `api/v1/users/me` (TODO: Comment about the query)
pub fn source_name(&self, id: SourceId) -> reqwest::Result<String> {
let source_url = format!("{}/api/v1/sources/{}", self.server_url, id);
#[derive(Deserialize)]
struct Source {
name: String,
}
let resp = self.client.get(&source_url).send()?.error_for_status()?;
let src: Source = resp.json()?;
Ok(src.name)
}
/// Create a new source with the given name.
///
/// The current user will be added to the source.
///
/// NOTE: You must be logged in to create a source.
///
/// NOTE: **THIS IS BLOCKING**
///
/// Route: `api/v1/sources/`
pub fn create_source(&self, name: &str) -> reqwest::Result<SourceId> {
let source_url = format!("{}/api/v1/sources", self.server_url);
let body = json!({
"name": name,
// Passing nothing here will add the current user to the source.
"user_ids": []
});
#[derive(Deserialize)]
struct CreateSourceResponse {
id: Uuid,
}
let resp = self
.client
.post(&source_url)
.json(&body)
.send()?
.error_for_status()?;
let parsed: CreateSourceResponse = resp.json()?;
Ok(SourceId(parsed.id))
}
/// Query the [`SourceId`]s for the given user.
///
/// NOTE: **THIS IS BLOCKING**
///
/// Route: `api/v1/sources/query` (TODO: Comment about the query)
pub fn query_sources(&self, user_id: Option<i32>) -> reqwest::Result<Vec<SourceId>> {
let sources_url = format!("{}/api/v1/sources/query", self.server_url);
#[derive(Deserialize)]
struct SourceItem {
id: Uuid,
}
#[derive(Deserialize)]
struct SourcesQueryResponse {
items: Vec<SourceItem>,
}
let mut query = HashMap::new();
if let Some(user_id) = user_id {
query.insert("user_id", user_id);
}
let query_str = json!(query).to_string();
let resp = self
.client
.post(&sources_url)
.body(query_str)
.header("Content-Type", "application/json")
.send()?
.error_for_status()?;
let parsed: SourcesQueryResponse = resp.json()?;
Ok(parsed.items.into_iter().map(|it| SourceId(it.id)).collect())
}
/// Query the [`NetworkTargetId`] for the given [`Target`].
///
/// NOTE: **THIS IS BLOCKING**
///
/// Route: `api/v1/targets/query` (TODO: Comment about the query)
pub fn query_target_id(&self, target: &Target) -> Option<NetworkTargetId> {
let query_target_url = format!("{}/api/v1/targets/query", self.server_url);
let mut query = HashMap::new();
if let Some(platform) = &target.platform {
query.insert("platform", platform);
}
if let Some(architecture) = &target.architecture {
query.insert("arch", architecture);
}
let query_str = json!(query).to_string();
#[derive(Deserialize)]
struct TargetQueryResponse {
id: NetworkTargetId,
}
// NOTE: This is blocking.
let response = self
.client
.post(query_target_url)
.body(query_str)
.header("Content-Type", "application/json")
.send()
.ok()?;
// Assuming the first response is the one we want.
// TODO: Handle multiple responses, or error out.
let json_response: Vec<TargetQueryResponse> = response.json().ok()?;
let first_response = json_response.first()?;
Some(first_response.id)
}
fn query_functions_body(
target: Option<NetworkTargetId>,
source: Option<SourceId>,
source_tags: &[SourceTag],
guids: &[FunctionGUID],
) -> serde_json::Value {
let guids_str: Vec<String> = guids.iter().map(|g| g.to_string()).collect();
// TODO: The limit here needs to be somewhat flexible. But 1000 will do for now.
let mut body = json!({
"format": "flatbuffer",
"guids": guids_str,
"limit": 1000
});
if let Some(target_id) = target {
body["target_id"] = json!(target_id);
}
if let Some(source_id) = source {
body["source_id"] = json!(source_id.to_string());
}
if !source_tags.is_empty() {
body["source_tags"] = json!(source_tags);
}
body
}
/// Query the functions, returning the warp file response containing the entries.
///
/// NOTE: **THIS IS BLOCKING**
///
/// Route: `api/v1/functions/query` (TODO: Comment about the query)
pub fn query_functions(
&self,
target: Option<NetworkTargetId>,
source: Option<SourceId>,
guids: &[FunctionGUID],
) -> Option<WarpFile<'static>> {
let query_functions_url = format!("{}/api/v1/functions/query", self.server_url);
// TODO: Allow for source tags? We really only need this in query_functions_source as that
// TODO: is what prevents a undesired source from being "known" to the container.
let payload = Self::query_functions_body(target, source, &[], guids);
// Make the POST request
let response = self
.client
.post(&query_functions_url)
.json(&payload)
.send()
.ok()?;
if !response.status().is_success() {
log::error!("Failed to query functions: {}", response.status());
return None;
}
// Get response bytes and convert to WarpFile
let bytes = response.bytes().ok()?;
WarpFile::from_owned_bytes(bytes.to_vec())
}
/// Query the functions, returning the sources and the corresponding function guids.
///
/// NOTE: **THIS IS BLOCKING**
///
/// Route: `api/v1/functions/query/source` (TODO: Comment about the query)
pub fn query_functions_source(
&self,
target: Option<NetworkTargetId>,
tags: &[SourceTag],
guids: &[FunctionGUID],
) -> Option<HashMap<SourceId, Vec<FunctionGUID>>> {
let query_functions_source_url =
format!("{}/api/v1/functions/query/source", self.server_url);
let payload = Self::query_functions_body(target, None, tags, guids);
// Make the POST request
let response = self
.client
.post(&query_functions_source_url)
.json(&payload)
.send()
.ok()?;
if !response.status().is_success() {
log::error!("Failed to query functions source: {}", response.status());
return None;
}
// Mapping of source id to function guids
let json_response: HashMap<String, Vec<String>> = response.json().ok()?;
let mapped_function_guids = json_response
.into_iter()
.filter_map(|(source_str, guid_strs)| {
let source_id = SourceId::from_str(&source_str).ok()?;
let guids = guid_strs
.into_iter()
.filter_map(|guid_str| FunctionGUID::from_str(&guid_str).ok())
.collect();
Some((source_id, guids))
})
.collect();
Some(mapped_function_guids)
}
/// Pushes the file to the remote source.
///
/// NOTE: **THIS IS BLOCKING**
///
/// Route: `api/v1/files/{source}`
pub fn push_file(&self, source_id: SourceId, file: &WarpFile, name: &str) -> bool {
let push_file_url = format!("{}/api/v1/files", self.server_url);
// Convert WarpFile to bytes
let file_bytes = file.to_bytes();
let Ok(file_part) = reqwest::blocking::multipart::Part::bytes(file_bytes)
.file_name("data.warp")
.mime_str("application/octet-stream")
else {
log::error!("Failed to create file part");
return false;
};
let form = reqwest::blocking::multipart::Form::new()
.part("file", file_part)
.text("name", name.to_string())
.text("source", source_id.to_string());
// Send the request
match self.client.post(&push_file_url).multipart(form).send() {
Ok(response) => {
if response.status().is_success() {
true
} else {
log::error!("Failed to push file: {}", response.status());
false
}
}
Err(e) => {
log::error!("Failed to send push request: {}", e);
false
}
}
}
pub fn function_data(&self, id: i32) -> Option<Function> {
let function_data_url = format!("{}/api/v1/functions/{}/data", self.server_url, id);
let response = self.client.get(&function_data_url).send().ok()?;
if !response.status().is_success() {
log::error!(
"Failed to fetch function data for {}: {}",
id,
response.status()
);
return None;
}
let bytes = response.bytes().ok()?;
Function::from_bytes(bytes.as_ref())
}
pub fn function_datas(&self, ids: &[i32]) -> Option<Vec<Function>> {
if ids.is_empty() {
return Some(Vec::new());
}
let function_data_url = format!("{}/api/v1/functions/data", self.server_url);
let body = json!({
"ids": ids,
});
let response = self
.client
.post(&function_data_url)
.json(&body)
.send()
.ok()?;
if !response.status().is_success() {
log::error!("Failed to fetch function data: {}", response.status());
return None;
}
let bytes = response.bytes().ok()?;
let file = WarpFile::from_bytes(bytes.as_ref())?;
let mut functions = Vec::with_capacity(ids.len());
for chunk in file.chunks {
let ChunkKind::Signature(sc) = chunk.kind else {
continue;
};
functions.extend(sc.functions());
}
Some(functions)
}
pub fn type_data(&self, guid: TypeGUID) -> Option<Type> {
let type_data_url = format!("{}/api/v1/types/{}/data", self.server_url, guid.to_string());
let response = self.client.get(&type_data_url).send().ok()?;
if !response.status().is_success() {
log::error!(
"Failed to fetch type data for {}: {}",
guid.to_string(),
response.status()
);
return None;
}
let bytes = response.bytes().ok()?;
Type::from_bytes(bytes.as_ref())
}
pub fn type_datas(&self, guids: &[TypeGUID]) -> Option<Vec<ComputedType>> {
if guids.is_empty() {
return Some(Vec::new());
}
let type_data_url = format!("{}/api/v1/types/data", self.server_url);
let body = json!({
"ids": guids.iter().map(|g| g.to_string()).collect::<Vec<_>>(),
});
let response = self.client.post(&type_data_url).json(&body).send().ok()?;
if !response.status().is_success() {
log::error!("Failed to fetch type data: {}", response.status());
return None;
}
let bytes = response.bytes().ok()?;
let file = WarpFile::from_bytes(bytes.as_ref())?;
let mut types = Vec::with_capacity(guids.len());
for chunk in file.chunks {
let ChunkKind::Type(tc) = chunk.kind else {
continue;
};
types.extend(tc.types());
}
Some(types)
}
pub fn search(&self, query: &ContainerSearchQuery) -> Option<ContainerSearchResponse> {
let search_url = format!("{}/api/v1/search", self.server_url);
#[derive(serde::Serialize)]
struct SearchRequest<'a> {
#[serde(rename = "q")]
q: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
offset: Option<usize>,
#[serde(rename = "source_id", skip_serializing_if = "Option::is_none")]
source_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
source_tags: Option<Vec<SourceTag>>,
#[serde(skip_serializing_if = "Option::is_none")]
retrieve_data: Option<bool>,
}
#[derive(serde::Deserialize)]
struct SearchResponse {
items: Vec<SearchItem>,
offset: usize,
total: usize,
}
#[derive(serde::Deserialize)]
struct SearchItem {
id: String,
kind: String,
#[serde(default)]
name: Option<String>,
#[serde(default)]
source_id: Option<Uuid>,
#[serde(default)]
data: Option<Vec<u8>>,
}
let source_id_str = query.source.map(|s| s.to_string());
let request = SearchRequest {
q: &query.query,
limit: query.limit,
offset: query.offset,
source_id: source_id_str,
source_tags: match query.tags.is_empty() {
true => None,
false => Some(query.tags.clone()),
},
// This must be passed to retrieve the function and type data.
retrieve_data: Some(true),
};
let resp = match self.client.get(search_url).query(&request).send() {
Ok(r) => r,
Err(err) => {
log::error!("Failed to send search request: {}", err);
return None;
}
};
let Ok(parsed) = resp.json::<SearchResponse>() else {
log::error!("Failed to parse search response");
return None;
};
// TODO: This is quite scuffed, but it works for now. (Mostly just that it looks bad and queries a lot)
// TODO: Here I think would be a good place to sort it so sources always come first.
// TODO: Users searching will want to get to the source first, likely to whitelist or blacklist.
let mut items = Vec::with_capacity(parsed.items.len());
for item in parsed.items {
let Some(source_uuid) = item.source_id else {
// Currently not interested in items without a source id.
// Things like symbols do not have a source id.
continue;
};
let kind = match item.kind.as_str() {
"function" => {
let Some(data) = &item.data else {
log::warn!(
"Function item {} has no data from network, skipping...",
item.id
);
continue;
};
let Some(func) = Function::from_bytes(&data) else {
log::warn!(
"Function item {} has invalid data from network, skipping...",
item.id
);
continue;
};
ContainerSearchItemKind::Function(func)
}
"source" => ContainerSearchItemKind::Source {
path: match item.name {
None => {
log::warn!("Source item {} has no name", item.id);
continue;
}
Some(name) => SourcePath(format!("{}/{}", self.server_url, name).into()),
},
id: SourceId(source_uuid),
},
"type" => {
let Some(data) = &item.data else {
log::warn!(
"Type item {} has no data from network, skipping...",
item.id
);
continue;
};
let Some(ty) = Type::from_bytes(&data) else {
log::warn!(
"Type item {} has invalid data from network, skipping...",
item.id
);
continue;
};
ContainerSearchItemKind::Type(ty)
}
_ => continue,
};
items.push(ContainerSearchItem {
source: SourceId(source_uuid),
kind,
});
}
Some(ContainerSearchResponse {
items,
total: parsed.total,
offset: parsed.offset,
})
}
}
|