看雪CTF.TSRC 2018 團隊賽 第八題 『二向箔』 解題思路
第八題《二向箔》在今天(12月17日)中午12:00 結束攻擊。本題共有4支團隊破解成功!
tekkens 以 64059 s 成為首支攻擊成功的團隊。本題出題團隊Tnppppppp也因此位列防守方第二名。
最新賽況戰況一覽
第八題之後攻擊方最新排名情況如下:
中午放題搬磚狗哭哭 守擂成功,有驚無險。風間仁所在的 tekkens 從第三名晉升到第二名!
作為多界看雪CTF的冠軍選手,tekkens與新晉看雪CTF新秀的PK,越發緊張,究竟風間仁能否繼續往屆的風采?而新晉冠軍是否能夠笑到最後?亦或者新晉黑馬突出重圍?
不到最後一切都是懸念,後續還會有怎樣的精彩?不加入怎麼知道呢?
第八題 點評
crownless:
二向箔此題由兩部分組成:第一部分是一個DPRE(基於4f系統的雙隨機相位編碼)演算法,難點在於識別演算法。第二部分為修改過的AES+XXTEA,需要攻擊者熟悉並理解這兩種加密演算法的原理。
第八題 出題團隊簡介
出題團隊: Tnppppppp
第八題 設計思路
由看雪論壇waiWH 原創
註冊成功:Good!
註冊碼:
76474B2B1926009C452B00627200190268740438FDCC641665D0EA735F2739B3EE7B315A
格式:
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
設計思路:
題目分成兩部分
第一部分是一個DPRE(基於4f系統的雙隨機相位編碼)演算法。
校驗流程為:把B當作原始影像的訊號,在經過Cat對映(Arnold對映),得到的二維訊號,與內建的RM1相乘,經過fft2變換,再與RM2相乘,再作ifft2變換。
可表達為: I = Cat(I);
EncryptionIm=ifft2(fft2(((I)).*RM1Im).*RM2Im);//(當進行*RM1時會填充成複數)
將得到的EncryptionIm與內建的一組EncryptionIm對比,如果一樣,即第一部分成功。
也就是需要攻擊者將內建的一組EncryptionIm進行解密還原
解密流程:
可表達為:
DecryptionIm=ifft2(fft2(double(EncryptionIm)).*conj(double(RM2Im))).*conj(double(RM1Im));
DecryptionIm = invCatMap(DecryptionIm);
難點就是需要攻擊者去識別是fft2,ifft2演算法,以及知道解密時需要對內建的RM2Im,RM1Im的複數陣列進行共軛,之後才進行fft2,ifft2。
當然,逆運算得到的DecryptionIm是一組複數陣列,不過複數部分為0,也就是輸入B註冊碼所需要的是整數部分。
第二部分為AES+XXTEA
AES有修改過sbox/invsbox,列混合變換採用8*8矩陣,每輪新增了一個矩陣轉置,以及修改行位移。使aes的特徵不那麼明顯。
驗證流程為:
將攻擊者輸入的A段註冊碼,經過xxtea解密,然後AES256解密。判斷最後得到的解密資料是否與內建的一組資料相同。
修改sbox/invsbox為:
採用8*8矩陣進行矩陣轉置,需要攻擊者對inv_mix矩陣求逆。由於這個8*8矩陣和逆矩陣一樣,所以直接貼出逆列混合變換所使用的逆矩陣。
修改行位移
變換中增加mix_transpose進行轉置。
另外想展示一下vm引擎,但由於檔案限制大小需要控制在1MB,所以就隨便v了一下流程。
攻擊者有很多方法去還原出該部分演算法,比如可以直接去觀察state所在的堆疊位置等。
vm引擎還有很多不足之處,一些模組未完成。所以也想借此機會看看攻擊者如何去分析該引擎。
那麼攻擊者需要先使用aes進行加密,然後在使用xxtea演算法加密,最後就得到A段註冊碼。
原文連結:
https://bbs.pediy.com/thread-248058.htm第八題 二向箔 解題思路
本題解析由看雪論壇 風間仁原創。
1. 處理邏輯
BYTE sn[16+20]; check1驗證前半部分, check2驗證後半部分。
.text:00402811 lea edx, [ebp+sn] .text:00402814 push edx ; buf .text:00402815 lea eax, [ebp+str_sn] .text:0040281B push eax ; a1 .text:0040281C call hex_to_bin .text:00402821 add esp, 20h .text:00402824 cmp eax, 36 .text:00402827 jnz short loc_402849 .text:00402829 lea ecx, [ebp+sn] .text:0040282C push ecx .text:0040282D call x_check1 .text:00402832 add esp, 4 .text:00402835 test eax, eax .text:00402837 jz short loc_402849 .text:00402839 lea edx, [ebp+sn+10h] .text:0040283C push edx .text:0040283D call
2. check1(主要是二元一次浮點運算), 得到sn前半部分:
76474B2B1926009C452B006272001902 test.cpp typedef struct tagPointF{ double x; double y; }PointF, *PPointF; double get_pi() { double pi; *((DWORD64 *)&pi) = 0x400921FB5442771C; return pi; } string g_str_part1_expected = util::hex2bin("EEB9171C0B535640B08F207B1FCB53C03EDEDAB2D8CD5BC0C2E04F8024B8014028D9AE091D454D40BCBFB0BAA4103D40593D04F9440D494079F8577F3FE845402A3EB76F3D4943C09047A102410318C0FE3CD86E3FD2574074E505B84FEE37409355C67FA98D5F40CC052BFED1914BC048D363E2EE4244404009C744AB39354050B7F31ADCAB0F4054CC0B4B2BA02B40309FBBE1D42C504018CF21801F7026C00A5D83EA8655514094B7E265F3E24240B1215C0315E74840C001F6C35AAEF03F34EE29D023421F4084016925DD933D4058E4B88B3C46204038F87B858407274070161F32582318C0B8D55808C32F20C0546B434AF2893940D89E2FD10FA4FA3F"); PPointF g_part1_expected = (PPointF)g_str_part1_expected.c_str(); string g_str_pts_const1 = util::hex2bin("EE1926ACE715E43FC9F8493847E9E8BF36075F9A6DD5EEBFA81D8421FE1ED13F000000000000F03F075C143326A6B1BC000000000000F03F000000000000000036075F9A6DD5EEBFA81D8421FE1ED13F000000000000F03F0000000000000000AB8604B97D69E73F141BA89A86D0E5BF41B13E0865DAEF3F151D92D94880B8BF000000000000F03F075C143326A6B1BCAB8604B97D69E73F141BA89A86D0E5BF2F58C368100FEE3FEAC193588EF3D53F1869F1C5E9FAEF3F508B6EE7560AA23F000000000000F03F000000000000000041B13E0865DAEF3F151D92D94880B8BF1869F1C5E9FAEF3F508B6EE7560AA23F138D486D01E2ED3FC402F17478E4D6BF"); PPointF g_pts_const1 = (PPointF)g_str_pts_const1.c_str(); string g_str_pts_const2 = util::hex2bin("26B18B5A4F64EC3F19D8B235EB85DD3F000000000000F03F075C143326A6B1BC000000000000F03F00000000000000006F5AAE0BC8B7EB3F7A016B8D7DFBDF3F000000000000F03F075C143326A6B1BC6F5AAE0BC8B7EB3F7A016B8D7DFBDF3F96A95B1782F4EF3F258E251B341CAB3FCDE70F56B7D9EF3FC76F400E7CB8B8BF000000000000F03F000000000000000096A95B1782F4EF3F258E251B341CAB3FBC732036F7D2EF3F2C266CAF5ECEBA3FD4702B0AD39CEF3F8605EF9065DBC33F6F5AAE0BC8B7EB3F7A016B8D7DFBDF3FCDE70F56B7D9EF3FC76F400E7CB8B8BFD4702B0AD39CEF3F8605EF9065DBC33FB4ADFBFF5EC3E83FD93C046A9144E43F"); PPointF g_pts_const2 = (PPointF)g_str_pts_const2.c_str(); PointF g_pi_const1[2] = { { cos((0) * get_pi() / 2), sin((0) * get_pi() / 2) }, { cos((-1) * get_pi() / 2), sin((-1) * get_pi() / 2) }, }; PointF g_pi_const2[2] = { { cos((0) * get_pi() / 2), sin((0) * get_pi() / 2) }, { cos((1) * get_pi() / 2), sin((1) * get_pi() / 2) }, }; void x_p0(PPointF pts, PPointF pts_const, int count) { for (int i = 0; i < count; i++) { double x = pts[i].x; double y = pts[i].y; double m = pts_const[i].x; double n = pts_const[i].y; pts[i].x = x * m - y * n; pts[i].y = x * n + y * m; } } void x_p0_r(PPointF pts, PPointF pts_const, int count) { for (int i = 0; i < count; i++) { double x = pts[i].x; double y = pts[i].y; double m = pts_const[i].x; double n = pts_const[i].y; pts[i].x = (y * n + x * m) / (m * m + n * n); pts[i].y = (y * m - x * n) / (m * m + n * n); } } void x_p1_raw(PPointF pts, PPointF pts_const) { PointF p0; PointF p1; PointF p2; PointF p3; PointF c0 = pts_const[0]; PointF c1 = pts_const[1]; p0 = pts[0]; p1 = pts[1]; p2 = pts[2]; p3 = pts[3]; pts[0].x = p0.x + p2.x; pts[0].y = p0.y + p2.y; pts[1].x = p1.x + p3.x; pts[1].y = p1.y + p3.y; pts[2].x = p0.x - p2.x; pts[2].y = p0.y - p2.y; pts[3].x = p1.x - p3.x; pts[3].y = p1.y - p3.y; x_p0(pts + 2, &c0, 1); x_p0(pts + 3, &c1, 1); p0 = pts[0]; p1 = pts[1]; p2 = pts[2]; p3 = pts[3]; pts[0].x = p0.x + p1.x; pts[0].y = p0.y + p1.y; pts[1].x = p0.x - p1.x; pts[1].y = p0.y - p1.y; pts[2].x = p2.x + p3.x; pts[2].y = p2.y + p3.y; pts[3].x = p2.x - p3.x; pts[3].y = p2.y - p3.y; x_p0(pts + 1, &c0, 1); x_p0(pts + 3, &c0, 1); PointF tmp = pts[1]; pts[1] = pts[2]; pts[2] = tmp; } void x_p1_raw_r(PPointF pts, PPointF pts_const) { PointF tmp = pts[1]; pts[1] = pts[2]; pts[2] = tmp; PointF p0; PointF p1; PointF p2; PointF p3; PointF c0 = pts_const[0]; PointF c1 = pts_const[1]; x_p0_r(pts + 1, &c0, 1); x_p0_r(pts + 3, &c0, 1); p0 = pts[0]; p1 = pts[1]; p2 = pts[2]; p3 = pts[3]; pts[0].x = (p0.x + p1.x) / 2; pts[1].x = (p0.x - p1.x) / 2; pts[0].y = (p0.y + p1.y) / 2; pts[1].y = (p0.y - p1.y) / 2; pts[2].x = (p2.x + p3.x) / 2; pts[3].x = (p2.x - p3.x) / 2; pts[2].y = (p2.y + p3.y) / 2; pts[3].y = (p2.y - p3.y) / 2; x_p0_r(pts + 2, &c0, 1); x_p0_r(pts + 3, &c1, 1); p0 = pts[0]; p1 = pts[1]; p2 = pts[2]; p3 = pts[3]; pts[0].x = (p0.x + p2.x) / 2; pts[2].x = (p0.x - p2.x) / 2; pts[0].y = (p0.y + p2.y) / 2; pts[2].y = (p0.y - p2.y) / 2; pts[1].x = (p1.x + p3.x) / 2; pts[3].x = (p1.x - p3.x) / 2; pts[1].y = (p1.y + p3.y) / 2; pts[3].y = (p1.y - p3.y) / 2; } void x_p1(PPointF pts) { size_t i; PointF tmp[4][4]; for (i = 0; i < 4; i++) { tmp[0][i] = pts[4 * i + 0]; tmp[1][i] = pts[4 * i + 1]; tmp[2][i] = pts[4 * i + 2]; tmp[3][i] = pts[4 * i + 3]; } for (i = 0; i < 4; i++) { x_p1_raw(&tmp[i][0], g_pi_const1); } for (i = 0; i < 4; i++) { pts[4 * i + 0] = tmp[0][i]; pts[4 * i + 1] = tmp[1][i]; pts[4 * i + 2] = tmp[2][i]; pts[4 * i + 3] = tmp[3][i]; } for (i = 0; i < 4; i++) { x_p1_raw(pts + 4 * i, g_pi_const1); } } void x_p1_r(PPointF pts) { size_t i; PointF tmp[4][4]; for (i = 0; i < 4; i++) { x_p1_raw_r(pts + 4 * i, g_pi_const1); } for (i = 0; i < 4; i++) { tmp[0][i] = pts[4 * i + 0]; tmp[1][i] = pts[4 * i + 1]; tmp[2][i] = pts[4 * i + 2]; tmp[3][i] = pts[4 * i + 3]; } for (i = 0; i < 4; i++) { x_p1_raw_r(&tmp[i][0], g_pi_const1); } for (i = 0; i < 4; i++) { pts[4 * i + 0] = tmp[0][i]; pts[4 * i + 1] = tmp[1][i]; pts[4 * i + 2] = tmp[2][i]; pts[4 * i + 3] = tmp[3][i]; } } void x_p2_raw(PPointF pts, PPointF pts_const) { x_p1_raw(pts, pts_const); for (size_t i = 0; i < 4; i++) { pts[i].x /= 4; pts[i].y /= 4; } } void x_p2_raw_r(PPointF pts, PPointF pts_const) { for (size_t i = 0; i < 4; i++) { pts[i].x *= 4; pts[i].y *= 4; } x_p1_raw_r(pts, pts_const); } void x_p2(PPointF pts) { size_t i; PointF tmp[4][4]; for (i = 0; i < 4; i++) { x_p2_raw(pts + 4 * i, g_pi_const2); } for (i = 0; i < 4; i++) { tmp[0][i] = pts[4 * i + 0]; tmp[1][i] = pts[4 * i + 1]; tmp[2][i] = pts[4 * i + 2]; tmp[3][i] = pts[4 * i + 3]; } for (i = 0; i < 4; i++) { x_p2_raw(&tmp[i][0], g_pi_const2); } for (i = 0; i < 4; i++) { pts[4 * i + 0] = tmp[0][i]; pts[4 * i + 1] = tmp[1][i]; pts[4 * i + 2] = tmp[2][i]; pts[4 * i + 3] = tmp[3][i]; } } void x_p2_r(PPointF pts) { size_t i; PointF tmp[4][4]; for (i = 0; i < 4; i++) { tmp[0][i] = pts[4 * i + 0]; tmp[1][i] = pts[4 * i + 1]; tmp[2][i] = pts[4 * i + 2]; tmp[3][i] = pts[4 * i + 3]; } for (i = 0; i < 4; i++) { x_p2_raw_r(&tmp[i][0], g_pi_const2); } for (i = 0; i < 4; i++) { pts[4 * i + 0] = tmp[0][i]; pts[4 * i + 1] = tmp[1][i]; pts[4 * i + 2] = tmp[2][i]; pts[4 * i + 3] = tmp[3][i]; } for (i = 0; i < 4; i++) { x_p2_raw_r(pts + 4 * i, g_pi_const2); } } void test_part1() { BYTE sn[16] = { 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, }; BYTE map[16] = { 0x00, 0x09, 0x02, 0x0B, 0x0F, 0x04, 0x0D, 0x06, 0x0A, 0x03, 0x08, 0x01, 0x05, 0x0E, 0x07, 0x0C, }; size_t i; BYTE buf[16] = {0}; for (i = 0; i < 16; i++) { buf[map[i]] = sn[i]; } PointF pts[16] = {0}; for (i = 0; i < 16; i++) { pts[i].x = buf[i]; } x_p0(pts, g_pts_const1, 16); x_p1(pts); x_p0(pts, g_pts_const2, 16); x_p2(pts); } BYTE double_to_byte(double v) { BYTE ch = 0; char buf[256] = {0}; sprintf(buf, "%f", v); char *dot = strchr(buf, '.'); if (dot != NULL) { *dot = 0; ch = (BYTE)atoi(buf); } return ch; } string x_get_sn_part1() { int i; PPointF pts = g_part1_expected; x_p2_r(pts); x_p0_r(pts, g_pts_const2, 16); x_p1_r(pts); x_p0_r(pts, g_pts_const1, 16); BYTE r_map[16] = {0}; for (i = 0; i < 16; i++) { BYTE row = (7 * (i / 4) + 2 * (i % 4)) % 4; BYTE col = (i / 4 + i % 4 + 2 * (i / 4)) % 4; r_map[4 * row + col] = i; } BYTE sn[16] = {0}; for (i = 0; i < 16; i++) { //printf("%f\n", pts[i].x); sn[r_map[i]] = double_to_byte(pts[i].x); } return util::bin2hex(sn, 16); } 3. check2(主要是AES256[修改過]解密和xxtea解密), 得到sn後半部分: 68740438FDCC641665D0EA735F2739B3EE7B315A AES程式碼參見: _https://github.com/kokke/tiny-AES-c/blob/master/aes.c
原始xxtea程式碼參見:
_https://github.com/xxtea/xxtea-c (沒改動, 就不貼了)
AES改動的部分
(1) sbox及rsbox;
(2) AddRoundKey之前/之後多了轉置矩陣操作;
(3) ShiftRows移位略做改動;
(4) MixColumns中的矩陣改成了8*8, 其逆矩陣是其自身。
AES解密的迴圈部分程式碼被VM了, 不過不需要跟進去, 下個硬體斷點跟蹤輸入/輸出資料, 就知道在幹嘛了。
aes.cpp #include "stdafx.h" #include <string.h> #include "aes.h" #define Nb 4 #define Nk 8 #define Nr 14 typedef uint8_t state_t[4][4]; static const uint8_t sbox[256] = { 0x03, 0x10, 0xD1, 0xD5, 0xC9, 0x27, 0xC8, 0x68, 0xB3, 0xEF, 0x30, 0xFA, 0x33, 0xB0, 0xCA, 0x51, 0x7E, 0x37, 0x74, 0xF6, 0xC7, 0x4B, 0xE3, 0x0A, 0x36, 0x98, 0x9B, 0xB6, 0xD3, 0x9E, 0x1D, 0x77, 0x9D, 0x46, 0xED, 0x8C, 0xEC, 0xE6, 0xA7, 0x12, 0x92, 0xF4, 0x76, 0xDC, 0xA0, 0x14, 0x24, 0xD9, 0x20, 0x79, 0x59, 0x08, 0x4E, 0xB1, 0x07, 0x90, 0xAA, 0x2E, 0xD7, 0x4F, 0x11, 0xCD, 0xC5, 0x8A, 0xA5, 0x1C, 0x6A, 0x19, 0xC2, 0x66, 0xB4, 0xBC, 0x94, 0xC0, 0x9C, 0x2D, 0xE1, 0x29, 0xE2, 0x15, 0x55, 0x2A, 0x97, 0x81, 0xCF, 0x1A, 0x5A, 0xF5, 0x2C, 0xF3, 0xDD, 0x93, 0xB5, 0x4A, 0xE0, 0x39, 0x57, 0x6F, 0x6E, 0xD6, 0x0C, 0x61, 0xAE, 0x31, 0xFF, 0xBD, 0xCE, 0x35, 0xD2, 0x5C, 0x40, 0x82, 0x17, 0x89, 0x75, 0xE7, 0x7C, 0xA6, 0x32, 0x01, 0x22, 0x78,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x16, 0xCE, 0x95, 0x25, 0x73, 0xD4, 0xBD, 0xCC, 0xE5, 0x24, 0x22, 0xA1, 0x09, 0x88, 0xDA, 0xFB, 0x59, 0x29, 0x57, 0x13, 0xB8, 0xD7, 0x9D, 0x0B, 0x91, 0xED, 0x8A, 0xA0, 0x68 }; static const uint8_t Rcon[11] = { 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; #define getSBoxValue(num) (sbox[(num)]) #define getSBoxInvert(num) (rsbox[(num)]) static void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key) { unsigned i, j, k; uint8_t tempa[4]; for (i = 0; i < Nk; ++i) { RoundKey[(i * 4) + 0] = Key[(i * 4) + 0]; RoundKey[(i * 4) + 1] = Key[(i * 4) + 1]; RoundKey[(i * 4) + 2] = Key[(i * 4) + 2]; RoundKey[(i * 4) + 3] = Key[(i * 4) + 3]; } for (i = Nk; i < Nb * (Nr + 1); ++i) { { k = (i - 1) * 4; tempa[0]=RoundKey[k + 0]; tempa[1]=RoundKey[k + 1]; tempa[2]=RoundKey[k + 2]; tempa[3]=RoundKey[k + 3]; } if (i % Nk == 0) { // This function shifts the 4 bytes in a word to the left once. // [a0,a1,a2,a3] becomes [a1,a2,a3,a0] // Function RotWord() { const uint8_t u8tmp = tempa[0]; tempa[0] = tempa[1]; tempa[1] = tempa[2]; tempa[2] = tempa[3]; tempa[3] = u8tmp; } // SubWord() is a function that takes a four-byte input word and // applies the S-box to each of the four bytes to produce an output word. // Function Subword() { tempa[0] = getSBoxValue(tempa[0]); tempa[1] = getSBoxValue(tempa[1]); tempa[2] = getSBoxValue(tempa[2]); tempa[3] = getSBoxValue(tempa[3]); } tempa[0] = tempa[0] ^ Rcon[i/Nk]; } if (i % Nk == 4) { // Function Subword() { tempa[0] = getSBoxValue(tempa[0]); tempa[1] = getSBoxValue(tempa[1]); tempa[2] = getSBoxValue(tempa[2]); tempa[3] = getSBoxValue(tempa[3]); } } j = i * 4; k=(i - Nk) * 4; RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0]; RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1]; RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2]; RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3]; } } void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key) { KeyExpansion(ctx->RoundKey, key); } static void AddRoundKey(uint8_t round,state_t* state,uint8_t* RoundKey) { uint8_t i,j; for (i = 0; i < 4; ++i) { for (j = 0; j < 4; ++j) { (*state)[i][j] ^= RoundKey[(round * Nb * 4) + (i * Nb) + j]; } } } static void Transpose(state_t* state) { int row, col; state_t tmp; for (row = 0; row < 4; row++) { for (col = 0; col < 4; col++) { tmp[row][col] = (*state)[col][row]; } } memcpy(state, tmp, 16); } static void SubBytes(state_t* state) { uint8_t i, j; for (i = 0; i < 4; ++i) { for (j = 0; j < 4; ++j) { (*state)[j][i] = getSBoxValue((*state)[j][i]); } } } static void InvSubBytes(state_t* state) { uint8_t i, j; for (i = 0; i < 4; ++i) { for (j = 0; j < 4; ++j) { (*state)[j][i] = getSBoxInvert((*state)[j][i]); } } } static void ShiftRows(state_t* state) { uint8_t temp1; uint8_t temp2; // col1: down 2 temp1 = (*state)[2][1]; temp2 = (*state)[3][1]; (*state)[2][1] = (*state)[0][1]; (*state)[3][1] = (*state)[1][1]; (*state)[0][1] = temp1; (*state)[1][1] = temp2; // col2: down 2 temp1 = (*state)[2][2]; temp2 = (*state)[3][2]; (*state)[2][2] = (*state)[0][2]; (*state)[3][2] = (*state)[1][2]; (*state)[0][2] = temp1; (*state)[1][2] = temp2; // col3: down 1 temp1 = (*state)[3][3]; (*state)[3][3] = (*state)[2][3]; (*state)[2][3] = (*state)[1][3]; (*state)[1][3] = (*state)[0][3]; (*state)[0][3] = temp1; } static void InvShiftRows(state_t* state) { uint8_t temp1; uint8_t temp2; // col1: up 2 temp1 = (*state)[0][1]; temp2 = (*state)[1][1]; (*state)[0][1] = (*state)[2][1]; (*state)[1][1] = (*state)[3][1]; (*state)[2][1] = temp1; (*state)[3][1] = temp2; // col2: up 2 temp1 = (*state)[0][2]; temp2 = (*state)[1][2]; (*state)[0][2] = (*state)[2][2]; (*state)[1][2] = (*state)[3][2]; (*state)[2][2] = temp1; (*state)[3][2] = temp2; // col3: up 1 temp1 = (*state)[0][3]; (*state)[0][3] = (*state)[1][3]; (*state)[1][3] = (*state)[2][3]; (*state)[2][3] = (*state)[3][3]; (*state)[3][3] = temp1; } static uint8_t xtime(uint8_t x) { return ((x<<1) ^ (((x>>7) & 1) * 0x1b)); } static uint8_t Multiply(uint8_t x, uint8_t y) { uint8_t v = 0; v ^= (y & 1) * x; v ^= (y>>1 & 1) * xtime(x); v ^= (y>>2 & 1) * xtime(xtime(x)); v ^= (y>>3 & 1) * xtime(xtime(xtime(x))); v ^= (y>>4 & 1) * xtime(xtime(xtime(xtime(x)))); return v; } static void MixColumns(state_t* state) { int i; uint8_t a, b, c, d; uint8_t e, f, g, h; uint8_t tmp[4][4] = {0}; for (i = 0; i < 2; i++) { a = (*state)[0][i+0]; b = (*state)[1][i+0]; c = (*state)[2][i+0]; d = (*state)[3][i+0]; e = (*state)[0][i+2]; f = (*state)[1][i+2]; g = (*state)[2][i+2]; h = (*state)[3][i+2]; tmp[0][i+0] = Multiply(a, 0x0B) ^ Multiply(b, 0x08) ^ Multiply(c, 0x06) ^ Multiply(d, 0x05) ^ Multiply(e, 0x04) ^ Multiply(f, 0x03) ^ Multiply(g, 0x01) ^ Multiply(h, 0x07); tmp[1][i+0] = Multiply(a, 0x08) ^ Multiply(b, 0x0B) ^ Multiply(c, 0x05) ^ Multiply(d, 0x06) ^ Multiply(e, 0x03) ^ Multiply(f, 0x04) ^ Multiply(g, 0x07) ^ Multiply(h, 0x01); tmp[2][i+0] = Multiply(a, 0x06) ^ Multiply(b, 0x05) ^ Multiply(c, 0x0B) ^ Multiply(d, 0x08) ^ Multiply(e, 0x01) ^ Multiply(f, 0x07) ^ Multiply(g, 0x04) ^ Multiply(h, 0x03); tmp[3][i+0] = Multiply(a, 0x05) ^ Multiply(b, 0x06) ^ Multiply(c, 0x08) ^ Multiply(d, 0x0B) ^ Multiply(e, 0x07) ^ Multiply(f, 0x01) ^ Multiply(g, 0x03) ^ Multiply(h, 0x04); tmp[0][i+2] = Multiply(a, 0x04) ^ Multiply(b, 0x03) ^ Multiply(c, 0x01) ^ Multiply(d, 0x07) ^ Multiply(e, 0x0B) ^ Multiply(f, 0x08) ^ Multiply(g, 0x06) ^ Multiply(h, 0x05); tmp[1][i+2] = Multiply(a, 0x03) ^ Multiply(b, 0x04) ^ Multiply(c, 0x07) ^ Multiply(d, 0x01) ^ Multiply(e, 0x08) ^ Multiply(f, 0x0B) ^ Multiply(g, 0x05) ^ Multiply(h, 0x06); tmp[2][i+2] = Multiply(a, 0x01) ^ Multiply(b, 0x07) ^ Multiply(c, 0x04) ^ Multiply(d, 0x03) ^ Multiply(e, 0x06) ^ Multiply(f, 0x05) ^ Multiply(g, 0x0B) ^ Multiply(h, 0x08); tmp[3][i+2] = Multiply(a, 0x07) ^ Multiply(b, 0x01) ^ Multiply(c, 0x03) ^ Multiply(d, 0x04) ^ Multiply(e, 0x05) ^ Multiply(f, 0x06) ^ Multiply(g, 0x08) ^ Multiply(h, 0x0B); } memcpy(state, tmp, 16); } static void Cipher(state_t* state, uint8_t* RoundKey) { uint8_t round = 0; AddRoundKey(0, state, RoundKey); Transpose(state); for (round = 1; round < Nr; ++round) { SubBytes(state); ShiftRows(state); MixColumns(state); Transpose(state); AddRoundKey(round, state, RoundKey); } SubBytes(state); ShiftRows(state); Transpose(state); AddRoundKey(Nr, state, RoundKey); } static void InvCipher(state_t* state,uint8_t* RoundKey) { uint8_t round = 0; AddRoundKey(Nr, state, RoundKey); Transpose(state); for (round = (Nr - 1); round > 0; --round) { InvShiftRows(state); InvSubBytes(state); AddRoundKey(round, state, RoundKey); Transpose(state); MixColumns(state); } InvShiftRows(state); InvSubBytes(state); Transpose(state); AddRoundKey(0, state, RoundKey); } void AES_ECB_encrypt(struct AES_ctx *ctx, uint8_t* buf) { Cipher((state_t*)buf, ctx->RoundKey); } void AES_ECB_decrypt(struct AES_ctx* ctx, uint8_t* buf) { InvCipher((state_t*)buf, ctx->RoundKey); } test.cpp string x_get_sn_part2() { BYTE key[32] = { 0x7E, 0x6E, 0xCF, 0xD4, 0x92, 0xBC, 0x4B, 0x27, 0xC0, 0xCF, 0xF2, 0x65, 0x6D, 0x3C, 0x49, 0xD1, 0xAA, 0xB1, 0x45, 0xA8, 0x6D, 0x75, 0x9B, 0xC1, 0xDF, 0x75, 0x60, 0xDD, 0x8D, 0xDD, 0x13, 0xDA, }; // expected BYTE buf_part2[16] = { 0xA5, 0x4B, 0x4A, 0x9A, 0xC5, 0x49, 0x8A, 0xA2, 0x62, 0xA4, 0xC5, 0x56, 0x2D, 0x52, 0x5A, 0xA6 }; AES_ctx ctx; AES_init_ctx(&ctx, key); AES_ECB_encrypt(&ctx, buf_part2); size_t tmp_len = 0; void *tmp = xxtea_encrypt(buf_part2, 16, "goodLuck7777777", &tmp_len); return util::bin2hex(tmp, tmp_len); }
合作伙伴
騰訊安全應急響應中心
TSRC,騰訊安全的先頭兵,肩負騰訊公司安全漏洞、黑客入侵的發現和處理工作。這是個沒有硝煙的戰場,我們與兩萬多名安全專家並肩而行,捍衛全球億萬使用者的資訊、財產安全。一直以來,我們懷揣感恩之心,努力構建開放的TSRC交流平臺,回饋安全社群。未來,我們將繼續攜手安全行業精英,探索網際網路安全新方向,建設網際網路生態安全,共鑄“網際網路+”新時代。
轉載請註明:轉自看雪學院
看雪CTF.TSRC 2018 團隊賽 解題思路彙總:
相關文章
- 看雪CTF.TSRC 2018 團隊賽 第二題 『半加器』 解題思路2018-12-23
- 看雪CTF.TSRC 2018 團隊賽 第九題『諜戰』 解題思路2018-12-19
- 看雪CTF.TSRC 2018 團隊賽 第七題 『魔法森林』 解題思路2018-12-23
- 看雪CTF.TSRC 2018 團隊賽 第一題 『初世紀』 解題思路2018-12-23
- 看雪CTF.TSRC 2018 團隊賽 第五題 『交響曲』 解題思路2018-12-23
- 看雪CTF.TSRC 2018 團隊賽 第六題 『追凶者也』 解題思路2018-12-23
- 看雪CTF.TSRC 2018 團隊賽 第十題『俠義雙雄』 解題思路2018-12-21
- 看雪CTF.TSRC 2018 團隊賽 第三題 『七十二疑冢』 解題思路2018-12-23
- 看雪CTF.TSRC 2018 團隊賽 第四題 『盜夢空間』 解題思路2018-12-23
- 看雪CTF.TSRC 2018 團隊賽 第十一題『伊甸園』 解題思路2018-12-23
- 看雪CTF.TSRC 2018 團隊賽 第十四題『 你眼中的世界』 解題思路2018-12-29
- 看雪CTF.TSRC 2018 團隊賽 第十五題『 密碼風雲』 解題思路2019-01-02密碼
- 看雪CTF.TSRC 2018 團隊賽 第十二題『移動迷宮』 解題思路2018-12-25
- 看雪CTF.TSRC 2018 團隊賽 第十三題『 機器人歷險記』 解題思路2018-12-27機器人
- 看雪CTF.TSRC 2018 團隊賽 獲獎名單公示2019-01-02
- 看雪·深信服 2021 KCTF 春季賽 | 第八題設計思路及解析2021-05-25
- 看雪.紐盾 KCTF 2019 Q3 | 第八題點評及解題思路2019-10-08
- 看雪.紐盾 KCTF 2019 Q2 | 第八題點評及解題思路2019-07-03
- 看雪·深信服 2021 KCTF 春季賽 | 第二題設計思路及解析2021-05-12
- 看雪.紐盾 KCTF 2019 Q2 | 第二題點評及解題思路2019-07-01
- 看雪·眾安 2021 KCTF 秋季賽 | 第十題設計思路及解析2021-12-16
- 看雪·眾安 2021 KCTF 秋季賽 | 第九題設計思路及解析2021-12-09
- 看雪·眾安 2021 KCTF 秋季賽 | 第七題設計思路及解析2021-12-03
- 看雪·眾安 2021 KCTF 秋季賽 | 第六題設計思路及解析2021-12-01
- 看雪·眾安 2021 KCTF 秋季賽 | 第五題設計思路及解析2021-11-29
- 看雪·眾安 2021 KCTF 秋季賽 | 第四題設計思路及解析2021-11-25
- 看雪·眾安 2021 KCTF 秋季賽 | 第三題設計思路及解析2021-11-22
- 看雪·深信服 2021 KCTF 春季賽 | 第十題設計思路及解析2021-05-31
- 看雪·深信服 2021 KCTF 春季賽 | 第七題設計思路及解析2021-05-25
- 看雪·深信服 2021 KCTF 春季賽 | 第九題設計思路及解析2021-05-28
- 看雪·深信服 2021 KCTF 春季賽 | 第六題設計思路及解析2021-05-21
- 看雪·深信服 2021 KCTF 春季賽 | 第三題設計思路及解析2021-05-14
- 看雪·深信服 2021 KCTF 春季賽 | 第四題設計思路及解析2021-05-17
- 看雪·深信服 2021 KCTF 春季賽 | 第五題設計思路及解析2021-05-17
- 看雪·眾安 2021 KCTF 秋季賽 | 第十一題設計思路及解析2021-12-15
- 看雪.紐盾 KCTF 2019 Q3 | 第四題點評及解題思路2019-09-29
- 看雪.紐盾 KCTF 2019 Q3 | 第七題點評及解題思路2019-09-30
- 看雪.紐盾 KCTF 2019 Q3 | 第一題點評及解題思路2019-09-25