-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCheckMemSignature.cpp
More file actions
308 lines (254 loc) · 7.51 KB
/
CheckMemSignature.cpp
File metadata and controls
308 lines (254 loc) · 7.51 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
297
298
299
300
301
302
303
304
305
306
307
308
/*
* File: CheckMemSignature.cpp
* Author: rbross
*
* Created on September 22, 2010, 10:48 AM
*/
#include "CheckMemSignature.h"
#include "executeshell.h"
#include <sstream>
#include <iomanip>
CheckMemSignature::CheckMemSignature(bool bMemLog)
{ // Get our hardware key
if (!GetHardwareKey())
{
::openlog("[spore].checkmemsignature", LOG_PID, LOG_LOCAL0);
::syslog(LOG_CRIT, "Unable to create encryption key");
::closelog();
return;
}
}
CheckMemSignature::~CheckMemSignature()
{
}
bool CheckMemSignature::GetResult()
{
#ifdef PRODUCTION_UNIT
char strSig[SIG_LENGTH + 1];
unsigned char uSig[HASH_LENGTH];
char strHex[3];
// Read physical memory
if (ReadPhysicalMem(SIG_OFFSET, SIG_LENGTH, (unsigned char *) strSig) < 0)
return false;
strSig[SIG_LENGTH] = '\0';
unsigned char *pMD5Value = GetMD5Hash((unsigned char *) strSig, SIG_LENGTH + 1);
if (!pMD5Value)
{
::openlog("[spore].checkmemsignature", LOG_PID, LOG_LOCAL0);
::syslog(LOG_WARNING, "Unable to create a valid MD5 hash");
::closelog();
return false;
}
// Convert hex hash string to binary
strHex[2] = '\0';
// ALIX 3D2
for(int iIndex = 0; iIndex < HASH_LENGTH << 1; iIndex += 2)
{
::memcpy(strHex, SIG_HASH3 + iIndex, 2);
uSig[iIndex >> 1] = ConvertHexByte(strHex);
}
// Compare result
int iX = ::memcmp(uSig, pMD5Value, HASH_LENGTH);
if (iX == 0)
return true;
// ALIX 6E1
for(int iIndex = 0; iIndex < HASH_LENGTH << 1; iIndex += 2)
{
::memcpy(strHex, SIG_HASH6 + iIndex, 2);
uSig[iIndex >> 1] = ConvertHexByte(strHex);
}
// Compare result
iX = ::memcmp(uSig, pMD5Value, HASH_LENGTH);
if (iX == 0)
return true;
::openlog("[spore].checkmemsignature", LOG_PID, LOG_LOCAL0);
::syslog(LOG_CRIT, "Hardware error; unable to find valid MD5 hash.");
::closelog();
return false;
#else
return true;
#endif
}
// Get an MD5hash
unsigned char *CheckMemSignature::GetMD5Hash(unsigned char *pBuffer, int iBufLen)
{
EVP_MD_CTX mdctx;
const EVP_MD *md;
unsigned int md_len;
// Now calculate the hash for the buffer and compared to the stored hash
OpenSSL_add_all_digests();
md = EVP_get_digestbyname("md5");
if (!md)
{
::openlog("[spore].checkmemsignature", LOG_PID, LOG_LOCAL0);
::syslog(LOG_ERR, "Unable to initialize MD5 digest");
::closelog();
return NULL;
}
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, pBuffer, iBufLen);
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
EVP_MD_CTX_cleanup(&mdctx);
return md_value;
}
// Read physical memory
int CheckMemSignature::ReadPhysicalMem(int iOffset, int iSize, unsigned char *pBuffer)
{
int mem_fd;
// Read the memory at the designated offset
if ((mem_fd = ::open("/dev/mem", O_RDONLY)) < 0)
{
string sLogOutput = "Error checking Spore memory: ";
sLogOutput += ::strerror(errno);
::openlog("[spore].checkmemsignature", LOG_PID, LOG_LOCAL0);
::syslog(LOG_ERR, "%s", sLogOutput.c_str());
::closelog();
return -1;
}
::lseek(mem_fd, iOffset, SEEK_SET);
::memset(pBuffer, 0, iSize);
int iRet = ::read(mem_fd, pBuffer, iSize);
::close(mem_fd);
if (iRet != iSize)
{
::openlog("[spore].checkmemsignature", LOG_PID, LOG_LOCAL0);
::syslog(LOG_ERR, "Cannot read sufficient hardware memory buffer");
::closelog();
return -1;
}
return 0;
}
// Get a hardware specific key
unsigned char *CheckMemSignature::GetHardwareKey()
{
string sKey;
::memset(ucHardwareKey, 0, sizeof(ucHardwareKey));
// This is no good because if the user is plugged into eth1 you can't get the address
// string sIPv6MacAddr = GetIPv6MacAddr("eth0");
// Get MAC address the easy way
ExecuteShell::Execute("/sbin/ifconfig eth0 | grep HWaddr | cut -d' ' -f11", sKey);
// Make sure that we got something back
if (!sKey.length())
{
::openlog("[spore].checkmemsignature", LOG_PID, LOG_LOCAL0);
::syslog(LOG_ERR, "Unable to retrieve network parameter");
::closelog();
return NULL;
}
sKey.insert(0, "silverspore");
sKey += "checkmemsignatute";
// Now turn it into an MD5 hash for the key
unsigned char *pHash = GetMD5Hash((unsigned char *) sKey.c_str(), sKey.length());
if (!pHash)
return NULL;
::memcpy(ucHardwareKey, pHash, sizeof(ucHardwareKey));
return ucHardwareKey;
}
// Get the hardware key
const char *CheckMemSignature::DisplayKey()
{
static string sKey;
char szFormat[3];
sKey.clear();
for (int iX = 0; iX < sizeof(ucHardwareKey); iX++)
{
::sprintf(szFormat, "%02x", ucHardwareKey[iX]);
sKey += szFormat;
}
return sKey.c_str();
}
// Get serial number
const char *CheckMemSignature::GetSerialNumber()
{
char strSig[SIG_LENGTH + 1];
string sBuffer;
// Get MAC address the easy way
ExecuteShell::Execute("/sbin/ifconfig eth0 | grep HWaddr | cut -d' ' -f11", sBuffer);
// Convert MAC address to something else
string sHexKey;
for(int iX = 6; iX < sBuffer.length(); iX++)
{
char strNibble[3];
if (sBuffer[iX] == ':' || sBuffer[iX] == '\n')
continue;
string sNibble = "0x";
sNibble += sBuffer[iX];
char *p;
unsigned char cNibble = ::strtoul(sNibble.c_str(), &p, 16);
cNibble ^= 0xf;
::sprintf(strNibble, "%x", cNibble);
sHexKey += strNibble[0];
}
// Read physical memory
if (ReadPhysicalMem(SIG_OFFSET, SIG_LENGTH, (unsigned char *) strSig) < 0)
{
sSerialNo = "DEMO_MODE";
return(sSerialNo.c_str());
}
strSig[SIG_LENGTH] = '\0';
if (!::strcmp(strSig, ALIX3D2_SIG))
{
sSerialNo = "PCX.32.";
sSerialNo += sHexKey;
}
else if (!::strcmp(strSig, ALIX6E1_SIG))
{
sSerialNo = "PCX.61.";
sSerialNo += sHexKey;
}
else
{
sSerialNo = "DEMO_MODE";
}
return(sSerialNo.c_str());
}
// Convert hex byte string to unsigned char
unsigned int CheckMemSignature::ConvertHexByte(const char *pHex)
{
unsigned int ucDigit;
stringstream ss;
ss << std::hex << pHex;
ss >> ucDigit;
return(ucDigit);
}
// Get the IPv6 MAC address
const char *CheckMemSignature::GetIPv6MacAddr(const char *strIfName)
{
static string sAddress;
sAddress.clear();
struct ifaddrs *ifa = NULL, *ifEntry = NULL;
void *addPtr = NULL;
int rc = 0;
char addressBuffer[INET6_ADDRSTRLEN];
rc = getifaddrs(&ifa);
if (rc == 0)
{
for(ifEntry = ifa; ifEntry != NULL; ifEntry = ifEntry->ifa_next)
{
if (ifEntry->ifa_addr->sa_data == NULL)
{
continue;
}
if (::strcmp(ifEntry->ifa_name, strIfName))
continue;
else if (ifEntry->ifa_addr->sa_family == AF_INET6)
{
addPtr = &((struct sockaddr_in6 *) ifEntry->ifa_addr)->sin6_addr;
}
else
{ // It IPv6
continue;
}
// Make printable
const char *a = inet_ntop(ifEntry->ifa_addr->sa_family, addPtr, addressBuffer, sizeof(addressBuffer));
if (a != NULL)
{
sAddress = a;
break;
}
}
freeifaddrs(ifa);
}
return sAddress.c_str();
}