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
//data_analyzer.rs
// analyzes that that is not traversed in the analysis queue
use arch::x86::archx86::X86Detail;
use arch::x86::analyzex86::*;
pub use disasm::*;
use std::collections::VecDeque;

// 1) Determine if data
// - Consecutive zeros 
// - Consecutive C
// - A Failed instruction
// 2) Check for address size boundaries
const MAX_SIGNATURE_SIZE: usize = 16;
pub fn check_if_padding(
	offset: usize, 
	analysis: &mut Analysisx86, 
	mem_manager: &mut MemoryManager) -> (bool, usize)
{
		
	let mut new_offset = offset;
	let known_padding: [u8; 3] = [0x00, 0xCC, 0x90];
	let address_size = match analysis.xi.mode
    {
        Mode::Mode16=>2,
        Mode::Mode32=>4,
        Mode::Mode64=>8,
        _=>4,
    };
    let start = offset-analysis.base;
    let end = start + address_size;
    for padding in known_padding.iter()
    {
    	if mem_manager.get_image_by_type(MemoryType::Image)
                [start..end] == *vec![*padding ; address_size].as_slice()
	    {
	    	let code_start = analysis.base + analysis.header.base_of_code as usize;
	    	let x = offset - code_start;
	    	let mut y = (x / address_size) * address_size;
	    	if y < x {
	    		y = y + address_size;
	    		new_offset = y + code_start;
	    	}
	    	return (true, new_offset);
	    }
    }
    return (false, new_offset);
}

pub fn analyze_datax86(
	offset: usize, 
	analysis: &mut Analysisx86, 
	mem_manager: &mut MemoryManager,
	state: &mut Statex86,
    queue: &mut VecDeque<Statex86>)-> AnalysisResult
{
	//
	let start = offset;
	let address_size = match analysis.xi.mode
    {
        Mode::Mode16=>2,
        Mode::Mode32=>4,
        Mode::Mode64=>8,
        _=>4,
    };

	let value = mem_manager.read(start, address_size, analysis);
	match mem_manager.get_mem_type(value as usize)
	{
		// Is a valid address
		MemoryType::Image=>{
			// 1) Check for function header ->
			//       a) add newfunction
			//       b) send to analysis queue as new current function
			//       c) db offset <ADDRESS> ; FUNC_<ADDRESS>
			//       d) continue
			if check_for_function_header(
				value as usize, 
				address_size,
				analysis,
				mem_manager)
			{
                let func_name = match analysis.add_func(
                    0,
                    offset as u64,
                    0,
                    0, 
                    value as u64, 
                    MemoryType::Image,
                    false)
                {
                    Some(f)=>f,
                    None=>String::new(),
                };
				let mut instructions: Instruction<X86Detail>;
				instructions = Instruction::new();
				instructions.address = offset as u64;
				instructions.offset = offset as u64;
				let bytes = transmute_integer_to_vec(value as usize, address_size);
				for (i, value) in bytes.iter().enumerate()
				{
					instructions.bytes[i] = *value;
				}
				instructions.mnemonic = String::from("dd");
				match address_size
				{
					2=>instructions.op_str = format!("offset 0x{:x}", value as u16),
					4=>instructions.op_str = format!("offset 0x{:x}", value as u32),
					8=>instructions.op_str = format!("offset 0x{:x}", value as u64),
					_=>{},
				}
				instructions.size = address_size;
				let mut valid_instr = InstructionInfo
				  {
				    instr: instructions,
				    detail: Vec::new(),
				};
				valid_instr.detail.push(DetailInfo{op_index: 0, contents: func_name});
				analysis.instr_info.insert(offset as u64, valid_instr);
				let new_analysis: Statex86 = Statex86 
                {
                    offset: value as usize,
                    cpu: state.cpu.clone(), 
                    stack: Vec::new(),
                    current_function_addr: value as u64,
                    emulation_enabled: state.emulation_enabled,
                    loop_state: state.loop_state.clone(),
                    analysis_type: AnalysisType::Code,
                };
                queue.push_front(new_analysis);
                
				let mut new_data_analysis = state.clone();
				new_data_analysis.offset = offset + address_size;
                queue.push_back(new_data_analysis);
                return AnalysisResult::End;
			}
			// 2) if not db offset <ADDRESS>, fill bytes
			//       a) add address as bytes db <BYTES>, fill bytes
			//       b) continue
			else {
				let mut instructions: Instruction<X86Detail>;
				instructions = Instruction::new();
				instructions.address = offset as u64;
				instructions.offset = offset as u64;
				let bytes = transmute_integer_to_vec(value as usize, address_size);
				for (i, value) in bytes.iter().enumerate()
				{
					instructions.bytes[i] = *value;
				}
				instructions.mnemonic = String::from("dd");
				match address_size
				{
					2=>instructions.op_str = format!("offset 0x{:x}", value as u16),
					4=>instructions.op_str = format!("offset 0x{:x}", value as u32),
					8=>instructions.op_str = format!("offset 0x{:x}", value as u64),
					_=>{},
				}
				instructions.size = address_size;
				let mut valid_instr = InstructionInfo
				  {
				    instr: instructions,
				    detail: Vec::new(),
				};
				analysis.instr_info.insert(offset as u64, valid_instr);
			}
		},
		// Not a valid address
		_=>{
			// 1) Check for function header
			//       a) add newfunction
			//       b) send to analysis queue as new current function
			//       c) Break
			if check_for_function_header(
				start, 
				address_size,
				analysis,
				mem_manager)
			{
				analysis.add_func(0, 0, 0, 0, 
                    start as u64, 
                    MemoryType::Image,
                    false);
				let new_analysis: Statex86 = Statex86 
                {
                    offset: start,
                    cpu: state.cpu.clone(), 
                    stack: Vec::new(),
                    current_function_addr: start as u64,
                    emulation_enabled: state.emulation_enabled,
                    loop_state: state.loop_state.clone(),
                    analysis_type: AnalysisType::Code,
                };
                queue.push_front(new_analysis);
                return AnalysisResult::End;
			} else if check_for_jump(
				start, 
				address_size,
				analysis,
				mem_manager)
			{
				let new_analysis: Statex86 = Statex86 
                {
                    offset: start,
                    cpu: state.cpu.clone(), 
                    stack: Vec::new(),
                    current_function_addr: 0,
                    emulation_enabled: state.emulation_enabled,
                    loop_state: state.loop_state.clone(),
                    analysis_type: AnalysisType::Code,
                };
                queue.push_front(new_analysis);
                return AnalysisResult::End;
			}
			// 2) if not, db <BYTES>, fill bytes 
			else {
			    let mut instructions: Instruction<X86Detail>;
				instructions = Instruction::new();
				instructions.address = offset as u64;
				instructions.offset = offset as u64;
				let bytes = transmute_integer_to_vec(value as usize, address_size);
				for (i, value) in bytes.iter().enumerate()
				{
					instructions.bytes[i] = *value;
				}
				instructions.mnemonic = String::from("db");
				match address_size
				{
					2=>instructions.op_str = format!("0x{:x}", value as u16),
					4=>instructions.op_str = format!("0x{:x}", value as u32),
					8=>instructions.op_str = format!("0x{:x}", value as u64),
					_=>{},
				}
				instructions.size = address_size;
				let mut _valid_instr = InstructionInfo
				  {
				    instr: instructions,
				    detail: Vec::new(),
				};
				analysis.instr_info.insert(offset as u64, _valid_instr);
			}
		},
	}
	return AnalysisResult::Continue;
}

