-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEnlyzeS7SoftwareControllerDecoder.cpp
More file actions
296 lines (247 loc) · 10 KB
/
Copy pathEnlyzeS7SoftwareControllerDecoder.cpp
File metadata and controls
296 lines (247 loc) · 10 KB
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
290
291
292
293
294
295
296
// Copyright 2023 ENLYZE GmbH
// SPDX-License-Identifier: Apache-2.0
//
// ENLYZE version of pintool as presented in "sOfT7: Revealing the Secrets of Siemens S7 PLCs" (https://i.blackhat.com/USA-22/Wednesday/US-22-Bitan-Revealing-S7-PLCs.pdf)
// Written by Colin Finck for ENLYZE GmbH
#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>
#include "pin.H"
const size_t STACK_SIZE = 100 * 1024 * 1024;
const size_t LOWER_ADDRESS_BOUND = 0x2000000000;
const size_t UPPER_ADDRESS_BOUND = 0x3000000000;
const size_t ALLOC_PFN = 0x20000bd5e0;
const size_t SETUP_PFN = 0x20000550a0;
const size_t DECODE_PFN = 0x2000143fc0;
struct CF_DecryptWorkStruct
{
void* pfnFirstFieldFun;
unsigned int TotalSizeOfFinalOutputFile;
unsigned int CurrentOffsetInOutputFile;
char *pTempBuf;
unsigned int DecodedBytesInThisIteration;
unsigned int SomethingElse;
unsigned int SetToZero1;
unsigned int SetToZero1_1;
unsigned int SetToZero2;
unsigned int OutputChunkBufferSize;
void* pElfImage;
char* pOutputChunkBuffer;
unsigned int Z11;
unsigned int Z12;
unsigned int Z3;
unsigned int Z4;
unsigned int CurrentOffsetInOutputChunkBuffer;
unsigned int Z14;
unsigned int Z9;
unsigned int Z10;
unsigned int TotalDecodedBytes;
unsigned int OutputChunkBufferSizeAgain;
};
KNOB<std::string> KnobCpuFile(KNOB_MODE_WRITEONCE, "pintool", "c", "", "Path to the encoded cpu.elf file");
ADDRINT g_addrEntry;
char *g_pCpuFile;
unsigned long long g_cbCpuFile;
std::ofstream g_OutputFile;
char *g_pStack;
CF_DecryptWorkStruct *g_pWorkStruct;
unsigned long long g_ReturnValue;
INT32 Usage()
{
std::cerr << "Runs the code from vmm_2nd_stage.elf to decode the given cpu.elf file" << std::endl
<< "ENLYZE version of pintool as presented in \"sOfT7: Revealing the Secrets of Siemens S7 PLCs\"" << std::endl
<< std::endl;
std::cerr << KNOB_BASE::StringKnobSummary() << std::endl;
return -1;
}
void *MyAlloc(void* unused, unsigned long long cb, unsigned long long unused_value)
{
std::cout << "[*] Hello from MyAlloc for " << cb << " bytes" << std::endl;
void* p = malloc(cb);
memset(p, 0, cb);
std::cout << " Allocated @ " << StringFromAddrint((ADDRINT)p) << std::endl;
return p;
}
VOID ImageLoad(IMG img, VOID* v)
{
std::cout << "[*] Loading image " << IMG_Name(img).c_str() << " @ " << StringFromAddrint(IMG_StartAddress(img)) << std::endl;
if (!IMG_IsMainExecutable(img))
{
return;
}
if (!IMG_IsStaticExecutable(img))
{
std::cerr << "Image is not static and can't be the vmm_2nd_stage.elf. Aborted!" << std::endl;
exit(1);
}
g_addrEntry = IMG_EntryAddress(img);
std::cout << "[*] Entry point @ " << StringFromAddrint(g_addrEntry) << std::endl;
for (SEC sec = IMG_SecHead(img); SEC_Valid(sec); sec = SEC_Next(sec))
{
for (RTN rtn = SEC_RtnHead(sec); RTN_Valid(rtn); rtn = RTN_Next(rtn))
{
std::cout << "[*] Routine @ " << StringFromAddrint(RTN_Address(rtn)) << std::endl;
}
}
}
UINT64 MyCallback(CF_DecryptWorkStruct* pWorkStruct)
{
printf("===========================\n");
std::cout << "[*] Hello from MyCallback" << std::endl;
printf("pfnFirstFieldFun = %p\n", pWorkStruct->pfnFirstFieldFun);
printf("TotalSizeOfFinalOutputFile = %08x\n", pWorkStruct->TotalSizeOfFinalOutputFile);
printf("CurrentOffsetInOutputFile = %08x\n", pWorkStruct->CurrentOffsetInOutputFile);
printf("pTempBuf = %p\n", pWorkStruct->pTempBuf);
printf("DecodedBytesInThisIteration = %08x\n", pWorkStruct->DecodedBytesInThisIteration);
printf("SomethingElse = %08x\n", pWorkStruct->SomethingElse);
printf("SetToZero1 = %08x\n", pWorkStruct->SetToZero1);
printf("SetToZero1_1 = %08x\n", pWorkStruct->SetToZero1_1);
printf("SetToZero2 = %08x\n", pWorkStruct->SetToZero2);
printf("OutputChunkBufferSize = %08x\n", pWorkStruct->OutputChunkBufferSize);
printf("pElfImage = %p\n", pWorkStruct->pElfImage);
printf("pOutputChunkBuffer = %p\n", pWorkStruct->pOutputChunkBuffer);
printf("Z11 = %08x\n", pWorkStruct->Z11);
printf("Z12 = %08x\n", pWorkStruct->Z12);
printf("Z3 = %08x\n", pWorkStruct->Z3);
printf("Z4 = %08x\n", pWorkStruct->Z4);
printf("CurrentOffsetInOutputChunkBuffer = %08x\n", pWorkStruct->CurrentOffsetInOutputChunkBuffer);
printf("Z14 = %08x\n", pWorkStruct->Z14);
printf("Z9 = %08x\n", pWorkStruct->Z9);
printf("Z10 = %08x\n", pWorkStruct->Z10);
printf("TotalDecodedBytes = %08x\n", pWorkStruct->TotalDecodedBytes);
printf("OutputChunkBufferSizeAgain = %08x\n", pWorkStruct->OutputChunkBufferSizeAgain);
std::cout << "[*] Saving " << pWorkStruct->DecodedBytesInThisIteration << " bytes for this chunk" << std::endl;
// pOutputChunkBuffer is used like a ring buffer.
// We have to check if it overflowed.
unsigned int Start = pWorkStruct->CurrentOffsetInOutputFile % pWorkStruct->OutputChunkBufferSize;
unsigned int End = std::min(Start + pWorkStruct->DecodedBytesInThisIteration, pWorkStruct->OutputChunkBufferSize);
unsigned int cb = End - Start;
g_OutputFile.write(&pWorkStruct->pOutputChunkBuffer[Start], cb);
if (Start + pWorkStruct->DecodedBytesInThisIteration > pWorkStruct->OutputChunkBufferSize)
{
// It overflowed. Write the other part as well.
Start = 0;
End = pWorkStruct->CurrentOffsetInOutputChunkBuffer;
cb = End - Start;
g_OutputFile.write(&pWorkStruct->pOutputChunkBuffer[Start], cb);
}
if (pWorkStruct->TotalDecodedBytes == pWorkStruct->TotalSizeOfFinalOutputFile)
{
std::cout << "[*] Finished successfully" << std::endl;
exit(0);
}
return 0;
}
void MyEntryPoint()
{
std::cout << "[*] Hello from MyEntryPoint" << std::endl;
// Set up the work struct.
{
CONTEXT ctxt = {};
char *pRsp = g_pStack + STACK_SIZE / 2;
PIN_SetContextRegval(&ctxt, REG_RSP, (const UINT8 *)&pRsp);
std::cout << "[*] Calling Decrypt_SetupStruct" << std::endl;
PIN_CallApplicationFunction(&ctxt, PIN_ThreadId(), CALLINGSTD_DEFAULT, (AFUNPTR)SETUP_PFN, nullptr,
PIN_PARG(unsigned long long *), &g_ReturnValue,
PIN_PARG(void *), g_pWorkStruct,
PIN_PARG_END());
}
// Perform the decoding.
{
CONTEXT ctxt = {};
char *pRsp = g_pStack + STACK_SIZE / 2;
PIN_SetContextRegval(&ctxt, REG_RSP, (const UINT8 *)&pRsp);
std::cout << "[*] Calling Decrypt_Do" << std::endl;
PIN_CallApplicationFunction(&ctxt, PIN_ThreadId(), CALLINGSTD_DEFAULT, (AFUNPTR)DECODE_PFN, nullptr,
PIN_PARG(unsigned long long *), &g_ReturnValue,
PIN_PARG(void *), g_pWorkStruct,
PIN_PARG(char *), g_pCpuFile,
PIN_PARG(unsigned long long), g_cbCpuFile,
PIN_PARG(char *), g_pWorkStruct->pTempBuf,
PIN_PARG(unsigned long long), 0x1000,
PIN_PARG(void *), MyCallback,
PIN_PARG_END());
}
}
void TraceInstruction(std::string *pstrTrace)
{
std::cout << *pstrTrace << std::endl;
}
VOID Instruction(INS ins, VOID* v)
{
ADDRINT addrIns = INS_Address(ins);
std::string strTrace = StringFromAddrint(addrIns) + ": " + INS_Disassemble(ins);
if (addrIns < LOWER_ADDRESS_BOUND || addrIns > UPPER_ADDRESS_BOUND)
{
// We only want to instrument the VMM image, not our own code when jumping to it.
return;
}
if (addrIns == g_addrEntry)
{
std::cout << strTrace << " --> calling MyEntryPoint instead" << std::endl;
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)MyEntryPoint, IARG_END);
}
else if (addrIns == ALLOC_PFN)
{
std::cout << strTrace << " --> jumping to MyAlloc instead" << std::endl;
INS_InsertDirectJump(ins, IPOINT_BEFORE, (ADDRINT)MyAlloc);
}
else
{
// Uncomment this to trace every instruction (heavy!)
//INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)TraceInstruction, IARG_PTR, new std::string(strTrace), IARG_END);
}
}
int main(int argc, char *argv[])
{
if (PIN_Init(argc, argv))
{
return Usage();
}
// Read the CPU file.
std::string strCpuFile = KnobCpuFile.Value();
if (strCpuFile.empty())
{
return Usage();
}
std::ifstream CpuFile(strCpuFile);
if (!CpuFile)
{
std::cerr << "Cannot open " << strCpuFile << std::endl;
return 1;
}
CpuFile.seekg(0, std::ios::end);
g_cbCpuFile = CpuFile.tellg();
CpuFile.seekg(0, std::ios::beg);
g_pCpuFile = (char *)malloc(g_cbCpuFile);
CpuFile.read(g_pCpuFile, g_cbCpuFile);
std::cout << "[*] Read " << g_cbCpuFile << " VMM bytes to decode @ " << StringFromAddrint((ADDRINT)g_pCpuFile) << std::endl;
// Create the output file.
std::string strOutputFile = strCpuFile + ".decoded";
g_OutputFile.open(strOutputFile);
if (!g_OutputFile)
{
std::cerr << "Cannot open " << strOutputFile << " for writing!" << std::endl;
return 1;
}
// Adjust inputs for the function we want to call.
g_pCpuFile += 4;
g_cbCpuFile -= 4;
UINT8 Skip = (UINT8)*g_pCpuFile;
g_pCpuFile += Skip;
g_cbCpuFile -= Skip;
// Prepare the buffers we need.
g_pStack = (char*)malloc(STACK_SIZE);
memset(g_pStack, 0, STACK_SIZE);
std::cout << "[*] Allocated g_pStack @ " << StringFromAddrint((ADDRINT)g_pStack) << std::endl;
g_pWorkStruct = (CF_DecryptWorkStruct*)malloc(sizeof(CF_DecryptWorkStruct));
memset(g_pWorkStruct, 0, sizeof(CF_DecryptWorkStruct));
std::cout << "[*] Allocated g_pWorkStruct @ " << StringFromAddrint((ADDRINT)g_pWorkStruct) << std::endl;
g_ReturnValue = 0;
// Instrument.
IMG_AddInstrumentFunction(ImageLoad, nullptr);
INS_AddInstrumentFunction(Instruction, 0);
PIN_StartProgram();
return 0;
}