RDFU  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
TableMonitor.c
Go to the documentation of this file.
1 
11 #include "EfiScannerHeaders.h"
12 
13 
14 
15 typedef struct _func_table
16 {
17  C_PTR ptr_va; // *(C_PTR*)ptr_va -> gives the API destination
19  CHAR8 name[56];
20 } func_table;
21 
28  // runtime servies
29  { 0, 0, "GetTime"},
30  { 0, 0, "SetTime"},
31  { 0, 0, "GetWakeupTime"},
32  { 0, 0, "SetWakeupTime"},
33  { 0, 0, "SetVirtualAddressMap"},
34  { 0, 0, "ConvertPointer"},
35  { 0, 0, "GetVariable"},
36  { 0, 0, "GetNextVariableName"},
37  { 0, 0, "SetVariable"},
38  { 0, 0, "GetNextHighMonotonicCount"},
39  { 0, 0, "ResetSystem"},
40  { 0, 0, "UpdateCapsule"},
41  { 0, 0, "QueryCapsuleCapabilities"},
42  { 0, 0, "QueryVariableInfo"},
43 
44  // boot time services
45  { 0, 0, "RaiseTPL"},
46  { 0, 0, "RestoreTPL"},
47  { 0, 0, "AllocatePages"},
48  { 0, 0, "FreePages"},
49  { 0, 0, "GetMemoryMap"},
50  { 0, 0, "AllocatePool"},
51  { 0, 0, "FreePool"},
52  { 0, 0, "CreateEvent"},
53  { 0, 0, "SetTimer"},
54  { 0, 0, "WaitForEvent"},
55  { 0, 0, "SignalEvent"},
56  { 0, 0, "CloseEvent"},
57  { 0, 0, "CheckEvent"},
58  { 0, 0, "InstallProtocolInterface"},
59  { 0, 0, "ReinstallProtocolInterface"},
60  { 0, 0, "UninstallProtocolInterface"},
61  { 0, 0, "HandleProtocol"},
62  { 0, 0, "RegisterProtocolNotify"},
63  { 0, 0, "LocateHandle"},
64  { 0, 0, "LocateDevicePath"},
65  { 0, 0, "InstallConfigurationTable"},
66  { 0, 0, "LoadImage"},
67  { 0, 0, "StartImage"},
68  { 0, 0, "Exit"},
69  { 0, 0, "UnloadImage"},
70  { 0, 0, "ExitBootServices"},
71  { 0, 0, "GetNextMonotonicCount"},
72  { 0, 0, "Stall"},
73  { 0, 0, "SetWatchdogTimer"},
74  { 0, 0, "ConnectController"},
75  { 0, 0, "DisconnectController"},
76  { 0, 0, "OpenProtocol"},
77  { 0, 0, "CloseProtocol"},
78  { 0, 0, "OpenProtocolInformation"},
79  { 0, 0, "ProtocolsPerHandle"},
80  { 0, 0, "LocateHandleBuffer"},
81  { 0, 0, "LocateProtocol"},
82  { 0, 0, "InstallMultipleProtocolInterfaces"},
83  { 0, 0, "UninstallMultipleProtocolInterfaces"},
84  { 0, 0, "CalculateCrc32"},
85  { 0, 0, "CopyMem"},
86  { 0, 0, "SetMem"},
87  { 0, 0, "CreateEventEx"},
88 
89  // EFI_SIMPLE_TEXT_INPUT_PROTOCOL *ConIn;
90  // Only ReadKey is interesting
91  { 0, 0, "ReadKey"},
92 
93 
94  { 0, 0, "?"}
95 };
96 
97 
104 {
105 #define write_entry(index, api) { gEFITables[index].ptr_va = (UINT64)&api; gEFITables[index].last_addr = (UINT64)api; }
106 
107 
108  write_entry(0, gRT->GetTime);
109  write_entry(1, gRT->SetTime);
110  write_entry(2, gRT->GetWakeupTime);
111  write_entry(3, gRT->SetWakeupTime);
112  write_entry(4, gRT->SetVirtualAddressMap);
113  write_entry(5, gRT->ConvertPointer);
114  write_entry(6, gRT->GetVariable);
115  write_entry(7, gRT->GetNextVariableName);
116  write_entry(8, gRT->SetVariable);
117  write_entry(9, gRT->GetNextHighMonotonicCount);
118  write_entry(10, gRT->ResetSystem);
119  write_entry(11, gRT->UpdateCapsule);
120  write_entry(12, gRT->QueryCapsuleCapabilities);
121  write_entry(13, gRT->QueryVariableInfo);
122 
123 
124  // boot time services
125  write_entry(14, gBS->RaiseTPL);
126  write_entry(15, gBS->RestoreTPL);
127  write_entry(16, gBS->AllocatePages);
128  write_entry(17, gBS->FreePages);
129  write_entry(18, gBS->GetMemoryMap);
130  write_entry(19, gBS->AllocatePool);
131  write_entry(20, gBS->FreePool);
132  write_entry(21, gBS->CreateEvent);
133  write_entry(22, gBS->SetTimer);
134  write_entry(23, gBS->WaitForEvent);
135  write_entry(24, gBS->SignalEvent);
136  write_entry(25, gBS->CloseEvent);
137  write_entry(26, gBS->CheckEvent);
138  write_entry(27, gBS->InstallProtocolInterface);
139  write_entry(28, gBS->ReinstallProtocolInterface);
140  write_entry(29, gBS->UninstallProtocolInterface);
141  write_entry(30, gBS->HandleProtocol);
142  write_entry(31, gBS->RegisterProtocolNotify);
143  write_entry(32, gBS->LocateHandle);
144  write_entry(33, gBS->LocateDevicePath);
145  write_entry(34, gBS->InstallConfigurationTable);
146  write_entry(35, gBS->LoadImage);
147  write_entry(36, gBS->StartImage);
148  write_entry(37, gBS->Exit);
149  write_entry(38, gBS->UnloadImage);
150  write_entry(39, gBS->ExitBootServices);
151  write_entry(40, gBS->GetNextMonotonicCount);
152  write_entry(41, gBS->Stall);
153  write_entry(42, gBS->SetWatchdogTimer);
154  write_entry(43, gBS->ConnectController);
155  write_entry(44, gBS->DisconnectController);
156  write_entry(45, gBS->OpenProtocol);
157  write_entry(46, gBS->CloseProtocol);
158  write_entry(47, gBS->OpenProtocolInformation);
159  write_entry(48, gBS->ProtocolsPerHandle);
160  write_entry(49, gBS->LocateHandleBuffer);
161  write_entry(50, gBS->LocateProtocol);
162  write_entry(51, gBS->InstallMultipleProtocolInterfaces);
163  write_entry(52, gBS->UninstallMultipleProtocolInterfaces);
164  write_entry(53, gBS->CalculateCrc32);
165  write_entry(54, gBS->CopyMem);
166  write_entry(55, gBS->SetMem);
167  write_entry(56, gBS->CreateEventEx);
168 
169 
170  // EFI_SIMPLE_TEXT_INPUT_PROTOCOL *ConIn;
171  write_entry(57, gST->ConIn->ReadKeyStroke);
172 
173 
174 #undef write_entry
175 
176  return EFI_SUCCESS;
177 }
178 
179 
180 /*
181 * Just a basic check for call/jmp rel (5bytes version)
182 * Maybe later extend to something else.
183 */
184 BOOLEAN IsFunctionHooked(C_PTR FuncAddr)
185 {
186  UCHAR *p = (UCHAR*)FuncAddr;
187  if ((p[0] == 0xE9) || (p[0] == 0xE8))
188  return TRUE;
189 
190  return FALSE;
191 }
192 
193 
194 
195 
196 
197 /*
198 *
199 * Function dumps/shows all functions (runtime/boottime)
200 * It also shows which one of them was modified and
201 * to which module it belongs.
202 *
203 * Additionally we are checking if the selected function
204 * was patched with JMP/CALL rel which points to other
205 * image or to memory outside the original image.
206 */
207 
208 EFI_STATUS DumpEFITables(BOOLEAN SilentMode)
209 {
210  int i, change;
211  CHAR8 *p;
212 
213  C_PTR ImageBase;
214  UINT64 ImageSize;
215  C_PTR TempAddr;
216 
217  ULONG32 HookDest;
218  BOOLEAN WasHooked;
219  image_entry *HookDestImageEntry;
220 
221  image_entry_list *ImageList;
222  image_entry *ImageEntry;
223 
224 
225  if (!SilentMode)
226  LogFullAscii(": Dumping EFI Functions tables...\r\n");
227 
228 
229  ImageList = GetAllImagesWithDescription(NULL, FALSE);
230  if (!ImageList)
231  {
232  LogFullAscii("%a: Error executing GetAllImagesWithDescription failed\r\n", __FUNCTION__);
233  return EFI_NOT_FOUND;
234  }
235 
236 
237 
238  for (i = 0; gEFITables[i].name[0] != '?'; i++)
239  {
240  ImageBase = 0;
241  ImageSize = 0;
242  change = 0;
243 
244  TempAddr = *(C_PTR*)gEFITables[i].ptr_va;
245  p = (CHAR8*)&gEFITables[i].name;
246 
247  ImageEntry = FindImageByAddr(ImageList, TempAddr);
248 
249  if (ImageEntry)
250  {
251  // addr in in range of image
252  ImageBase = (C_PTR)ImageEntry->ImageBase;
253  ImageSize = ImageEntry->ImageSize;
254  }
255 
256  // check if there was a change in the values
257  if (TempAddr != gEFITables[i].last_addr)
258  {
259  change = 1;
260 
261  LogFileAscii("[%04d] Modification detected OldAddr=%016lx NewAddr=%016lx Function Name: %a\r\n",
262  i,
263  (MAX_PTR)gEFITables[i].last_addr,
264  (MAX_PTR)TempAddr,
265  p);
266 
267 
268  if (!SilentMode)
269  {
270  PRINT_FUNC_ASCII("[%04d] Modification detected OldAddr=%016lx NewAddr=%016lx Function Name: %a\r\n",
271  i,
272  (MAX_PTR)gEFITables[i].last_addr,
273  (MAX_PTR)TempAddr,
274  p);
275  }
276 
277  }
278 
279  if (!SilentMode)
280  {
281  WasHooked = FALSE;
282 
283  // on mac mini QueryCapsuleCapabilities/CreateEventEx is invalid therefore probing it
284  // will cause an access violation
285  // lets assume on x64 that every address above 4gb is not valid
286 
287 #if defined(_WIN64)
288  if ((ULONG32)(gEFITables[i].last_addr >> 32))
289  LogFullAscii("Addr: %016lx is probably invalid!\r\n", gEFITables[i].last_addr);
290  else
291  WasHooked = IsFunctionHooked(gEFITables[i].last_addr);
292 #else
293  WasHooked = IsFunctionHooked(gEFITables[i].last_addr);
294 #endif
295 
296  LogFullAscii("[%04d] Addr: %016lx M: %d InImage: %016lx-%016lx Function Name: %a \r\n",
297  i,
298  (MAX_PTR)TempAddr,
299  change,
300  (MAX_PTR)ImageBase, (
301  MAX_PTR)(ImageBase + ImageSize),
302  p);
303 
304  if (WasHooked)
305  {
306  // at this point we only know that the function prologue contains
307  // a long rel jump or a rel call
308  // so lets check the destination of it as well
309 
310  HookDest = (ULONG32)(*(INT32*)(gEFITables[i].last_addr + 1) + gEFITables[i].last_addr + 5);
311  HookDestImageEntry = FindImageByAddr(ImageList, HookDest);
312 
313  // function is hooked when HookDestImageEntry = 0 or it points to
314  // an image different from the current image
315  if (!HookDestImageEntry)
316  LogFullAscii("Function %016lx is hooked to %016lx (not in image)\r\n",
317  (MAX_PTR)gEFITables[i].last_addr,
318  (MAX_PTR)HookDest);
319  else if (HookDestImageEntry != ImageEntry)
320  LogFullAscii("Function %016lx is hooked to %016lx (image base=%016lx)\r\n",
321  (MAX_PTR)gEFITables[i].last_addr,
322  (MAX_PTR)HookDest,
323  (MAX_PTR)HookDestImageEntry->ImageBase);
324  }
325  }
326  else if (change == 1)
327  {
328  // we are now in timmer so we log only this modified entry
329 
330  LogFileAscii("[%04d] Addr: %016lx M: %d InImage: %016lx-%016lx Function Name: %a \r\n",
331  i,
332  (MAX_PTR)TempAddr,
333  change,
334  (MAX_PTR)ImageBase,
335  (MAX_PTR)(ImageBase + ImageSize),
336  p);
337 
338 
339  // update the last addr, because otherwise the log will be flooded
340  // with the same information (because of the timer)
341 
342  gEFITables[i].last_addr = TempAddr;
343 
344  }
345 
346  }
347 
348  if (!SilentMode)
349  LogFullAscii(": Dumping EFI Functions tables done.\r\n");
350 
351  return EFI_SUCCESS;
352 }
353 
359 VOID
361  IN EFI_EVENT Event,
362  IN VOID *Context
363 )
364 {
365  int i;
366  C_PTR TempAddr;
367 
368  // if we spot a change then we call the DumpEFITables
369  // otherwise it is too slow
370 
371  for (i = 0; gEFITables[i].name[0] != '?'; i++)
372  {
373  TempAddr = *(C_PTR*)gEFITables[i].ptr_va;
374  if (TempAddr != gEFITables[i].last_addr)
375  {
376 
377  DumpEFITables(TRUE);
378  return;
379  }
380  }
381 }
382 
383 
384 
385 EFI_EVENT gScannerEvent = 0;
386 
387 
398 C_PTR EFITableGetAddr(CHAR8 *FuncName)
399 {
400  UINTN i;
401 
402  if (!FuncName)
403  return 0;
404 
405  for (i = 0; gEFITables[i].name[0] != '?'; i++)
406  {
407  if (strcmp(gEFITables[i].name, FuncName) == 0)
408  return gEFITables[i].last_addr;
409  }
410 
411  return 0;
412 }
413 
414 
427 {
428  EFI_STATUS Status;
429 
430  LogFullAscii(": Installing the EFI table scanner\r\n");
431 
432  Status = gBS->CreateEvent(EVT_NOTIFY_SIGNAL | EVT_TIMER,
435  NULL,
436  &gScannerEvent);
437  if (EFI_ERROR(Status))
438  {
439  LogFullAscii("%a: Unable to install the table scanner, status = %08x\r\n",
440  __FUNCTION__, Status);
441  return Status;
442  }
443 
444 
445  Status = gBS->SetTimer(gScannerEvent,
446  TimerPeriodic,
447  SAMPLE_INTERVAL);
448 
449 
450  if (EFI_ERROR(Status))
451  {
452  gBS->CloseEvent(gScannerEvent);
453 
454  LogFullAscii("%a: Unable to install the timer for table scanner, status = %08x\r\n",
455  __FUNCTION__, Status);
456 
457  return Status;
458  }
459 
460  return EFI_SUCCESS;
461 }
462 
472 {
473  LogFullAscii(": UnInstalling the EFI table scanner\r\n");
474 
475  if (gScannerEvent)
476  gBS->CloseEvent(gScannerEvent);
477 
478  return EFI_SUCCESS;
479 }
480 
481 
482