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
use crate::{header, section, segment};
use std::io::{BufWriter, Write};
use std::os::unix::fs::OpenOptionsExt;
#[repr(C)]
pub struct ELF64 {
ehdr: header::Ehdr64,
sections: Vec<section::Section64>,
segments: Vec<segment::Segment64>,
}
impl ELF64 {
pub fn new(elf_header: header::Ehdr64) -> Self {
Self {
ehdr: elf_header,
sections: Vec::new(),
segments: Vec::new(),
}
}
pub fn first_shidx_by<P>(&self, predicate: P) -> Option<usize>
where
P: Fn(§ion::Section64) -> bool,
{
for (i, sct) in self.sections.iter().enumerate() {
if predicate(sct) {
return Some(i);
}
}
None
}
pub fn first_section_by<P>(&self, predicate: P) -> Option<§ion::Section64>
where
P: Fn(§ion::Section64) -> bool,
{
match self.first_shidx_by(predicate) {
Some(idx) => Some(&self.sections[idx]),
None => None,
}
}
pub fn first_mut_section_by<P>(&mut self, predicate: P) -> Option<&mut section::Section64>
where
P: Fn(§ion::Section64) -> bool,
{
match self.first_shidx_by(predicate) {
Some(idx) => Some(&mut self.sections[idx]),
None => None,
}
}
pub fn set_segments(&mut self, segments: Vec<segment::Segment64>) {
self.segments = segments;
}
pub fn clone_sections(&self) -> Vec<section::Section64> {
self.sections.clone()
}
pub fn iter_sections_as_mut(&mut self) -> std::slice::IterMut<section::Section64> {
self.sections.iter_mut()
}
pub fn condition(&mut self) {
self.ehdr.set_shentsize(section::Shdr64::size());
self.ehdr.set_shnum(self.sections.len() as u16);
self.ehdr.set_shstrndx(self.sections.len() as u16 - 1);
self.ehdr.set_ehsize(header::Ehdr64::size());
let shoff = header::Ehdr64::size() as u64 + self.all_section_size();
self.ehdr.set_shoff(shoff);
let file_offset = header::Ehdr64::size() as u64;
self.clean_sections_offset(file_offset);
let shstrndx = self.ehdr.get_shstrndx() as usize;
let shnum = self.ehdr.get_shnum() as usize;
let mut sh_name = 1;
for (idx, bb) in self.sections[shstrndx]
.bytes
.as_ref()
.unwrap()
.to_vec()
.splitn(shnum, |num| *num == 0x00)
.enumerate()
{
if idx == 0 || idx >= shnum {
continue;
}
let b: Vec<&u8> = bb
.iter()
.take_while(|num| *num != &0x00)
.collect::<Vec<&u8>>();
self.sections[idx].header.set_name(sh_name as u32);
sh_name += b.len() as u32 + 1;
}
}
pub fn to_le_bytes(&self) -> Vec<u8> {
let mut file_binary: Vec<u8> = Vec::new();
let mut header_binary = self.ehdr.to_le_bytes();
file_binary.append(&mut header_binary);
for seg in self.segments.iter() {
let mut phdr_binary = seg.header.to_le_bytes();
file_binary.append(&mut phdr_binary);
}
for sct in self.sections.iter() {
let mut section_binary = sct.to_le_bytes();
file_binary.append(&mut section_binary);
}
for sct in self.sections.iter() {
let mut shdr_binary = sct.header.to_le_bytes();
file_binary.append(&mut shdr_binary);
}
file_binary
}
pub fn section_number(&self) -> usize {
self.sections.len()
}
pub fn segment_number(&self) -> usize {
self.segments.len()
}
pub fn add_section(&mut self, sct: section::Section64) {
self.sections.push(sct);
}
pub fn get_ehdr_as_mut(&mut self) -> &mut header::Ehdr64 {
&mut self.ehdr
}
pub fn all_section_size(&self) -> u64 {
self.sections.iter().map(|sct| sct.header.get_size()).sum()
}
pub fn add_segment(&mut self, seg: segment::Segment64) {
self.segments.push(seg);
}
fn clean_sections_offset(&mut self, base: u64) {
let mut total = base;
for section in self.sections.iter_mut() {
let sh_offset = section.header.get_offset();
section.header.set_offset(sh_offset + total);
let sh_size = section.header.get_size();
total += sh_size;
}
}
}
pub struct ELF64Dumper {
pub file: ELF64,
permission: u32,
}
impl ELF64Dumper {
pub fn new(f: ELF64, perm: u32) -> Self {
Self {
file: f,
permission: perm,
}
}
pub fn generate_elf_file(
&self,
output_filename: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let bytes = self.file.to_le_bytes();
let file = std::fs::OpenOptions::new()
.create(true)
.read(true)
.write(true)
.mode(self.permission)
.open(output_filename)?;
let mut writer = BufWriter::new(file);
writer.write_all(&bytes)?;
writer.flush()?;
Ok(())
}
}