RDFU  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
SniffImage.c File Reference

Image sniffer. More...

Include dependency graph for SniffImage.c:

Go to the source code of this file.

Functions

VOID EFIAPI NotifyExitBootServices (EFI_EVENT Event, VOID *Context)
 
EFI_STATUS hooked_ExitBootServices (IN EFI_HANDLE ImageHandle, IN UINTN MapKey)
 
EFI_STATUS hooked_LoadImage (IN BOOLEAN BootPolicy, IN EFI_HANDLE ParentImageHandle, IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN VOID *SourceBuffer OPTIONAL, IN UINTN SourceSize, OUT EFI_HANDLE *ImageHandle)
 
EFI_STATUS InstallSniffImage (CALLBACK_IMAGESNIFFER_NEW UserNewImageCallback, CALLBACK_IMAGESNIFFER_MOD UserModImageCallback)
 
EFI_STATUS UnInstallSniffImage (VOID)
 

Variables

EFI_IMAGE_LOAD gOrg_LoadImage = 0
 
EFI_EXIT_BOOT_SERVICES gOrg_ExitBootServices = 0
 
EFI_EVENT gSetExitBootServicesEvent = 0
 
image_entry_list * gImageList = 0
 
CALLBACK_IMAGESNIFFER_NEW gUserImageSnifferNewImageCallback = 0
 
CALLBACK_IMAGESNIFFER_MOD gUserImageSnifferModImageCallback = 0
 

Detailed Description

Image sniffer.

Author
Piotr Bania
Date
Feb 2013
  • hooking LoadImage function from BootServices and monitoring all loaded images
  • creating a notification event for ExitBootServices signal, and scanning the loaded images for checksum changes (limited to code section)

Definition in file SniffImage.c.

Function Documentation

EFI_STATUS hooked_ExitBootServices ( IN EFI_HANDLE  ImageHandle,
IN UINTN  MapKey 
)

reserved for future: may be it would be also good to put the upper procedure in ExitBootServices direct hook it is important to note that ExitBootServices may be called multiple times (unlike the event itself).

Definition at line 128 of file SniffImage.c.

References EFI_STATUS(), and gOrg_ExitBootServices.

132 {
133  EFI_STATUS Status = (EFI_STATUS)gOrg_ExitBootServices(ImageHandle, MapKey);
134  return Status;
135 }

Here is the call graph for this function:

EFI_STATUS hooked_LoadImage ( IN BOOLEAN  BootPolicy,
IN EFI_HANDLE  ParentImageHandle,
IN EFI_DEVICE_PATH_PROTOCOL *  DevicePath,
IN VOID *SourceBuffer  OPTIONAL,
IN UINTN  SourceSize,
OUT EFI_HANDLE *  ImageHandle 
)

original LoadImage function is redirected here. Function writes to the log if any image is being loaded. It also dumps it to the "sdump" directory.

Function also executes the user mode callback if there is any.

Definition at line 151 of file SniffImage.c.

References AddImageToList(), DumpMemoryToFile(), EFI_STATUS(), GetLoadedImageProtocol(), gOrg_LoadImage, gUserImageSnifferNewImageCallback, and LogFileAscii().

Referenced by InstallSniffImage().

