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
#![allow(non_snake_case)]
use std::error::Error;
use std::io::{Write,Read};
use std::collections::HashMap;
pub use serde_json::Value;
mod logging;
pub use logging::*;
mod report;
pub use report::*;
type RunFn = fn(&MsfParamWrapper)->Result<Option<String>,Box<dyn Error>>;
type CheckFn = fn(&MsfParamWrapper)->Result<(),Box<dyn Error>>;
#[derive(serde_derive::Deserialize,serde_derive::Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Reference {
pub Type: String,
pub Ref: String,
}
#[derive(serde_derive::Deserialize,serde_derive::Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ModuleOption {
pub Type: String,
pub description: String,
pub required: bool,
pub default: String,
}
#[derive(serde_derive::Deserialize,serde_derive::Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Metadata {
pub name: String,
pub description: String,
pub authors: Vec<String>,
pub date: String,
pub Type: String,
pub privileged: bool,
pub options: HashMap<String,ModuleOption>,
pub references: Vec<Reference>,
}
#[derive(serde_derive::Deserialize,serde_derive::Serialize, Debug)]
struct RunResponse {
jsonrpc: String,
id: String,
result: RunResult,
}
#[derive(serde_derive::Deserialize,serde_derive::Serialize, Debug)]
#[serde(rename_all = "camelCase")]
struct RunResult {
Message: String,
Return: String,
}
#[derive(serde_derive::Deserialize,serde_derive::Serialize, Debug)]
pub struct MetadataResponse {
pub jsonrpc: String,
pub id: String,
pub result: Metadata,
}
#[derive(serde_derive::Deserialize,serde_derive::Serialize, Debug)]
struct CheckError {
code: isize,
message: String,
}
#[derive(serde_derive::Deserialize,serde_derive::Serialize, Debug)]
struct CheckErrorResponse {
jsonrpc: String,
id: String,
error: CheckError,
}
fn send_check_unsupported(
id: &str,
) -> Result<(),Box<dyn Error>> {
let check_error_response = CheckErrorResponse {
jsonrpc: "2.0".to_string(),
id: id.to_string(),
error: CheckError {
code: -32601,
message: "Soft checks are not supported".to_string(),
}
};
let check_error_response_str= serde_json::to_string(&check_error_response)?;
rpc_send(&check_error_response_str);
Ok(())
}
pub fn send_metadata(
request: Request,
metadata: Metadata
) -> Result<(),Box<dyn Error>> {
let metadata_response = MetadataResponse {
jsonrpc: request.jsonrpc,
id: request.id,
result: metadata,
};
let metadata_resp_string = serde_json::to_string(&metadata_response)?;
rpc_send(&metadata_resp_string);
Ok(())
}
fn rpc_send(string: &str){
print!("{}",string);
std::io::stdout().flush().ok().expect("couldn't flush stdout");
}
fn rpc_read(
) -> Result<Request,Box<dyn Error>> {
let mut stdin_h = std::io::stdin();
let mut buf = [0u8; 10000];
let num = stdin_h.read(&mut buf)?;
let req_dec = std::str::from_utf8(&buf)?;
let mut data = req_dec.to_string();
data.truncate(num);
let request: Request= serde_json::from_str(&data)?;
Ok(request)
}
fn send_complete(
id: &str,
result: Option<String>,
)-> Result<(),Box<dyn Error>> {
let result_str = match result {
Some(v) => v,
None => "".to_string(),
};
let run_result = RunResult {
Message: "Module complete".to_string(),
Return: result_str,
};
let run_response = RunResponse {
jsonrpc: "2.0".to_string(),
id: id.to_string(),
result: run_result,
};
let run_response_string = serde_json::to_string(&run_response)?;
rpc_send(&run_response_string);
Ok(())
}
pub fn init(
metadata: Metadata,
run_fn: RunFn,
check_fn: Option<CheckFn>,
) -> Result<(),Box<dyn Error>> {
let request = rpc_read()?;
match &request.method.as_str() {
&"describe" => {
send_metadata(request,metadata)?;
}
&"run" => {
let msfparams: MsfParamWrapper = MsfParamWrapper::new(request.params);
let response = run_fn(&msfparams)?;
send_complete(&request.id, response)?;
}
&"soft_check" => {
match check_fn {
_ => send_check_unsupported(&request.id)?,
}
}
_ => eprintln!("ERROR: unknown method received"),
}
Ok(())
}