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
//! Mach-O format parser for macOS/iOS binaries
use crate::{
types::{
Architecture, BinaryFormat as Format, BinaryMetadata, Endianness, Export, Import, Section,
SectionPermissions, SectionType, SecurityFeatures, Symbol,
},
BinaryError, BinaryFormatParser, BinaryFormatTrait, Result,
};
use goblin::mach::{Mach, MachO};
/// Mach-O format parser
pub struct MachOParser;
impl BinaryFormatParser for MachOParser {
fn parse(data: &[u8]) -> Result<Box<dyn BinaryFormatTrait>> {
let mach = Mach::parse(data)?;
match mach {
Mach::Binary(macho) => Ok(Box::new(MachOBinary::new(macho, data)?)),
Mach::Fat(_) => Err(BinaryError::unsupported_format(
"Fat binaries not yet supported",
)),
}
}
fn can_parse(data: &[u8]) -> bool {
if data.len() < 4 {
return false;
}
let magic = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);
matches!(
magic,
goblin::mach::header::MH_MAGIC
| goblin::mach::header::MH_CIGAM
| goblin::mach::header::MH_MAGIC_64
| goblin::mach::header::MH_CIGAM_64
| goblin::mach::fat::FAT_MAGIC
| goblin::mach::fat::FAT_CIGAM
)
}
}
/// Parsed Mach-O binary
pub struct MachOBinary {
#[allow(dead_code)]
macho: MachO<'static>,
#[allow(dead_code)]
data: Vec<u8>,
metadata: BinaryMetadata,
sections: Vec<Section>,
symbols: Vec<Symbol>,
imports: Vec<Import>,
exports: Vec<Export>,
}
impl MachOBinary {
fn new(macho: MachO<'_>, data: &[u8]) -> Result<Self> {
let data = data.to_vec();
// Convert architecture
let architecture = match macho.header.cputype() {
goblin::mach::constants::cputype::CPU_TYPE_X86 => Architecture::X86,
goblin::mach::constants::cputype::CPU_TYPE_X86_64 => Architecture::X86_64,
goblin::mach::constants::cputype::CPU_TYPE_ARM => Architecture::Arm,
goblin::mach::constants::cputype::CPU_TYPE_ARM64 => Architecture::Arm64,
goblin::mach::constants::cputype::CPU_TYPE_POWERPC => Architecture::PowerPC,
goblin::mach::constants::cputype::CPU_TYPE_POWERPC64 => Architecture::PowerPC64,
_ => Architecture::Unknown,
};
// Determine endianness from the original data parsing
// Goblin normalizes magic numbers, so we need to check the raw bytes
let endian = if data.len() >= 4 {
let raw_magic_be = u32::from_be_bytes([data[0], data[1], data[2], data[3]]);
// Check for big endian magic (raw bytes match canonical form)
if raw_magic_be == goblin::mach::header::MH_MAGIC
|| raw_magic_be == goblin::mach::header::MH_MAGIC_64
{
Endianness::Big
} else {
// All other cases (including CIGAM variants) are little endian
Endianness::Little
}
} else {
Endianness::Little // Default for malformed data
};
// Analyze security features
let security_features = analyze_security_features(&macho);
let metadata = BinaryMetadata {
size: data.len(),
format: Format::MachO,
architecture,
entry_point: find_entry_point(&macho),
base_address: None, // Mach-O uses ASLR, no fixed base
timestamp: None, // Not readily available in Mach-O
compiler_info: extract_compiler_info(&macho),
endian,
security_features,
};
// Parse sections
let sections = parse_sections(&macho, &data)?;
// Parse symbols
let symbols = parse_symbols(&macho)?;
// Parse imports and exports
let (imports, exports) = parse_imports_exports(&macho)?;
// Handle lifetime issues with MachO struct
let macho_owned = unsafe { std::mem::transmute::<MachO<'_>, MachO<'static>>(macho) };
Ok(Self {
macho: macho_owned,
data,
metadata,
sections,
symbols,
imports,
exports,
})
}
}
impl BinaryFormatTrait for MachOBinary {
fn format_type(&self) -> Format {
Format::MachO
}
fn architecture(&self) -> Architecture {
self.metadata.architecture
}
fn entry_point(&self) -> Option<u64> {
self.metadata.entry_point
}
fn sections(&self) -> &[Section] {
&self.sections
}
fn symbols(&self) -> &[Symbol] {
&self.symbols
}
fn imports(&self) -> &[Import] {
&self.imports
}
fn exports(&self) -> &[Export] {
&self.exports
}
fn metadata(&self) -> &BinaryMetadata {
&self.metadata
}
}
fn parse_sections(macho: &MachO, data: &[u8]) -> Result<Vec<Section>> {
let mut sections = Vec::new();
for segment in &macho.segments {
for (section, _) in &segment.sections()? {
let name = section.name().unwrap_or("unknown").to_string();
// Determine section type based on section name and flags
let section_type = if section.flags & goblin::mach::constants::S_ATTR_PURE_INSTRUCTIONS
!= 0
|| name.starts_with("__text")
{
SectionType::Code
} else if name.starts_with("__data") {
SectionType::Data
} else if name.starts_with("__const") || name.starts_with("__rodata") {
SectionType::ReadOnlyData
} else if name.starts_with("__bss") {
SectionType::Bss
} else if name.starts_with("__debug") {
SectionType::Debug
} else {
SectionType::Other("MACHO_SECTION".to_string())
};
// Mach-O section permissions are inherited from segment
let permissions = SectionPermissions {
read: segment.initprot & 0x1 != 0, // VM_PROT_READ
write: segment.initprot & 0x2 != 0, // VM_PROT_WRITE
execute: segment.initprot & 0x4 != 0, // VM_PROT_EXECUTE
};
// Extract small section data
let section_data = if section.size <= 1024 && section.offset > 0 {
let start = section.offset as usize;
let end = start + section.size as usize;
if end <= data.len() {
Some(data[start..end].to_vec())
} else {
None
}
} else {
None
};
sections.push(Section {
name,
address: section.addr,
size: section.size,
offset: section.offset as u64,
permissions,
section_type,
data: section_data,
});
}
}
Ok(sections)
}
fn parse_symbols(_macho: &MachO) -> Result<Vec<Symbol>> {
let symbols = Vec::new();
// NOTE: Symbol parsing API changed in goblin 0.10, requires implementation update
// The symbol API has changed in goblin 0.10
// For now, create empty symbols vector
// symbols = vec![];
Ok(symbols)
}
fn parse_imports_exports(macho: &MachO) -> crate::types::ImportExportResult {
let mut imports = Vec::new();
let mut exports = Vec::new();
// Parse imports from bind info
for import in &macho.imports()? {
imports.push(Import {
name: import.name.to_string(),
library: Some(import.dylib.to_string()),
address: Some(import.address),
ordinal: None,
});
}
// Parse exports from export info
for export in &macho.exports()? {
exports.push(Export {
name: export.name.to_string(),
address: export.offset,
ordinal: None,
forwarded_name: None, // Mach-O doesn't have forwarded exports like PE
});
}
Ok((imports, exports))
}
fn analyze_security_features(macho: &MachO) -> SecurityFeatures {
let mut features = SecurityFeatures::default();
// Check file type and flags for security features
let flags = macho.header.flags;
// PIE (Position Independent Executable)
features.pie = flags & goblin::mach::header::MH_PIE != 0;
// ASLR is generally enabled with PIE on macOS
features.aslr = features.pie;
// NX bit (No-Execute) is typically enabled on modern macOS
features.nx_bit = true; // Default assumption for modern binaries
// Check for stack canaries (would need more complex analysis)
features.stack_canary = false; // Would need to analyze for __stack_chk_guard
// Check load commands for additional security features
for _load_command in &macho.load_commands {
// NOTE: LoadCommand variants changed in goblin 0.10, awaiting API stabilization
// LoadCommand::CodeSignature(_, _) => {
// features.signed = true;
// }
}
features
}
fn find_entry_point(macho: &MachO) -> Option<u64> {
// Look for LC_MAIN or LC_UNIX_THREAD load commands
for _load_command in &macho.load_commands {
// NOTE: LoadCommand variants changed in goblin 0.10, awaiting API stabilization
// LoadCommand::Main(entry) => {
// return Some(entry.entryoff);
// }
// LoadCommand::UnixThread(_) => {
// // Entry point is in the thread state
// // This is architecture-specific parsing
// return Some(0); // Placeholder - would need arch-specific parsing
// }
}
None
}
fn extract_compiler_info(macho: &MachO) -> Option<String> {
// Look for build version or version min load commands
for _load_command in &macho.load_commands {
// NOTE: LoadCommand variants changed in goblin 0.10, awaiting API stabilization
// LoadCommand::BuildVersion(build) => {
// return Some(format!(
// "Platform: {}, SDK: {}.{}.{}",
// build.platform,
// build.sdk >> 16,
// (build.sdk >> 8) & 0xff,
// build.sdk & 0xff
// ));
// }
}
Some("Unknown Apple toolchain".to_string())
}