159 {
160 
161  EFI_STATUS Status, Status2;
162  EFI_LOADED_IMAGE_PROTOCOL *LoadedImageProto;
163 
164  CHAR16 DmpFilePath[512];
165 
166  image_entry *NewImageEntry;
167 
168  Status = gOrg_LoadImage(BootPolicy,
169  ParentImageHandle,
170  DevicePath,
171  SourceBuffer,
172  SourceSize,
173  ImageHandle);
174 
175 
176  if (!EFI_ERROR(Status))
177  {
178  Status2 = GetLoadedImageProtocol(*ImageHandle, &LoadedImageProto);
179  /*
180  if (EFI_ERROR(Status2))
181  {
182  AsciiSPrint(LogBuffer, sizeof(LogBuffer)-1, "Sniffed LoadedImage, handle=%08x, but unable to open!\r\n", *ImageHandle);
183  WriteLogFile(&LogBuffer);
184  return Status;
185  }
186  */
187 
188 
189  if (!EFI_ERROR(Status))
190  {
191 
192  LogFileAscii("%a: Sniffed LoadedImage, handle=%08x, imagebase=%016lx!\r\n",
193  __FUNCTION__,
194  *ImageHandle,
195  (MAX_PTR)LoadedImageProto->ImageBase);
196 
197  // dump file
198  UnicodeSPrint(DmpFilePath, sizeof(DmpFilePath)-1, L"sdump\\%p.dmp",
199  LoadedImageProto->ImageBase);
200 
201  LogFileAscii("%a: Dumping to %s\r\n", __FUNCTION__, DmpFilePath);
202 
203  DumpMemoryToFile((C_PTR)LoadedImageProto->ImageBase,
204  (UINTN)LoadedImageProto->ImageSize,
205  DmpFilePath,
206  TRUE);
207 
208  // add image to list
209  NewImageEntry = AddImageToList(gImageList,
210  *ImageHandle,
211  LoadedImageProto,
212  TRUE);
213 
216 
217  }
218  }
219 
220 
221 
222  return Status;
223 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS InstallSniffImage ( CALLBACK_IMAGESNIFFER_NEW  UserNewImageCallback,
CALLBACK_IMAGESNIFFER_MOD  UserModImageCallback 
)

Function installs hook for BootServices->LoadImage. At this point it is able to monitor which images are being loaded by the system.

Parameters
UserNewImageCallbackUser callback which will be executed when new image is detected
UserModImageCallbackUser callback which will be executed when a modified image will be detected
Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 243 of file SniffImage.c.

References EFI_STATUS(), FreeImages(), GetAllImagesWithDescription(), gOrg_ExitBootServices, gOrg_LoadImage, gSetExitBootServicesEvent, gUserImageSnifferModImageCallback, gUserImageSnifferNewImageCallback, hooked_LoadImage(), LogFullAscii(), and NotifyExitBootServices().

Referenced by DrawBoxAndMenu(), and TestDumpHandles().

245 {
246  EFI_STATUS Status;
247 
248  LogFullAscii(": Installing image sniffer...");
249 
250  if (UserNewImageCallback)
251  gUserImageSnifferNewImageCallback = UserNewImageCallback;
252  if (UserModImageCallback)
253  gUserImageSnifferModImageCallback = UserModImageCallback;
254 
255  gOrg_LoadImage = gBS->LoadImage;
256  gOrg_ExitBootServices = gBS->ExitBootServices;
257 
259  if (!gImageList)
260  {
261  LogFullAscii("%a: error executing GetAllImagesWithDescription\r\n",
262  __FUNCTION__);
263  return EFI_NOT_FOUND;
264  }
265 
266 
267  Status = gBS->CreateEvent(EVT_SIGNAL_EXIT_BOOT_SERVICES,
268  TPL_NOTIFY,
270  NULL,
272 
273  if (EFI_ERROR(Status))
274  {
275  LogFullAscii("%a: unable to create event, status = %08x\r\n",
276  __FUNCTION__, Status);
278  gImageList = 0;
279  return EFI_NOT_FOUND;
280 
281  }
282 
283 
284  gBS->LoadImage = &hooked_LoadImage;
285  //gBS->ExitBootServices = &hooked_ExitBootServices;
286 
287  LogFullAscii("done.\r\n");
288 
289  return EFI_SUCCESS;
290 }

Here is the call graph for this function:

Here is the caller graph for this function:

VOID EFIAPI NotifyExitBootServices ( EFI_EVENT  Event,
VOID *  Context 
)

This function is executed when the ExitBootServices function is executed correctly by the system loader or other EFI application.

Function enumarates all loaded images and checks whether their checksum was changed. It also executes user callback if there is any.

Definition at line 51 of file SniffImage.c.

References _image_entry::Crc32, _image_entry::FilePath, FindImageByHandle(), FreeImages(), GetAllImagesWithDescription(), gUserImageSnifferModImageCallback, _image_entry::Handle, _image_entry::ImageBase, _image_entry_list::ImageEntry, _image_entry::ImageSize, LogFileAscii(), _image_entry::Name, and _image_entry_list::NumOfEntries.

Referenced by InstallSniffImage().

53 {
54  UINTN i;
55  image_entry *ImageEntry, *ImageEntryMatched;
56  image_entry_list *ImageList;
57 
58  //LogFileAscii("EXIT BOTO SERVICES\r\n");
59 
60  if (!gImageList)
61  return;
62 
63  ImageList = GetAllImagesWithDescription(NULL, TRUE);
64 
65  if (ImageList)
66  {
67  for (i = 0; i < ImageList->NumOfEntries; i++)
68  {
69  ImageEntry = &ImageList->ImageEntry[i];
70  ImageEntryMatched = FindImageByHandle(gImageList, ImageEntry->Handle);
71 
72  if (!ImageEntryMatched ||
73  ((C_PTR)ImageEntry->ImageBase != (C_PTR)ImageEntryMatched->ImageBase))
74  LogFileAscii("%a: Unable to find image HANDLE=%08x FilePath=%a Name=%a\r\n",
75  __FUNCTION__,
76  ImageEntry->Handle,
77  ImageEntry->FilePath,
78  ImageEntry->Name);
79  else
80  {
81  if (ImageEntry->Crc32 != ImageEntryMatched->Crc32)
82  {
83  LogFileAscii("Image HANDLE=%08x ##### MISMATCHED CRC ##### %08x -> %08x\r\n",
84  ImageEntry->Handle,
85  ImageEntry->Crc32,
86  ImageEntryMatched->Crc32);
87 
88 
89  LogFileAscii("1ST ENTRY -> Index=%08x Handle=%08x ImageBase=%016lx ImageSize=%08llx Crc32=%08x Name=%a FilePath=%a\r\n",
90  i,
91  ImageEntry->Handle,
92  (MAX_PTR)ImageEntry->ImageBase,
93  ImageEntry->ImageSize,
94  ImageEntry->Crc32,
95  ImageEntry->Name,
96  ImageEntry->FilePath);
97  LogFileAscii("2ND ENTRY -> Index=%08x Handle=%08x ImageBase=%016lx ImageSize=%08llx Crc32=%08x Name=%a FilePath=%a\r\n",
98  i,
99  ImageEntryMatched->Handle,
100  (MAX_PTR)ImageEntryMatched->ImageBase,
101  ImageEntryMatched->ImageSize,
102  ImageEntryMatched->Crc32,
103  ImageEntryMatched->Name,
104  ImageEntryMatched->FilePath);
105 
106 
108  gUserImageSnifferModImageCallback(gImageList, ImageEntryMatched);
109 
110  }
111  }
112  }
113  }
114 
115  FreeImages(ImageList);
117 
118  gImageList = 0;
119 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS UnInstallSniffImage ( VOID  )

Function uninstalls the image sniffer.

Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 305 of file SniffImage.c.

References FreeImages(), gOrg_LoadImage, gSetExitBootServicesEvent, and LogFullAscii().

306 {
307  LogFullAscii(": Uninstalling image sniffer...");
308 
309  gBS->LoadImage = gOrg_LoadImage;
310  //gBS->ExitBootServices = org_ExitBootServices;
311 
313  gBS->CloseEvent(gSetExitBootServicesEvent);
314 
315  LogFullAscii("done.\r\n");
316 
317 
319  gImageList = 0;
320  return EFI_SUCCESS;
321 }

Here is the call graph for this function:

Variable Documentation

image_entry_list* gImageList = 0

Definition at line 29 of file SniffImage.c.

EFI_EXIT_BOOT_SERVICES gOrg_ExitBootServices = 0

Definition at line 25 of file SniffImage.c.

Referenced by hooked_ExitBootServices(), and InstallSniffImage().

EFI_IMAGE_LOAD gOrg_LoadImage = 0

original EFI functions

Definition at line 24 of file SniffImage.c.

Referenced by hooked_LoadImage(), InstallSniffImage(), and UnInstallSniffImage().

EFI_EVENT gSetExitBootServicesEvent = 0

Definition at line 28 of file SniffImage.c.

Referenced by InstallSniffImage(), and UnInstallSniffImage().

CALLBACK_IMAGESNIFFER_MOD gUserImageSnifferModImageCallback = 0

Definition at line 36 of file SniffImage.c.

Referenced by InstallSniffImage(), and NotifyExitBootServices().

CALLBACK_IMAGESNIFFER_NEW gUserImageSnifferNewImageCallback = 0

user callbacks (optional)

Definition at line 35 of file SniffImage.c.

Referenced by hooked_LoadImage(), and InstallSniffImage().