From f03d6ac30ef9c4d25358ae71f3727c26d09450ed Mon Sep 17 00:00:00 2001 From: Alexej Harm Date: Sun, 15 Oct 2017 19:38:40 +0200 Subject: [PATCH] first commit --- LICENSE | 28 ++++++++++ include/xorstr.h | 122 ++++++++++++++++++++++++++++++++++++++++++++ readme.md | 29 +++++++++++ xorstr-config.cmake | 5 ++ 4 files changed, 184 insertions(+) create mode 100644 LICENSE create mode 100644 include/xorstr.h create mode 100644 readme.md create mode 100644 xorstr-config.cmake diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ad882ec --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +Copyright (c) 2010-2014, Sebastien Andrivet +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/include/xorstr.h b/include/xorstr.h new file mode 100644 index 0000000..a462d62 --- /dev/null +++ b/include/xorstr.h @@ -0,0 +1,122 @@ +// Copyright (c) 2010-2014, Sebastien Andrivet +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from this +// software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#pragma once +#include +#include +#include +#include + +namespace xorstr_impl { + +#ifdef _MSC_VER +#define XORSTR_INLINE __forceinline +#else +#define XORSTR_INLINE inline +#endif + +constexpr auto time = __TIME__; +constexpr auto seed = static_cast(time[7]) + + static_cast(time[6]) * 10 + + static_cast(time[4]) * 60 + + static_cast(time[3]) * 600 + + static_cast(time[1]) * 3600 + + static_cast(time[0]) * 36000; + +// 1988, Stephen Park and Keith Miller +// "Random Number Generators: Good Ones Are Hard To Find", considered as "minimal standard" +// Park-Miller 31 bit pseudo-random number generator, implemented with G. Carta's optimisation: +// with 32-bit math and without division + +template +struct random_generator { +private: + static constexpr unsigned a = 16807; // 7^5 + static constexpr unsigned m = 2147483647; // 2^31 - 1 + static constexpr unsigned s = random_generator::value; + static constexpr unsigned lo = a * (s & 0xFFFF); // multiply lower 16 bits by 16807 + static constexpr unsigned hi = a * (s >> 16); // multiply higher 16 bits by 16807 + static constexpr unsigned lo2 = lo + ((hi & 0x7FFF) << 16); // combine lower 15 bits of hi with lo's upper bits + static constexpr unsigned hi2 = hi >> 15; // discard lower 15 bits of hi + static constexpr unsigned lo3 = lo2 + hi; + +public: + static constexpr unsigned max = m; + static constexpr unsigned value = lo3 > m ? lo3 - m : lo3; +}; + +template <> +struct random_generator<0> { + static constexpr unsigned value = seed; +}; + +template +struct random_int { + static constexpr auto value = random_generator::value % M; +}; + +template +struct random_char { + static const char value = static_cast(1 + random_int::value); +}; + +template +struct string { +private: + const char key_; + std::array encrypted_; + + constexpr char enc(char c) const { + return c ^ key_; + } + + char dec(char c) const { + return c ^ key_; + } + +public: + template + constexpr XORSTR_INLINE string(const char* str, std::index_sequence) : + key_(random_char::value), encrypted_{ { enc(str[Is])... } } {} + + XORSTR_INLINE decltype(auto) decrypt() { + for (size_t i = 0; i < N; ++i) { + encrypted_[i] = dec(encrypted_[i]); + } + encrypted_[N] = '\0'; + return encrypted_.data(); + } +}; + +#undef XORSTR_INLINE + +} // namespace xorstr_impl + +#define xorstr(s) (xorstr_impl::string(s, std::make_index_sequence()).decrypt()) diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..28ecd96 --- /dev/null +++ b/readme.md @@ -0,0 +1,29 @@ +# XorStr +A simple constexpr string literal obfuscator. The algorythm was taken from [ADVobfuscator][ADVobfuscator].
+This technique does not hide strings from sophisticated static analysis tools and memory inspection. + +## Usage +Copy the file to your project and include it in the source code. + +```cpp +#include +#include + +int main() { + const std::string str = xorstr("xorstr_test_verify_error"); + std::cout << str << std::endl; +} +``` + +You can verify that the string `xorstr_test_verify_error` does not exist in **Release** builds: + +```sh +~/workspace/xorstr_test strings build/llvm/release/main | grep xorstr_test_verify_error +~/workspace/xorstr_test strings build/llvm/debug/main | grep xorstr_test_verify_error +xorstr_test_verify_error +~/workspace/xorstr_test strings build/msvc/Release/main.exe | grep xorstr_test_verify_error +~/workspace/xorstr_test strings build/msvc/Debug/main.exe | grep xorstr_test_verify_error +xorstr_test_verify_error +``` + +[ADVobfuscator]: https://github.com/andrivet/ADVobfuscator/blob/master/ADVobfuscator/MetaRandom.h diff --git a/xorstr-config.cmake b/xorstr-config.cmake new file mode 100644 index 0000000..6e0bf84 --- /dev/null +++ b/xorstr-config.cmake @@ -0,0 +1,5 @@ +if(NOT TARGET xorstr) + add_library(xorstr INTERFACE IMPORTED) + set_target_properties(xorstr PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_DIR}/include") +endif()