溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么使用c++實現異或加密

發(fā)布時間:2022-04-12 10:39:20 來源:億速云 閱讀:358 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹“怎么使用c++實現異或加密”,在日常操作中,相信很多人在怎么使用c++實現異或加密問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用c++實現異或加密”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

加密原理

由于展示最基本最簡單的實現,使用算法加密就沒用復雜的。如果使用比較復雜的加密,首先你在C++代碼層面和匯編層面要有配套的代碼,C++負責加密,匯編負責自我解密,否則你加密完了,結果加密后的PE文件自己又解密不了,這就很尷尬。

在所有加密算法,異或加密是最簡單的,也是最好是實現的。我們來介紹異或加密的原理。

已知兩個數AB,如果A xor B = C,則C xor B = A,其中xor表示異或運算符。如果不理解,這個是入門編程的最基本的知識,請自行補缺,這里我就不嘮叨了。

異或加密的實現

下面是我們實現異或加密的相關函數:

// GNU AFFERO GENERAL PUBLIC LICENSE
//Version 3, 19 November 2007
//Copyright(C) 2007 Free Software Foundation, Inc.
//Everyone is permitted to copyand distribute verbatim copies
//of this license document, but changing it is not allowed.
// Author : WingSummer (寂靜的羽夏)
//Warning: You can not use it for any commerical use,except you get 
// my AUTHORIZED FORM ME!This project is used for tutorial to teach
// the beginners what is the PE structure and how the packer of the PE files works.
BOOL CWingProtect::XORCodeSection(BOOL NeedReloc, BOOL FakeCode)
{
    using namespace asmjit;
    if (_lasterror != ParserError::Success) return FALSE;
    auto filesize = peinfo.FileSize.QuadPart;
    CodeHolder holder;
    /// <summary>
    /// PointerToRawData
    /// </summary>    auto p = peinfo.PCodeSection->PointerToRawData;
    /// <summary>
    /// SizeOfRawData
    /// </summary>
    auto sizecode = peinfo.PCodeSection->SizeOfRawData;
    auto repeat = sizecode;
    BYTE* shellcode;
    INT3264 ccount;
    if (is64bit)
    {
        Environment env(Arch::kX64);
        holder.init(env);
        x86::Assembler a(&holder);
        Label loop = a.newLabel();
        x86::Mem mem;
        mem.setSegment(x86::gs);
        mem.setOffset(0x60);
        //生成加密 shellcode,此處的 rax = ImageBase
        a.push(x86::rcx);
        a.push(x86::rdi);
        //xor 解密
        a.mov(x86::rax, mem);
        a.mov(x86::rax, x86::qword_ptr(x86::rax, 0x10));
        a.mov(x86::rdi, x86::rax);
        a.add(x86::rdi, peinfo.PCodeSection->VirtualAddress);
        a.mov(x86::rcx, repeat);
        a.bind(loop);
        if (FakeCode) FakeProtect(a);
        a.xor_(x86::byte_ptr(x86::rdi), 0x55);
        a.inc(x86::rdi);
        a.dec(x86::rcx);
        a.test(x86::rcx, x86::rcx);
        a.jnz(loop);
        //確保此時 rax 或 eax 存放的是 ImageBase ,否則是未定義行為
        if (NeedReloc)
            RelocationSection(a);
        a.pop(x86::rdi);
        a.pop(x86::rcx);
        a.ret();
        shellcode = a.bufferData();
        ccount = holder.codeSize();
    }
    else
    {
        Environment env(Arch::kX86);
        holder.init(env);
        x86::Assembler a(&holder);
        Label loop = a.newLabel();
        x86::Mem mem;
        mem.setSegment(x86::fs);
        mem.setOffset(0x30);
        //生成加密 shellcode
        a.push(x86::ecx);
        a.push(x86::edi);
        a.mov(x86::eax, mem);
        a.mov(x86::eax, x86::dword_ptr(x86::eax, 0x8));
        a.mov(x86::edi, x86::eax);
        a.add(x86::edi, peinfo.PCodeSection->VirtualAddress);
        a.mov(x86::ecx, repeat);
        a.bind(loop);
        if (FakeCode) FakeProtect(a);
        a.xor_(x86::byte_ptr(x86::edi), 0x55);
        a.inc(x86::edi);
        a.dec(x86::ecx);
        a.test(x86::ecx, x86::ecx);
        a.jnz(loop);
        //確保此時 rax 或 eax 存放的是 ImageBase ,否則是未定義行為
        if (NeedReloc)
            RelocationSection(a);
        a.pop(x86::edi);
        a.pop(x86::ecx);
        a.ret();
        shellcode = a.bufferData();
        ccount = holder.codeSize();
    }
    //異或加密
    auto se = (BYTE*)b;
    for (UINT i = 0; i < repeat; i++)
    {
        se[i] ^= (BYTE)0x55;
    }
    //加密完畢,寫 Shellcode
    encryptInfo.XORDecodeShellCode = (UINT)peinfo.PointerOfWingSeciton;
    auto ws = GetPointerByOffset(peinfo.WingSecitonBuffer, peinfo.PointerOfWingSeciton);
    memcpy_s(ws, ccount, shellcode, ccount);
    peinfo.PointerOfWingSeciton += ccount;
    if (!NeedReloc)
    {
        auto tmp = (PIMAGE_SECTION_HEADER)TranModPEWapper(peinfo.PCodeSection);
        tmp->Characteristics |= IMAGE_SCN_MEM_WRITE;
    }
    return TRUE;
}

