逆向中常見的加密演算法
1.Base64
1) 原理與特徵:
a.原理:將3個byte(即3×8=24bit)切割為4×6,然後根據6bit表示的數字在base64表(64byte的表)尋找對應的值;如果待加密字串長度不為3的整數,則在末尾處補0對齊,其中0對應的字元為'='。
b.特徵:在反彙編程式碼中會出現0x3F,‘=’ 的特殊標識。(某些題目可能會直接在base64加密函式修改而不僅僅是變表-安洵杯2019-crackme
2) 解密指令碼
a.不換表
import base64
encode_str=''
decode_str=base64.b64decode(str0).decode('utf-8')
print(decode_str)
b.換表
import base64
encode_string = ""
string1 = ""#base64變表
string2 = "ABCDEFGIJKLMNOPQRSTUVWXYZabcdefgijklmnopqrstuvwxyz0123456789+/"
decode_str=base64.b64decode(encode_string.translate(str.maketrans(string1,string2)))
print(decode_str)
2.Tea-XTea
1) 原理與特徵
a.原理:tea演算法家族的原理比較簡單(目前博主僅遇到的tea/xtea),魔改tea的型別也比較多,可能是在迴圈移位進行改動,也可能是修改了delta的值
b.特徵:(出自 [HNCTF 2022 WEEK2]TTTTTTTTTea)
a) 存在delta,(上圖delta=0x61C88647)
b) input[i]與input[i+n]進行移位/異或/加減操作
2) 解密指令碼(題目出自 [HNCTF 2022 WEEK2]TTTTTTTTTea
#include <stdio.h>
int main() {
unsigned int enc[] = {0xC11EE75A, 0xA4AD0973, 0xF61C9018, 0x32E37BCD, 0x2DCC1F26, 0x344380CC};
unsigned int key[] = { 0x10203,0x4050607,0x8090A0B,0x0C0D0E0F };
unsigned int delta = 0x61C88647;
for(int j=0;j<6;j+=2)
{
unsigned int v4 = 0 - (32 * 0x61C88647);
for (int i = 0; i < 32; ++i)
{
enc[j + 1] -= (((enc[j] >> 5) ^ (16 * enc[j])) + enc[j]) ^ (key[((v4 >> 11) & 3)] + v4);
v4 += 0x61C88647;
enc[j] -= (((enc[j + 1] >> 5) ^ (16 * enc[j + 1])) + enc[j + 1]) ^ (key[(v4 & 3)] + v4);
}
printf("%lx,%lx,",enc[j],enc[j+1]);
}
printf("\n");
char* ptr = enc;
for (int p = 0; p < 24; p++) {
printf("%c", *(ptr++));
}
}
3.RC4
1) 原理與特徵
a.原理:
a) 先初始化一個長度為256的s盒,同時根據金鑰key初始化一個長度為256的T盒;
b) 根據T盒打亂S盒(重要的步驟是交換s盒元素的位置);
c) 將待加密的資料進行與s盒元素進行元素。
b.特徵:a) 一般出現三個for迴圈,前兩個迴圈進行128次,第三個迴圈32次;
b) 會出現兩個長度為256的陣列,其中一個陣列的初始值一般為0-255。
2) 解密指令碼
rc4是對稱演算法(異或),所以加密指令碼也是解密指令碼
/*初始化函式*/
void rc4_init(unsigned char*s,unsigned char*key, unsigned long Len)
{
int i=0,j=0;
char k[256]={0};
unsigned char tmp=0;
for(i=0;i<256;i++) {
s[i]=i;
k[i]=key[i%Len];
}
for(i=0;i<256;i++) {
j=(j+s[i]+k[i])%256;
tmp=s[i];
s[i]=s[j];//交換s[i]和s[j]
s[j]=tmp;
}
}
/*加解密*/
void rc4_crypt(unsigned char*s,unsigned char*Data,unsigned long Len)
{
int i=0,j=0,t=0;
unsigned long k=0;
unsigned char tmp;
for(k=0;k<Len;k++)
{
i=(i+1)%256;
j=(j+s[i])%256;
tmp=s[i];
s[i]=s[j];//交換s[x]和s[y]
s[j]=tmp;
t=(s[i]+s[j])%256;
Data[k]^=s[t];
}
}
int main()
{
unsigned char s[256] = { 0 }, s2[256] = { 0 };//S-box
char key[256] = { "" }; //金鑰
char pData[512] = ""; //密文
unsigned long len = strlen(pData);
int i;
rc4_init(s, (unsigned char*)key, strlen(key)); //已經完成了初始化
rc4_crypt(s, (unsigned char*)pData, len);
printf("pData=%s\n\n", pData);
return 0;
}
//(c語言原始碼解釋:https://www.cnblogs.com/Moomin/p/15023601.html)
4.AES
1) 原理與特徵
(AES的數學運算太複雜了,可以在b站或者其他部落格研究原理,本部落格側重解密)
【5分鐘搞定AES演算法】https://www.bilibili.com/video/BV1yq4y1X7Kt?vd_source=69ffcd703762aa7a204e6cc6f57ba69d
a.加密過程簡述:金鑰擴充套件--位元組替換--行移位--列混合--輪金鑰加。
a) 先將金鑰轉化為4*4的矩陣進行擴充套件(這裡的擴充套件會進行移位/異或的一系列操作)
b) 位元組替換:AES有一個16×16=256長度的S盒用於替換位元組,可以理解為二維陣列。將原有位元組,例如data=0xab,則替換為s盒中第a行第b列對應的值。(S盒的構造非常麻煩,所以一般是固定的,但也存在魔改AES的情況
c) 行移位:AES的操作基於4*4的矩陣進行,第i行迴圈左移i位元組
d) 列混合:矩陣相乘
e) 輪金鑰加:將資料與金鑰進行異或
b.特徵:
a) AES會進行9次迴圈(準確來說是10次?),且一般在反彙編程式碼中,每個操作以函式的形式存在
b) 金鑰擴充套件存在輪常量:
1000000h, 2000000h, 4000000h, 8000000h, 10000000h, 20000000h, 40000000h, 80000000h, 1B000000h, 36000000h
c) 未魔改的s盒與逆s盒
uint8_t sbox[256] = {
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0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d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
};
b.解密指令碼
a) 魔改S盒求逆S盒
//NKCTF2024-loginsystem
int main() {
int Sbox[16][16] = {
0x31, 0x52, 0x5A, 0xC8, 0x0B, 0xAC, 0xF3, 0x3A, 0x8B, 0x54, 0x27, 0x9B, 0xAB, 0x95, 0xDE, 0x83,
0x60, 0xCB, 0x53, 0x7F, 0xC4, 0xE3, 0x0A, 0x97, 0xE0, 0x29, 0xD5, 0x68, 0xC5, 0xDF, 0xF4, 0x7B,
0xAA, 0xD6, 0x42, 0x78, 0x6C, 0xE9, 0x70, 0x17, 0xD7, 0x37, 0x24, 0x49, 0x75, 0xA9, 0x89, 0x67,
0x03, 0xFA, 0xD9, 0x91, 0xB4, 0x5B, 0xC2, 0x4E, 0x92, 0xFC, 0x46, 0xB1, 0x73, 0x08, 0xC7, 0x74,
0x09, 0xAF, 0xEC, 0xF5, 0x4D, 0x2D, 0xEA, 0xA5, 0xDA, 0xEF, 0xA6, 0x2B, 0x7E, 0x0C, 0x8F, 0xB0,
0x04, 0x06, 0x62, 0x84, 0x15, 0x8E, 0x12, 0x1D, 0x44, 0xC0, 0xE2, 0x38, 0xD4, 0x47, 0x28, 0x45,
0x6E, 0x9D, 0x63, 0xCF, 0xE6, 0x8C, 0x18, 0x82, 0x1B, 0x2C, 0xEE, 0x87, 0x94, 0x10, 0xC1, 0x20,
0x07, 0x4A, 0xA4, 0xEB, 0x77, 0xBC, 0xD3, 0xE1, 0x66, 0x2A, 0x6B, 0xE7, 0x79, 0xCC, 0x86, 0x16,
0xD0, 0xD1, 0x19, 0x55, 0x3C, 0x9F, 0xFB, 0x30, 0x98, 0xBD, 0xB8, 0xF1, 0x9E, 0x61, 0xCD, 0x90,
0xCE, 0x7C, 0x8D, 0x57, 0xAE, 0x6A, 0xB3, 0x3D, 0x76, 0xA7, 0x71, 0x88, 0xA2, 0xBA, 0x4F, 0x3E,
0x40, 0x64, 0x0F, 0x48, 0x21, 0x35, 0x36, 0x2F, 0xE8, 0x14, 0x5D, 0x51, 0xD8, 0xB5, 0xFE, 0xD2,
0x96, 0x93, 0xA1, 0xB6, 0x43, 0x0D, 0x4C, 0x80, 0xC9, 0xFF, 0xA3, 0xDD, 0x72, 0x05, 0x59, 0xBF,
0x0E, 0x26, 0x34, 0x1F, 0x13, 0xE5, 0xDC, 0xF2, 0xC6, 0x50, 0x1E, 0xE4, 0x85, 0xB7, 0x39, 0x8A,
0xCA, 0xED, 0x9C, 0xBB, 0x56, 0x23, 0x1A, 0xF0, 0x32, 0x58, 0xB2, 0x65, 0x33, 0x6F, 0x41, 0xBE,
0x3F, 0x6D, 0x11, 0x00, 0xAD, 0x5F, 0xC3, 0x81, 0x25, 0xA8, 0xA0, 0x9A, 0xF6, 0xF7, 0x5E, 0x99,
0x22, 0x2E, 0x4B, 0xF9, 0x3B, 0x02, 0x7A, 0xB9, 0x5C, 0x69, 0xF8, 0x1C, 0xDB, 0x01, 0x7D, 0xFD
};
//博主用的是自己比較好理解的方式寫的。。其實還有更簡單的指令碼
int InvSbox[16][16] = {0};
printf(" ");
for(int i =0;i<16;i++)
{
for(int j=0;j<16;j++)
{
InvSbox[(Sbox[i][j]) >> 4][(Sbox[i][j]) & 0xf] = (i << 4) + j;
}
}
for (int j = 0; j < 16;j++) {
printf("%.2x ", j);
for (int k = 0; k < 16; k++) {
printf("0x%.2x ", InvSbox[j][k]);
}
printf("\n");
}
}
接著將魔改的S盒匯入以下部落格裡的指令碼就可以解密
https://blog.csdn.net/weixin_45582916/article/details/121429445
b) 未魔改
from Crypto.Cipher import AES
from Crypto.Util.number import *
enc=0xBC0AADC0147C5ECCE0B140BC9C51D52B46B2B9434DE5324BAD7FB4B39CDB4B5B
key=0xcb8d493521b47a4cc1ae7e62229266ce
keyy=long_to_bytes(key)
encc=long_to_bytes(enc)
decrypt_str=AES.new(keyy,mode=AES.MODE_ECB)
flag=decrypt_str.decrypt(encc)
print(flag)
5.SM4
1) 原理與特徵
a. 原理:
a) SM4的輸入與金鑰都是128bit,進行了32輪的迭代運算與1次反序變換,每一輪進行4bit的運算。
(詳細原理請戳:https://www.bilibili.com/video/BV1LS4y167r3?vd_source=69ffcd703762aa7a204e6cc6f57ba69d)
b) 以安洵杯2019-crackme為例
- 這裡有很明顯的輪金鑰擴充套件特徵(使用了findcrypt外掛
- 進入SM4加密,可以看到進行32次迴圈,跟進sub_D91700檢視,發現是輪函式(即加密
b.特徵:
a) 有S盒進行位元組替換
s_box[256]={
0xD6, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6, 0x14, 0xC2, 0x28, 0xFB, 0x2C, 0x05,
0x2B, 0x67, 0x9A, 0x76, 0x2A, 0xBE, 0x04, 0xC3, 0xAA, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99,
0x9C, 0x42, 0x50, 0xF4, 0x91, 0xEF, 0x98, 0x7A, 0x33, 0x54, 0x0B, 0x43, 0xED, 0xCF, 0xAC, 0x62,
0xE4, 0xB3, 0x1C, 0xA9, 0xC9, 0x08, 0xE8, 0x95, 0x80, 0xDF, 0x94, 0xFA, 0x75, 0x8F, 0x3F, 0xA6,
0x47, 0x07, 0xA7, 0xFC, 0xF3, 0x73, 0x17, 0xBA, 0x83, 0x59, 0x3C, 0x19, 0xE6, 0x85, 0x4F, 0xA8,
0x68, 0x6B, 0x81, 0xB2, 0x71, 0x64, 0xDA, 0x8B, 0xF8, 0xEB, 0x0F, 0x4B, 0x70, 0x56, 0x9D, 0x35,
0x1E, 0x24, 0x0E, 0x5E, 0x63, 0x58, 0xD1, 0xA2, 0x25, 0x22, 0x7C, 0x3B, 0x01, 0x21, 0x78, 0x87,
0xD4, 0x00, 0x46, 0x57, 0x9F, 0xD3, 0x27, 0x52, 0x4C, 0x36, 0x02, 0xE7, 0xA0, 0xC4, 0xC8, 0x9E,
0xEA, 0xBF, 0x8A, 0xD2, 0x40, 0xC7, 0x38, 0xB5, 0xA3, 0xF7, 0xF2, 0xCE, 0xF9, 0x61, 0x15, 0xA1,
0xE0, 0xAE, 0x5D, 0xA4, 0x9B, 0x34, 0x1A, 0x55, 0xAD, 0x93, 0x32, 0x30, 0xF5, 0x8C, 0xB1, 0xE3,
0x1D, 0xF6, 0xE2, 0x2E, 0x82, 0x66, 0xCA, 0x60, 0xC0, 0x29, 0x23, 0xAB, 0x0D, 0x53, 0x4E, 0x6F,
0xD5, 0xDB, 0x37, 0x45, 0xDE, 0xFD, 0x8E, 0x2F, 0x03, 0xFF, 0x6A, 0x72, 0x6D, 0x6C, 0x5B, 0x51,
0x8D, 0x1B, 0xAF, 0x92, 0xBB, 0xDD, 0xBC, 0x7F, 0x11, 0xD9, 0x5C, 0x41, 0x1F, 0x10, 0x5A, 0xD8,
0x0A, 0xC1, 0x31, 0x88, 0xA5, 0xCD, 0x7B, 0xBD, 0x2D, 0x74, 0xD0, 0x12, 0xB8, 0xE5, 0xB4, 0xB0,
0x89, 0x69, 0x97, 0x4A, 0x0C, 0x96, 0x77, 0x7E, 0x65, 0xB9, 0xF1, 0x09, 0xC5, 0x6E, 0xC6, 0x84,
0x18, 0xF0, 0x7D, 0xEC, 0x3A, 0xDC, 0x4D, 0x20, 0x79, 0xEE, 0x5F, 0x3E, 0xD7, 0xCB, 0x39, 0x48
}
b) 輪金鑰擴充套件時存在常數CK和FK
CK[32]={
0x00070e15, 0x1c232a31, 0x383f64d, 0x545b6269,
0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9,
0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249,
0x50575e65, 0x6c737a81, 0x88f969d, 0xa4abb2b9,
0xc0c7ced5, 0xdce3eafl, 0xf8ff060d, 0x141b2229,
0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299,
0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209,
0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279
}
FK0=(A3B1BAC6),FK1=(56AA3350),FK2=(677D9197), FK3=(B27022DC)
b.解密指令碼
在github上下載python庫進行解密即可/線上解密/找指令碼
https://github.com/yang3yen/pysm4
>>> from pysm4 import encrypt, decrypt
# 明文
>>> clear_num = 0x0123456789abcdeffedcba9876543210
# 金鑰
>>> mk = 0x0123456789abcdeffedcba9876543210
# 加密
>>> cipher_num = encrypt(clear_num, mk)
>>> hex(cipher_num)[2:].replace('L', '')
'681edf34d206965e86b3e94f536e4246'
# 解密
>>> clear_num == decrypt(cipher_num, mk)
True