JustasMasiulis/xorstr
GitHub: JustasMasiulis/xorstr
一个基于 C++17 的高度向量化编译时字符串加密库,用于在编译期对字符串字面量进行加密以规避静态字符串分析。
Stars: 1412 | Forks: 199
## xorstr
一个高度向量化的 C++17 编译时字符串加密库。
# 快速示例
```
int main() {
std::puts(xorstr_("an extra long hello_world"));
}
```
# API
```
// This macro creates an encrypted xor_string string instance.
#define xorstr(string) xor_string<...>{string}
// For convenience sake there is also a macro to instantly decrypt the string
#define xorstr_(string) xorstr(string).crypt_get()
struct xor_string {
using size_type = std::size_t;
using value_type = CharT;
using pointer = value_type*;
using const_pointer = const value_type*;
// Returns string size in characters, not including null terminator.
constexpr size_type size() const;
// Runs the encryption/decryption algorithm on the internal storage.
void crypt() noexcept;
// Returns const pointer to the storage, without doing any modifications to it.
const_pointer get() const;
// Returns non const pointer to the storage, without doing any modifications to it.
pointer get();
// Runs crypt() and returns the pointer to the internal storage.
pointer crypt_get();
}
```
# 值得注意的事项
* 所有密钥均为 64 位,并在编译期间生成。
* 数据块以 16 字节为增量,因此可能会浪费一些空间。
* 代码经过精心设计,所有数据将直接嵌入到代码中,而不存储在 .rdata 等位置。
* 字符串加密和解密的全部过程都将被内联 (inlined)。
# 支持的编译器和平台
经测试,可在 clang 5.0+、gcc 7.1+ 和 MSVC v141 上正常工作。
如果您的 CPU 不支持 AVX,请定义 JM_XORSTR_DISABLE_AVX_INTRINSICS 以仅使用 SSE。
# 示例汇编输出
来自快速示例的 gcc (trunk) 输出
```
main:
movabs rax, -4762152789334367252
push rbp
mov rbp, rsp
and rsp, -32
sub rsp, 64
mov QWORD PTR [rsp], rax
mov rdi, rsp
movabs rax, -6534519754492314190
mov QWORD PTR [rsp+8], rax
movabs rax, -2862143164529545214
mov QWORD PTR [rsp+16], rax
movabs rax, -4140208776682645948
mov QWORD PTR [rsp+24], rax
vmovdqa ymm1, YMMWORD PTR [rsp]
movabs rax, -2550414817236710003
mov QWORD PTR [rsp+32], rax
movabs rax, -4595755740016602734
mov QWORD PTR [rsp+40], rax
movabs rax, -5461194525092864914
mov QWORD PTR [rsp+48], rax
movabs rax, -4140208776682645984
mov QWORD PTR [rsp+56], rax
vpxor ymm0, ymm1, YMMWORD PTR [rsp+32]
vmovdqa YMMWORD PTR [rsp], ymm0
vzeroupper
call puts
xor eax, eax
leave
ret
```
标签:AVX/SSE, C++17, DNS 反向解析, DNS 解析, HTTP头分析, SIMD指令集, XOR加密, 云资产清单, 代码保护, 头部库, 字符串混淆, 模板元编程, 混淆器, 混淆库, 游戏安全, 编译期加密, 逆向工程, 静态分析对抗