在C++代碼層面,加密代碼區(qū)內容相關的代碼如下:

//異或加密
auto se = (BYTE*)b;
for (UINT i = 0; i < repeat; i++)
{
    se[i] ^= (BYTE)0x55;
}

^表示異或運算符,在匯編層面,以64位為例,實現如下所示:

a.mov(x86::rax, mem);
a.mov(x86::rax, x86::qword_ptr(x86::rax, 0x10));
a.mov(x86::rdi, x86::rax);
a.add(x86::rdi, peinfo.PCodeSection->VirtualAddress);
a.mov(x86::rcx, repeat);
a.bind(loop);
if (FakeCode) FakeProtect(a);
a.xor_(x86::byte_ptr(x86::rdi), 0x55);
a.inc(x86::rdi);
a.dec(x86::rcx);
a.test(x86::rcx, x86::rcx);
a.jnz(loop);

可以看出來匯編寫起來比寫C++代碼麻煩多了,里面有一些代碼可能有一些其他的考慮,我們這里說一下:

首先是FakeProtect,這個就是生成花指令,這里不多說,后面在介紹。還有一個函數比較注意RelocationSection,這個函數是用來生成做重定位的匯編代碼的,為什么要有這個函數呢?

比如我只有異或加密,我們是在硬編碼的層面進行的加密,PE被加載進入的時候如果基址不和預想的那樣,就會查是否有重定位表,如果有的話就解析并修復。但是,我們的代碼是加密的,而重定位表沒做修改,它就會錯誤的把被加密的硬編碼進行重定位,這個是不能夠允許的。所以我們需要摧毀重定位表,可以看到CWingProtect::Proctect里面有一個函數DestoryRelocation,這個作用就是用來銷毀它的,不讓PE加載器幫我們做重定位。

綜上所述,我們需要自己做重定位,我們需要在匯編層面來實現重定位表的修復,我們來看一下相關代碼:

//
// GNU AFFERO GENERAL PUBLIC LICENSE
//Version 3, 19 November 2007
//
//Copyright(C) 2007 Free Software Foundation, Inc.
//Everyone is permitted to copyand distribute verbatim copies
//of this license document, but changing it is not allowed.
// Author : WingSummer (寂靜的羽夏)
// 
//Warning: You can not use it for any commerical use,except you get 
// my AUTHORIZED FORM ME!This project is used for tutorial to teach
// the beginners what is the PE structure and how the packer of the PE files works.