fn check_for_function_header(
	offset: usize, 
	address_size: usize,
	analysis: &mut Analysisx86, 
	mem_manager: &mut MemoryManager) -> bool
{
	let max_length = mem_manager.get_image_by_type(MemoryType::Image).len();
	let start: isize = (offset as isize) - (analysis.base as isize);
	if start < 0
	{
		return false;
	}
	let mut end = (start as usize) + MAX_SIGNATURE_SIZE;
	if end > max_length
	{
		end = max_length;
	}
	match address_size
    {
        4=>{
        	let results = analysis.sig_analyzer.match_bytes(&mem_manager.get_image_by_type(MemoryType::Image)[(start as usize)..end]);
        	match results.get(&String::from("_standard_func_header"))
        	{
        		Some(result)=>{
        			if result.contains(&0usize)
        			{
        				return true;
        			}
        		}
        		None=>{},
        	}
        	match results.get(&String::from("_non_standard_func_header"))
        	{
        		Some(result)=>{
        			if result.contains(&0usize)
        			{
        				return true;
        			}
        		}
        		None=>{},
        	}
        },
        8=>{
	    	let results = analysis.sig_analyzer.match_bytes(&mem_manager.get_image_by_type(MemoryType::Image)[(start as usize)..end]);
	    	match results.get(&String::from("_standard_func_header"))
	    	{
	    		Some(result)=>{
	    			if result.contains(&0usize)
	    			{
	    				return true;
	    			}
	    		}
	    		None=>{},
	    	}
        },
        _=>{},
    }
	return false;
}

fn check_for_jump(
	offset: usize, 
	address_size: usize,
	analysis: &mut Analysisx86, 
	mem_manager: &mut MemoryManager) -> bool
{
	let max_length = mem_manager.get_image_by_type(MemoryType::Image).len();
	let start: isize = (offset as isize) - (analysis.base as isize);
	if start < 0
	{
		return false;
	}
	let mut end = (start as usize) + MAX_SIGNATURE_SIZE;
	if end > max_length
	{
		end = max_length;
	}
	match address_size
    {
        4=>{
        	let results = analysis.sig_analyzer.match_bytes(&mem_manager.get_image_by_type(MemoryType::Image)[(start as usize)..end]);
        	match results.get(&String::from("_possible_function_jump"))
        	{
        		Some(result)=>{
        			if result.contains(&0usize)
        			{
        				return true;
        			}
        		}
        		None=>{},
        	}
        },
        8=>{
	    	let results = analysis.sig_analyzer.match_bytes(&mem_manager.get_image_by_type(MemoryType::Image)[(start as usize)..end]);
	    	match results.get(&String::from("_possible_function_jump"))
	    	{
	    		Some(result)=>{
	    			if result.contains(&0usize)
	    			{
	    				return true;
	    			}
	    		}
	    		None=>{},
	    	}
        },
        _=>{},
    }
	return false;
}