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
#[cfg(feature = "server")]
use crate::offsets::offset_server::OffsetServer;
use crate::{
db::{Offsets, OffsetsDatabase},
offsets::{offset_server::Offset, OffsetsRequest, OffsetsResponse},
};
use pdb::{FallibleIterator, Source, SymbolData, SymbolTable, PDB};
use reqwest::StatusCode;
#[cfg(feature = "server")]
use std::net::SocketAddr;
use std::{borrow::Cow, collections::HashMap, fs::read_to_string, io::Cursor, path::PathBuf};
use tokio::fs::write;
use tokio::sync::Mutex;
#[cfg(feature = "server")]
use tonic::transport;
#[cfg(all(feature = "server", feature = "tls"))]
use tonic::transport::{Identity, ServerTlsConfig};
use tonic::{Request, Response, Status};
#[cfg(not(feature = "tls"))]
pub struct Identity {}
pub struct OffsetHandler {
pub database: Mutex<OffsetsDatabase>,
pub cache_path: PathBuf,
}
#[cfg(feature = "server")]
pub struct Server {
handler: OffsetHandler,
endpoint: SocketAddr,
#[allow(dead_code)]
tls_identity: Option<Identity>,
}
#[cfg(feature = "server")]
impl Server {
pub fn new<S: AsRef<str>>(
endpoint: SocketAddr,
cache_path: S,
tls_identity: Option<Identity>,
) -> Result<Server, anyhow::Error> {
Ok(Server {
handler: OffsetHandler::new(cache_path.as_ref())?,
endpoint,
tls_identity,
})
}
pub async fn run(self) -> Result<(), anyhow::Error> {
let endpoint = self.endpoint;
let mut server = transport::Server::builder();
#[cfg(feature = "tls")]
if let Some(tls_identity) = self.tls_identity {
server = server.tls_config(ServerTlsConfig::new().identity(tls_identity))?;
}
server
.add_service(OffsetServer::new(self.handler))
.serve(endpoint)
.await?;
Ok(())
}
}
impl OffsetHandler {
pub fn new<S: AsRef<str>>(cache_path: S) -> anyhow::Result<OffsetHandler> {
let database = Mutex::new(match read_to_string(cache_path.as_ref()) {
Ok(s) => serde_json::from_str(&s)?,
_ => OffsetsDatabase::default(),
});
Ok(OffsetHandler {
database,
cache_path: cache_path.as_ref().into(),
})
}
}
macro_rules! get_offset {
($map:ident, $name:literal) => {
match $map.get($name) {
Some(offset) => offset,
None => {
eprintln!("Failed to find offset for {}", $name);
return Ok(None);
}
}
};
}
fn get_offsets_from_pdb_bytes<'a, S: 'a + Source<'a>>(s: S) -> pdb::Result<Option<Offsets>> {
let mut pdb: PDB<'a, S> = pdb::PDB::open(s)?;
let symbol_table: SymbolTable<'a> = pdb.global_symbols()?;
let address_map = pdb.address_map()?;
let map: HashMap<Cow<str>, u32> = symbol_table
.iter()
.map(|sym| sym.parse())
.filter_map(|data| match data {
SymbolData::Public(proc) => Ok(Some(proc)),
_ => Ok(None),
})
.filter_map(|proc| match proc.offset.to_rva(&address_map) {
Some(rva) => Ok(Some((proc.name.to_string(), rva.0))),
_ => Ok(None),
})
.collect()?;
let ldrp_hash_table = *get_offset!(map, "LdrpHashTable");
let ldrp_module_datatable_lock = *get_offset!(map, "LdrpModuleDatatableLock");
let ldrp_handle_tls_data = *get_offset!(map, "LdrpHandleTlsData");
let ldrp_release_tls_entry = *get_offset!(map, "LdrpReleaseTlsEntry");
let ldrp_mapping_info_index = *get_offset!(map, "LdrpMappingInfoIndex");
let ldrp_module_base_address_index = *get_offset!(map, "LdrpModuleBaseAddressIndex");
let rtl_initialize_history_table = *get_offset!(map, "RtlInitializeHistoryTable");
Ok(Some(Offsets {
ldrp_hash_table,
ldrp_module_datatable_lock,
ldrp_handle_tls_data,
ldrp_release_tls_entry,
ldrp_mapping_info_index,
ldrp_module_base_address_index,
rtl_initialize_history_table,
}))
}
#[tonic::async_trait]
impl Offset for OffsetHandler {
async fn get_offsets(
&self,
request: Request<OffsetsRequest>,
) -> Result<Response<OffsetsResponse>, Status> {
let hash = request.into_inner().ntdll_hash;
if hash.len() != 33 {
return Err(Status::invalid_argument("Bad hash length"));
}
if !hash.chars().all(|x| char::is_ascii_hexdigit(&x)) {
return Err(Status::invalid_argument("Bad hex digit"));
}
let database = &mut self.database.lock().await;
let offsets_map = &mut database.offsets;
if let Some(offsets) = offsets_map.get(&hash) {
return Ok(Response::new(offsets.into()));
}
let pdb = match reqwest::get(format!(
"http://msdl.microsoft.com/download/symbols/ntdll.pdb/{}/ntdll.pdb",
&hash
))
.await
{
Ok(response) => response,
Err(e) => return Err(Status::not_found(format!("Error on fetch: {}", e))),
};
let status = pdb.status();
match status {
StatusCode::OK => {}
StatusCode::NOT_FOUND => {
return Err(Status::not_found("PDB hash not found"));
}
c => {
return Err(Status::internal(format!("Internal error: {}", c)));
}
};
let pdb = match pdb.bytes().await {
Ok(bytes) => bytes,
Err(e) => {
return Err(Status::internal(format!("Error on bytes: {}", e)));
}
};
let offsets = match get_offsets_from_pdb_bytes(Cursor::new(&pdb)) {
Ok(offsets) => offsets,
Err(e) => {
return Err(Status::internal(format!(
"Processing error: {}. Bytes: {:?}",
e, pdb
)));
}
};
let offsets = match offsets {
Some(offsets) => offsets,
None => {
return Err(Status::internal("Failed to find some functions"));
}
};
offsets_map.insert(hash, offsets);
let s = match serde_json::to_string::<OffsetsDatabase>(&*database) {
Ok(s) => s,
Err(e) => {
eprintln!("Failed to serialize database: {}. DB: {:?}", e, database);
return Ok(Response::new(offsets.into()));
}
};
match write(&self.cache_path, &s).await {
Ok(_) => {}
Err(e) => {
eprintln!(
"Failed to write database to cache file {}: {}. Payload: {}",
&self.cache_path.as_path().to_string_lossy(),
e,
&s
)
}
}
Ok(Response::new(offsets.into()))
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::offsets::{offset_server::Offset, OffsetsRequest};
use tonic::Request;
#[tokio::test]
async fn hash_length() {
let handler = OffsetHandler::new("test/cache.json").unwrap();
let request = Request::new(OffsetsRequest {
ntdll_hash: "123".into(),
});
let err = handler.get_offsets(request).await.unwrap_err();
assert_eq!(err.code(), tonic::Code::InvalidArgument);
assert_eq!(err.message(), "Bad hash length");
}
#[tokio::test]
async fn hash_digits() {
let handler = OffsetHandler::new("test/cache.json").unwrap();
let request = Request::new(OffsetsRequest {
ntdll_hash: "46F6F5C30E7147E46F2A953A5DAF201AG".into(),
});
let err = handler.get_offsets(request).await.unwrap_err();
assert_eq!(err.code(), tonic::Code::InvalidArgument);
assert_eq!(err.message(), "Bad hex digit");
}
#[tokio::test]
async fn not_found() {
let handler = OffsetHandler::new("test/cache.json").unwrap();
let request = Request::new(OffsetsRequest {
ntdll_hash: "46F6F5C30E7147E46F2A953A5DAF201A2".into(),
});
let err = handler.get_offsets(request).await.unwrap_err();
assert_eq!(err.message(), "PDB hash not found");
}
#[tokio::test]
async fn good_fetch() {
let handler = OffsetHandler::new("test/cache.json").unwrap();
let request = Request::new(OffsetsRequest {
ntdll_hash: "46F6F5C30E7147E46F2A953A5DAF201A1".into(),
});
let response = handler.get_offsets(request).await.unwrap().into_inner();
assert!(handler
.database
.lock()
.await
.offsets
.contains_key("46F6F5C30E7147E46F2A953A5DAF201A1"));
assert_eq!(response.ldrp_hash_table, 0x16A140);
assert_eq!(response.ldrp_module_datatable_lock, 0x16B240);
assert_eq!(response.ldrp_handle_tls_data, 0x47C14);
}
}