├── LICENSE ├── README.md └── XorStr.h /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 ReaP 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XORString 2 | Easy to include string and wstring obfuscation 3 | 4 | # Usage 5 | ```cpp 6 | LONG main( 7 | VOID 8 | ) 9 | { 10 | printf( _XOR_( "Hello World" ) ); 11 | printf( _XOR_( "%ws" ), _XOR_( L"Hello World" ) ); 12 | } 13 | ``` 14 | 15 | # Assembly Output 16 | ```asm 17 | .text:0000000140001090 mov [rsp-8+arg_8], rbx 18 | .text:0000000140001095 mov [rsp-8+arg_10], rsi 19 | .text:000000014000109A mov [rsp-8+arg_18], rdi 20 | .text:000000014000109F push rbp 21 | .text:00000001400010A0 mov rbp, rsp 22 | .text:00000001400010A3 sub rsp, 50h 23 | .text:00000001400010A7 mov [rbp+_Format], 88h ; 'ˆ' 24 | .text:00000001400010AB xor ebx, ebx 25 | .text:00000001400010AD mov [rbp+var_2F], 0A0h ; ' ' 26 | .text:00000001400010B1 mov rsi, 0A734021017465BC8h 27 | .text:00000001400010BB mov [rbp+var_2E], 0ADh ; '­' 28 | .text:00000001400010BF mov r11, rsi 29 | .text:00000001400010C2 mov [rbp+var_2D], 0A2h ; '¢' 30 | .text:00000001400010C6 mov r10d, ebx 31 | .text:00000001400010C9 mov [rbp+var_2C], 0A5h ; '¥' 32 | .text:00000001400010CD lea edi, [rbx+0Fh] 33 | .text:00000001400010D0 mov [rbp+var_2B], 0E9h ; 'é' 34 | .text:00000001400010D4 mov [rbp+var_2A], 9Eh ; 'ž' 35 | .text:00000001400010D8 mov [rbp+var_29], 0A1h ; '¡' 36 | .text:00000001400010DC mov [rbp+var_28], 0A2h ; '¢' 37 | .text:00000001400010E0 mov [rbp+var_27], 0BCh ; '¼' 38 | .text:00000001400010E4 mov [rbp+var_26], 0B4h ; '´' 39 | .text:00000001400010E8 mov [rbp+var_25], 0D3h ; 'Ó' 40 | .text:00000001400010EC movzx eax, [rbp+_Format] 41 | .text:00000001400010F0 lea rax, [rbp+_Format] 42 | .text:00000001400010F4 sub r11, rax 43 | .text:00000001400010F7 nop word ptr [rax+rax+00000000h] 44 | .text:0000000140001100 45 | .text:0000000140001100 loc_140001100: ; CODE XREF: main+A9↓j 46 | .text:0000000140001100 mov rax, r10 47 | .text:0000000140001103 lea r9, [rbp+_Format] 48 | .text:0000000140001107 add r9, r10 49 | .text:000000014000110A and eax, edi 50 | .text:000000014000110C cmp rax, rdi 51 | .text:000000014000110F mov rdx, rsi 52 | .text:0000000140001112 cmovnb rax, rdi 53 | .text:0000000140001116 inc r10 54 | .text:0000000140001119 movzx r8d, byte ptr [r9] 55 | .text:000000014000111D shl al, 2 56 | .text:0000000140001120 movzx ecx, al 57 | .text:0000000140001123 lea eax, [r11+r9] 58 | .text:0000000140001127 shr rdx, cl 59 | .text:000000014000112A and dl, dil 60 | .text:000000014000112D xor dl, al 61 | .text:000000014000112F xor dl, r8b 62 | .text:0000000140001132 mov [r9], dl 63 | .text:0000000140001135 cmp r10, 0Ch 64 | .text:0000000140001139 jb short loc_140001100 65 | .text:000000014000113B lea rcx, [rbp+_Format] ; _Format 66 | .text:000000014000113F call printf 67 | .text:0000000140001144 mov ecx, 5BA2h 68 | .text:0000000140001149 mov eax, 5B88h 69 | .text:000000014000114E mov [rbp+var_20], ax 70 | .text:0000000140001152 mov r9, rbx 71 | .text:0000000140001155 mov eax, 5BA0h 72 | .text:000000014000115A mov [rbp+var_1E], ax 73 | .text:000000014000115E mov eax, 5BADh 74 | .text:0000000140001163 mov [rbp+var_1C], ax 75 | .text:0000000140001167 lea r10d, [rcx+26h] 76 | .text:000000014000116B mov [rbp+var_1A], cx 77 | .text:000000014000116F mov eax, 5BA5h 78 | .text:0000000140001174 mov [rbp+var_18], ax 79 | .text:0000000140001178 mov eax, 5BE9h 80 | .text:000000014000117D mov [rbp+var_16], ax 81 | .text:0000000140001181 mov eax, 5B9Eh 82 | .text:0000000140001186 mov [rbp+var_14], ax 83 | .text:000000014000118A mov eax, 5BA1h 84 | .text:000000014000118F mov [rbp+var_12], ax 85 | .text:0000000140001193 mov eax, 5BBCh 86 | .text:0000000140001198 mov [rbp+var_10], cx 87 | .text:000000014000119C mov [rbp+var_E], ax 88 | .text:00000001400011A0 mov eax, 5BB4h 89 | .text:00000001400011A5 mov [rbp+var_C], ax 90 | .text:00000001400011A9 mov eax, 5BD3h 91 | .text:00000001400011AE mov [rbp+var_A], ax 92 | .text:00000001400011B2 movzx eax, [rbp+var_20] 93 | .text:00000001400011B6 db 66h, 66h 94 | .text:00000001400011B6 nop word ptr [rax+rax+00000000h] 95 | .text:00000001400011C0 96 | .text:00000001400011C0 loc_1400011C0: ; CODE XREF: main+169↓j 97 | .text:00000001400011C0 movzx r8d, [rbp+r9*2+var_20] 98 | .text:00000001400011C6 mov rax, r9 99 | .text:00000001400011C9 and eax, edi 100 | .text:00000001400011CB mov rdx, rsi 101 | .text:00000001400011CE cmp rax, rdi 102 | .text:00000001400011D1 cmovnb rax, rdi 103 | .text:00000001400011D5 shl al, 2 104 | .text:00000001400011D8 movzx ecx, al 105 | .text:00000001400011DB lea eax, [r10+r9] 106 | .text:00000001400011DF shr rdx, cl 107 | .text:00000001400011E2 and dx, di 108 | .text:00000001400011E5 xor dx, ax 109 | .text:00000001400011E8 xor dx, r8w 110 | .text:00000001400011EC mov [rbp+r9*2+var_20], dx 111 | .text:00000001400011F2 inc r9 112 | .text:00000001400011F5 cmp r9, 0Ch 113 | .text:00000001400011F9 jb short loc_1400011C0 114 | .text:00000001400011FB mov [rbp+arg_0], 0B5h ; 'µ' 115 | .text:00000001400011FF mov r11, 0F26FFD4189B4CF98h 116 | .text:0000000140001209 mov [rbp+arg_1], 0E7h ; 'ç' 117 | .text:000000014000120D mov r10, r11 118 | .text:0000000140001210 mov [rbp+arg_2], 0E6h ; 'æ' 119 | .text:0000000140001214 mov [rbp+arg_3], 97h ; '—' 120 | .text:0000000140001218 movzx eax, [rbp+arg_0] 121 | .text:000000014000121C lea rax, [rbp+arg_0] 122 | .text:0000000140001220 sub r10, rax 123 | .text:0000000140001223 nop dword ptr [rax+00h] 124 | .text:0000000140001227 nop word ptr [rax+rax+00000000h] 125 | .text:0000000140001230 126 | .text:0000000140001230 loc_140001230: ; CODE XREF: main+1D9↓j 127 | .text:0000000140001230 mov rax, rbx 128 | .text:0000000140001233 lea r9, [rbp+arg_0] 129 | .text:0000000140001237 add r9, rbx 130 | .text:000000014000123A and eax, edi 131 | .text:000000014000123C cmp rax, rdi 132 | .text:000000014000123F mov rdx, r11 133 | .text:0000000140001242 cmovnb rax, rdi 134 | .text:0000000140001246 inc rbx 135 | .text:0000000140001249 movzx r8d, byte ptr [r9] 136 | .text:000000014000124D shl al, 2 137 | .text:0000000140001250 movzx ecx, al 138 | .text:0000000140001253 lea eax, [r10+r9] 139 | .text:0000000140001257 shr rdx, cl 140 | .text:000000014000125A and dl, dil 141 | .text:000000014000125D xor dl, al 142 | .text:000000014000125F xor dl, r8b 143 | .text:0000000140001262 mov [r9], dl 144 | .text:0000000140001265 cmp rbx, 4 145 | .text:0000000140001269 jb short loc_140001230 146 | .text:000000014000126B lea rdx, [rbp+var_20] 147 | .text:000000014000126F lea rcx, [rbp+arg_0] ; _Format 148 | .text:0000000140001273 call printf 149 | .text:0000000140001278 mov rbx, [rsp+50h+arg_8] 150 | .text:000000014000127D xor eax, eax 151 | .text:000000014000127F mov rsi, [rsp+50h+arg_10] 152 | .text:0000000140001284 mov rdi, [rsp+50h+arg_18] 153 | .text:0000000140001289 add rsp, 50h 154 | .text:000000014000128D pop rbp 155 | .text:000000014000128E retn 156 | ``` 157 | -------------------------------------------------------------------------------- /XorStr.h: -------------------------------------------------------------------------------- 1 | #ifndef XORSTR_H 2 | #define XORSTR_H 3 | 4 | /* 5 | * MIT License 6 | * 7 | * Copyright (c) 2022 ReaP 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in all 17 | * copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | */ 27 | 28 | #include 29 | 30 | #define XORSTR_INLINE __forceinline 31 | #define XORSTR_NOINLINE __declspec( noinline ) 32 | #define XORSTR_CONST constexpr 33 | #define XORSTR_VOLATILE volatile 34 | 35 | #define XORSTR_CONST_INLINE \ 36 | XORSTR_INLINE XORSTR_CONST 37 | 38 | #define XORSTR_CONST_NOINLINE \ 39 | XORSTR_NOINLINE XORSTR_CONST 40 | 41 | #define XORSTR_FNV_OFFSET_BASIS 0xCBF29CE484222325 42 | #define XORSTR_FNV_PRIME 0x100000001B3 43 | 44 | #define XORSTR_TYPE_SIZEOF( _VALUE ) \ 45 | sizeof( decltype( _VALUE ) ) 46 | 47 | #define XORSTR_BYTE( _VALUE, _IDX ) \ 48 | ( ( _VALUE >> ( __min( _IDX, ( XORSTR_TYPE_SIZEOF( _VALUE ) ) - 1) * 8 ) ) & 0xFF ) 49 | 50 | #define XORSTR_NIBBLE( _VALUE, _IDX ) \ 51 | ( ( _VALUE >> ( __min( _IDX, ( XORSTR_TYPE_SIZEOF( _VALUE ) * 2 ) - 1 ) * 4 ) ) & 0xF ) 52 | 53 | #define XORSTR_MAKE_INTEGER_SEQUENCE( _LEN_ ) \ 54 | __make_integer_seq< XORSTR_INT_SEQ, SIZE_T, _LEN_ >( ) 55 | 56 | #define XORSTR_INTEGER_SEQUENCE( _INDICES_ ) \ 57 | XORSTR_INT_SEQ< SIZE_T, _INDICES_... > 58 | 59 | template< typename _Ty, _Ty... Types > 60 | struct XORSTR_INT_SEQ 61 | { 62 | }; 63 | 64 | /** 65 | * @brief A Lazy compile-time ATOI used for the XORSTR_KEY function 66 | * @param [in] Character: The character to convert to a number 67 | * @return The number 68 | */ 69 | XORSTR_CONST_NOINLINE 70 | INT XORSTR_ATOI8( 71 | IN CHAR Character 72 | ) noexcept 73 | { 74 | return ( Character >= '0' && Character <= '9' ) ? 75 | ( Character - '0' ) : NULL; 76 | } 77 | 78 | /** 79 | * @brief A compile-time function that generates a 64-bit XOR key that'll be used for a string 80 | * @param [in] CryptStrLength: The length of the string for added randomization 81 | * @return The 64-bit XOR key 82 | */ 83 | XORSTR_CONST_NOINLINE 84 | UINT64 XORSTR_KEY( 85 | IN SIZE_T CryptStrLength 86 | ) noexcept 87 | { 88 | UINT64 KeyHash = XORSTR_FNV_OFFSET_BASIS; 89 | 90 | for ( SIZE_T i = NULL; i < sizeof( __TIME__ ); i++ ) { 91 | KeyHash = KeyHash ^ ( XORSTR_ATOI8( __TIME__[ i ] ) + ( CryptStrLength * i ) ) & 0xFF; 92 | KeyHash = KeyHash * XORSTR_FNV_PRIME; 93 | } 94 | 95 | return KeyHash; 96 | } 97 | 98 | template< typename _CHAR_TYPE_, 99 | SIZE_T _STR_LENGTH_ > 100 | class _XORSTR_ 101 | { 102 | public: 103 | XORSTR_CONST_INLINE _XORSTR_( 104 | IN _CHAR_TYPE_ CONST( &String )[ _STR_LENGTH_ ] 105 | ) : _XORSTR_( String, XORSTR_MAKE_INTEGER_SEQUENCE( _STR_LENGTH_ ) ) 106 | { 107 | } 108 | 109 | /** 110 | * @brief Decrypts the encrypted characters and returns the string 111 | * @param NONE 112 | * @return The decrypted string data 113 | */ 114 | XORSTR_INLINE 115 | CONST _CHAR_TYPE_* String( 116 | VOID 117 | ) 118 | { 119 | for ( SIZE_T i = NULL; i < _STR_LENGTH_; i++ ) { 120 | StringData[ i ] = CRYPT_CHAR( StringData[ i ], i ); 121 | } 122 | 123 | return ( _CHAR_TYPE_* )( StringData ); 124 | } 125 | 126 | private: 127 | 128 | /** 129 | * @brief A unique compile-time generated XOR key used for the CRYPT_CHAR function 130 | */ 131 | static XORSTR_CONST UINT64 Key = XORSTR_KEY( _STR_LENGTH_ ); 132 | 133 | /** 134 | * @brief Encrypts the character using the generated XOR key 135 | * @param [in] Character: The character to encrypt 136 | * @param [in] KeyIndex: The shifted index of the key to use for the encryption( nibble ) 137 | * @return The encrypted character 138 | */ 139 | static XORSTR_CONST_INLINE 140 | _CHAR_TYPE_ CRYPT_CHAR( 141 | IN _CHAR_TYPE_ Character, 142 | IN SIZE_T KeyIndex 143 | ) 144 | { 145 | return ( Character ^ ( ( Key + KeyIndex ) ^ 146 | ( XORSTR_NIBBLE( Key, KeyIndex % 16 ) ) ) ); 147 | } 148 | 149 | template< SIZE_T... _INDEX_ > 150 | XORSTR_CONST_INLINE _XORSTR_( 151 | IN _CHAR_TYPE_ CONST( &String )[ _STR_LENGTH_ ], 152 | IN XORSTR_INTEGER_SEQUENCE( _INDEX_ ) IntSeq 153 | ) : StringData{ CRYPT_CHAR( String[ _INDEX_ ], _INDEX_ )... } 154 | { 155 | } 156 | 157 | XORSTR_VOLATILE _CHAR_TYPE_ StringData[ _STR_LENGTH_ ]; 158 | }; 159 | 160 | /** 161 | * @brief Encrypt a regular ANSI string at compile-time 162 | * @param [in] String: The string literal to encrypt 163 | * @return The _XORSTR_ object that can be used to decrypt the string 164 | */ 165 | template< SIZE_T _STR_LEN_ > 166 | XORSTR_CONST_INLINE 167 | _XORSTR_< CHAR, _STR_LEN_ > XorStr( 168 | IN CHAR CONST( &String )[ _STR_LEN_ ] 169 | ) 170 | { 171 | return _XORSTR_< CHAR, _STR_LEN_ >( String ); 172 | } 173 | 174 | /** 175 | * @brief Encrypt a regular ASCII string at compile-time 176 | * @param [in] String: The string literal to encrypt 177 | * @return The _XORSTR_ object that can be used to decrypt the string 178 | */ 179 | template< SIZE_T _STR_LEN_ > 180 | XORSTR_CONST_INLINE 181 | _XORSTR_< WCHAR, _STR_LEN_ > XorStr( 182 | IN WCHAR CONST( &String )[ _STR_LEN_ ] 183 | ) 184 | { 185 | return _XORSTR_< WCHAR, _STR_LEN_ >( String ); 186 | } 187 | 188 | /** 189 | * @brief Encrypt a CHAR32 string at compile-time 190 | * @param [in] String: The string literal to encrypt 191 | * @return The _XORSTR_ object that can be used to decrypt the string 192 | */ 193 | template< SIZE_T _STR_LEN_ > 194 | XORSTR_CONST_INLINE 195 | _XORSTR_< char32_t, _STR_LEN_ > XorStr( 196 | IN char32_t CONST( &String )[ _STR_LEN_ ] 197 | ) 198 | { 199 | return _XORSTR_< char32_t, _STR_LEN_ >( String ); 200 | } 201 | 202 | /** 203 | * @brief Encrypt a string at compile-time and automatically decrypt when used at runtime 204 | * @param [in] _STR_: The string literal to encrypt 205 | * @return The decrypted string at runtime 206 | */ 207 | #define _XOR_( _STR_ ) XorStr( _STR_ ).String( ) 208 | 209 | #endif --------------------------------------------------------------------------------