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
use std::fs;
use std::io;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::io::prelude::*;
use serde::{Serialize, Deserialize};
use colored::*;
use term_table::{
row::Row,
table_cell::{Alignment, TableCell},
};
use term_table::{Table, TableStyle};
#[cfg(target_pointer_width = "64")]
use goblin::elf64 as elf;
#[cfg(target_pointer_width = "32")]
use goblin::elf32 as elf;
use goblin::Object;
use goblin::elf::dynamic::{tag_to_str, Dyn};
use goblin::elf::{header, program_header, ProgramHeader};
#[derive(Serialize, Deserialize)]
pub enum Relro {
FullRelro,
PartialRelro,
NoRelro
}
pub enum Format {
Normal,
Json,
Toml
}
#[derive(Default, Serialize, Deserialize)]
pub struct BinInfo {
pub machine: String,
pub file_class: String,
pub bin_type: String,
pub entry_point: u64
}
#[derive(Serialize, Deserialize)]
pub struct Binsec {
pub exec_stack: bool,
pub stack_canary: bool,
pub pie: bool,
pub relro: Relro,
}
impl Default for Binsec {
fn default() -> Self {
Self {
exec_stack: false,
stack_canary: false,
pie: false,
relro: Relro::NoRelro
}
}
}
pub struct Detector {
path: PathBuf,
pub basic_info: Option<BinInfo>,
pub features: Binsec
}
impl Default for Detector {
fn default() -> Self {
Self {
path: PathBuf::new(),
basic_info: None,
features: Binsec::default()
}
}
}
impl Detector {
pub fn new(_binary: String) -> io::Result<Self> {
let _path = Path::new(_binary.as_str());
let path = fs::canonicalize(_path.to_path_buf())?;
Ok(Self { path, ..Detector::default() })
}
#[inline]
fn basic_info(header: header::Header) -> BinInfo {
let file_class: &str = match header.e_ident[4] {
1 => "ELF32",
2 => "ELF64",
_ => "unknown"
};
BinInfo {
machine: header::machine_to_str(header.e_machine).to_string(),
file_class: file_class.to_string(),
bin_type: header::et_to_str(header.e_type).to_string(),
entry_point: header.e_entry,
}
}
pub fn detect(&mut self, basic_info: bool) -> io::Result<&Self> {
let mut fd = File::open(self.path.clone())?;
let buffer = { let mut v = Vec::new(); fd.read_to_end(&mut v)?; v};
let elf = match Object::parse(&buffer).unwrap() {
Object::Elf(elf) => elf,
_ => { panic!("unsupported binary format"); }
};
if basic_info {
let header: header::Header = elf.header;
self.basic_info = Some(Detector::basic_info(header));
}
let stack_header: Option<ProgramHeader> = elf.program_headers
.iter().find(|ph| program_header::pt_to_str(ph.p_type) == "PT_GNU_STACK")
.cloned();
if let Some(sh) = stack_header {
if sh.p_flags == 6 {
self.features.exec_stack = true
}
}
let relro_header: Option<ProgramHeader> = elf.program_headers
.iter().find(|ph| program_header::pt_to_str(ph.p_type) == "PT_GNU_RELRO")
.cloned();
if let Some(rh) = relro_header {
if rh.p_flags == 4 {
if let Some(segs) = &elf.dynamic {
let dyn_seg: Option<Dyn> = segs.dyns
.iter()
.find(|tag| tag_to_str(tag.d_tag) == "DT_BIND_NOW")
.cloned();
if let None = dyn_seg {
self.features.relro = Relro::PartialRelro;
} else {
self.features.relro = Relro::FullRelro;
}
}
}
}
let strtab = elf.strtab.to_vec().unwrap();
let str_sym: Option<_> = strtab
.iter()
.find(|sym| sym.contains("__stack_chk_fail"))
.cloned();
if let Some(_) = str_sym {
self.features.stack_canary = true;
}
let e_type = elf.header.e_type;
match e_type {
3 => { self.features.pie = true; },
2 | _ => { self.features.pie = false; }
}
Ok(self)
}
pub fn output(&self, format: &Format) -> () {
if let Some(elf_info) = &self.basic_info {
match format {
Format::Normal => {
let mut basic_table = Table::new();
basic_table.max_column_width = 60;
basic_table.style = TableStyle::blank();
basic_table.add_row(Row::new(vec![
TableCell::new_with_alignment("BASIC BINARY INFORMATION".underline(), 2, Alignment::Center)
]));
basic_table.add_row(Row::new(vec![
TableCell::new("Binary Path:"),
TableCell::new_with_alignment(self.path.to_str().unwrap(), 1, Alignment::Right)
]));
basic_table.add_row(Row::new(vec![
TableCell::new("Machine:"),
TableCell::new_with_alignment(&elf_info.machine, 1, Alignment::Right)
]));
basic_table.add_row(Row::new(vec![
TableCell::new("Class:"),
TableCell::new_with_alignment(&elf_info.file_class, 1, Alignment::Right)
]));
basic_table.add_row(Row::new(vec![
TableCell::new("Binary Type:"),
TableCell::new_with_alignment(&elf_info.bin_type, 1, Alignment::Right)
]));
basic_table.add_row(Row::new(vec![
TableCell::new("Entry Point:"),
TableCell::new_with_alignment(&format_args!("0x{:x}", elf_info.entry_point), 1, Alignment::Right)
]));
println!("{}", basic_table.render());
},
Format::Json => {
println!("{}", serde_json::to_string_pretty(&self.basic_info).unwrap());
},
Format::Toml => {
println!("{}", toml::to_string(&self.features).unwrap());
}
}
}
match format {
Format::Normal => {
let mut table = Table::new();
table.max_column_width = 60;
table.style = TableStyle::blank();
table.add_row(Row::new(vec![
TableCell::new_with_alignment("KERNEL SECURITY FEATURES".underline(), 2, Alignment::Center)
]));
let mut nx_row = vec![TableCell::new("NX bit")];
if self.features.exec_stack {
nx_row.push(TableCell::new_with_alignment("Enabled".green(), 1, Alignment::Right));
} else {
nx_row.push(TableCell::new_with_alignment("Disabled".red(), 1, Alignment::Right));
}
table.add_row(Row::new(nx_row));
let mut relro_row = vec![TableCell::new("RELRO")];
match self.features.relro {
Relro::PartialRelro => {
relro_row.push(TableCell::new_with_alignment("Partial RELRO enabled".yellow(), 1, Alignment::Right));
},
Relro::FullRelro => {
relro_row.push(TableCell::new_with_alignment("Full RELRO enabled".green(), 1, Alignment::Right));
},
Relro::NoRelro => {
relro_row.push(TableCell::new_with_alignment("No RELRO enabled".red(), 1, Alignment::Right));
}
}
table.add_row(Row::new(relro_row));
let mut sc_row = vec![TableCell::new("Stack Canary")];
if self.features.stack_canary {
sc_row.push(TableCell::new_with_alignment("Enabled".green(), 1, Alignment::Right));
} else {
sc_row.push(TableCell::new_with_alignment("Not Enabled".red(), 1, Alignment::Right));
}
table.add_row(Row::new(sc_row));
let mut pie_row = vec![TableCell::new("PIE")];
if self.features.pie {
pie_row.push(TableCell::new_with_alignment("PIE enabled (PIE executable)".green(), 1, Alignment::Right));
} else {
pie_row.push(TableCell::new_with_alignment("PIE disabled (executable)".red(), 1, Alignment::Right));
}
table.add_row(Row::new(pie_row));
println!("{}", table.render());
},
Format::Json => {
println!("{}", serde_json::to_string_pretty(&self.features).unwrap());
},
Format::Toml => {
println!("{}", toml::to_string(&self.features).unwrap());
}
}
}
}