void CWingProtect::RelocationSection(asmjit::x86::Assembler& a)
{
    using namespace asmjit;

    Label loop_xor = a.newLabel();
    Label loop_reloc = a.newLabel();
    Label loop_rt = a.newLabel();
    Label endproc = a.newLabel();
    auto rdd = peinfo.PDataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
    if (is64bit)
    {
        a.nop();
        a.push(x86::rdi);
        a.push(x86::rcx);
        a.push(x86::rsi);        //征用 rsi
        a.mov(x86::rsi, rdd.VirtualAddress);    //重定位表基址
        a.add(x86::rsi, x86::rax);
        a.push(x86::rdx);    //征用 rdx
        a.push(x86::r10);
        a.mov(x86::r10, peinfo.ImageBase);    //PE 加載后,該值會被重定位,只能寫死
        a.sub(x86::r10, x86::rax);
        a.jz(endproc);

        a.bind(loop_rt);
        a.mov(x86::edi, x86::dword_ptr(x86::rsi));        //偏移基址地址
        a.add(x86::rdi, x86::rax);        //此時 rdi 為加載到內存的虛擬基址地址
        //計數
        a.mov(x86::ecx, x86::dword_ptr(x86::rsi, 4));
        a.sub(x86::ecx, 8);
        a.shr(x86::ecx, 1);    //此時為重定位表的真實項目個數
        a.add(x86::rsi, 8);    //將指針指向該索引下的第一個重定位項目
        a.bind(loop_reloc);
        a.dec(x86::rcx);
        a.mov(x86::dx, x86::word_ptr(x86::rsi, x86::rcx, 1));
        a.test(x86::dx, 0xF000);
        a.jz(loop_reloc);        //contine;
        a.and_(x86::edx, 0xFFF);
        a.add(x86::rdx, x86::rdi);
        a.sub(x86::qword_ptr(x86::rdx), x86::r10);    //修正
        a.cmp(x86::rcx, 0);
        a.ja(loop_reloc);
        a.sub(x86::rsi, 8);    //重新指向表頭
        a.mov(x86::edx, x86::dword_ptr(x86::rsi, 4));
        a.add(x86::rsi, x86::rdx);        //指向下一個
        a.mov(x86::edx, x86::dword_ptr(x86::rsi));
        a.test(x86::edx, x86::edx);
        a.jnz(loop_rt);
        a.bind(endproc);
        a.pop(x86::r10);
        a.pop(x86::rdx);
        a.pop(x86::rsi);    //釋放 rsi 自由身
        a.pop(x86::rcx);
        a.pop(x86::rdi);
    }
    else
    {
        a.push(x86::edi);
        a.push(x86::ecx);
        a.push(x86::esi);        //征用 rsi
        a.mov(x86::esi, rdd.VirtualAddress);    //重定位表基址
        a.add(x86::esi, x86::eax);
        a.push(x86::edx);    //征用 edx
        a.push((DWORD32)peinfo.ImageBase);    //x86寄存器沒那么多,只能自己維護一個局部變量
        a.sub(x86::dword_ptr(x86::esp), x86::rax);
        a.jz(endproc);

        a.bind(loop_rt);
        a.mov(x86::edi, x86::dword_ptr(x86::esi));        //偏移基址地址
        a.add(x86::edi, x86::eax);        //此時 rdi 為加載到內存的虛擬基址地址
        //計數
        a.mov(x86::ecx, x86::dword_ptr(x86::esi, 4));
        a.sub(x86::ecx, 8);
        a.shr(x86::ecx, 1);    //此時為重定位表的真實項目個數
        a.add(x86::esi, 8);    //將指針指向該索引下的第一個重定位項目
        a.bind(loop_reloc);
        a.dec(x86::ecx);
        a.mov(x86::dx, x86::word_ptr(x86::rsi, x86::ecx, 1));
        a.test(x86::dx, 0xF000);
        a.jz(loop_reloc);        //contine;
        a.and_(x86::edx, 0xFFF);
        a.add(x86::edx, x86::edi);
        a.push(x86::eax);    //使用局部變量
        a.mov(x86::eax, x86::dword_ptr(x86::esp, 4));    //注意被 push 了一個,所以加個偏移
        a.sub(x86::dword_ptr(x86::edx), x86::eax);    //修正
        a.pop(x86::eax);
        a.cmp(x86::ecx, 0);
        a.ja(loop_reloc);
        a.sub(x86::esi, 8);    //重新指向表頭
        a.mov(x86::edx, x86::dword_ptr(x86::esi, 4));
        a.add(x86::esi, x86::rdx);        //指向下一個
        a.mov(x86::edx, x86::dword_ptr(x86::esi));
        a.test(x86::edx, x86::edx);
        a.jnz(loop_rt);
        a.bind(endproc);
        a.add(x86::esp, 4);        //釋放局部變量
        a.pop(x86::edx);
        a.pop(x86::esi);    //釋放 rsi 自由身
        a.pop(x86::ecx);
        a.pop(x86::edi);
    }
    //將所有的節(jié)全部改為可寫
    auto length = peinfo.NumberOfSections;
    for (UINT i = 0; i < length; i++)
    {
        ((PIMAGE_SECTION_HEADER)TranModPEWapper(&peinfo.PSectionHeaders[i]))
            ->Characteristics |= IMAGE_SCN_MEM_WRITE;
    }
}

對于以上代碼你可能有一些疑問,我這里說一下:
為什么調用a.nop()來生成沒有用的指令,這個是我用來方便調試我生成的ShellCode用的,否則會生成一大坨匯編到后來自己也不清楚自己在調試啥的,通過這個nop我就可以清楚的直到我到那里了,如果出錯的話我也方便進行定位。
此函數最后生成完ShellCode之后又將所有的節(jié)全部改為可寫屬性,這是為什么呢?因為線性內存是有屬性的,如果我沒有將其設置可寫,如果它是只讀內存,如果我對它做重定位修改的話,就會報內存訪問錯誤,導致程序崩潰。
怎么用匯編來解析重定位表,這里就不贅述了。

ShellCode 編寫注意事項

在編寫ShellCode代碼的時候,請一定保證如下原則,避免一些麻煩,否則會出現出乎意料的錯誤:

  • 除了 eax / rax 其他寄存器用到的話,一定要注意保存好,因為其它函數調用有各種調用約定,一定不要影響它們,否則會出錯。為什么要對 eax / rax 區(qū)別對待,因為通常來說它只用做返回值,調用函數返回結果一定會修改它,所以大可不必。

  • 在使用 ASMJIT 生成匯編的時候,使用類似 MOV 的指令的時候,一定要注意如果要寫入多大的數據一定要在目標操作數體現數來,比如要移動 WORD 大小的話,用 ax 就不要用 eax,否則它正常生成匯編指令不報錯,結果和你想生成的代碼不一樣。

  • 一定要注意堆棧平衡,這個是非常重要的東西,在64位尤甚,32位的操作系統(tǒng)也是十分注意堆棧平衡的。

到此,關于“怎么使用c++實現異或加密”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

c++
AI