HadesMem v2

August 27th, 2010 raptorfactor 3 comments

This is ‘old news’, but other than following development of HadesMem via the SVN there’s no way for people to know what’s going on (unless I make posts like these). Work on HadesMem v2 has started, and is coming along quite nicely. Unfortunately there will be breaking changes, but that’s because the changes I’m making are quite large (they’re worth it though). One of the most noticeable changes being made is that I am restructuring the entire project into smaller modules to make compilation less of a hassle (so you only need to compile the parts you need, rather than the entire thing). I am also making a lot of improvements to the scripting layer, and will be cleaning up the entire API of Hades-Memory.

For now work is focused entirely on the ‘library’ portion of HadesMem (i.e. Hades-Memory, including the scripting layer and the sandbox app), rather than the injected portion. However I will be making improvements to that too in the near future. Eventually I’d also like to write a fully-fledged memory hacking app with a nice GUI (read: A Cheat Engine alternative with full x64 support), however that’s a while off as for now I simply don’t have the time (and the sandbox will provide a lot of similar functionality, albeit in a less convenient format)!

Please note that because I’m restructuring the entire project and working on pieces as I add them, only a portion of the project is currently implemented in the v2 branch. Furthermore, constant breaking changes are being made, so unless you’re a developer I suggest sticking to v1 for production use until v2 is in a more stable state.

I’m still looking for help with the project, so if you’re fluent in modern C++ (read: templates, exceptions, STL, Boost, etc) and know a thing or two about Windows programming, I’d love to have a chat with you and get you on board. It’s a huge job for a single person and having some extra help would certainly speed up development and make working on some of the more ambitious parts of my plans more feasible.

Even if you’re not a developer though (or you are but can’t afford the time investment), things as simple as bug reports, feature suggestions, etc. really do make a difference. I read every report/suggestion/etc. I get, so keep them coming!

Finally, for those of you who may be unfamiliar with the project, it can be found here.

As always, I can be contacted via email at:

raptorfactor [at] raptorfactor [dot] com

P.S. There has been no commits in the past week because it’s been my birthday and I’ve been taking it easy. Development will start up again shortly after the weekend.

Improved Compile-time String Encryption

August 20th, 2010 raptorfactor No comments

In the comments for my previous post on compile-time string encryption I noted the code could be improved by using a more complex algorithm (one using the result of the previous operation as the seed for the current operation). Well, now we can do that!

Proof of concept code provided below. Again, you will need a C++0x compiler which supports variadic templates. Also, another huge thanks to ‘Georg Fritzsche’ on StackOverflow who wrote most of the underlying templates (I got about half-way then got a headache before he beat me. Still getting used to all this TMP stuff.).

EDIT (20100820-171227):

Fixed usage of ‘char’ instead of ‘CharT’. Sorry.

// Improved compile-time string 'encryption' example.
// By RaptorFactor.
// http://www.raptorfactor.com/improved-compile-time-string-encryption/
// Thanks to 'Georg Fritzsche' on StackOverflow for his help.
 
// C++ Standard Library
#include <string>
#include <iostream>
#include <algorithm>
 
namespace CompileCrypt
{
  template <typename CharT>
  CharT DecryptChar(CharT c)
  {
    return (((c - 1) ^ 0x55) ^ 0x12);
  }
 
  template <typename CharT>
  std::basic_string<CharT> DecryptString(CharT const* const pEncrypted,
    std::size_t Length)
  {
    std::basic_string<CharT> Decrypted;
    Decrypted.reserve(Length);
 
    CharT Seed = 0;
    for (auto p = pEncrypted; p != pEncrypted + Length; ++p)
    {
      CharT Cur = DecryptChar<CharT>(*p ^ Seed);
      Decrypted += Cur;
      Seed = *p;
    }
 
    return Decrypted;
  }
 
  template<typename CharT, CharT... P> struct StringBuilder
  {
    template<CharT C> struct AddChar
    {
      typedef StringBuilder<CharT, P..., C> Type;
    };
 
    static const CharT Value[sizeof...(P)+1];
    static std::size_t const Length = sizeof...(P);
  };
 
  template<typename CharT, CharT... P> const CharT StringBuilder<CharT,
    P...>::Value[sizeof...(P)+1] =
  {
    P...
  };
 
  template<typename CharT, class B, CharT...> struct EncryptImpl;
 
  template<typename CharT, class B, CharT Seed, CharT Head, CharT... Tail>
  struct EncryptImpl<CharT, B, Seed, Head, Tail...>
  {
    template <CharT C>
    struct EncryptChar
    {
      static CharT const Value = (((C ^ 0x12) ^ 0x55) + 1);
    };
 
    static const CharT Next = EncryptChar<Head>::Value ^ Seed;
    typedef typename EncryptImpl<CharT, typename B::template AddChar<Next>::
      Type, Next, Tail...>::Type Type;
  };
 
  template<typename CharT, class B, CharT Seed>
  struct EncryptImpl<CharT, B, Seed>
  {
    typedef B Type;
  };
 
  template<typename CharT, CharT... P> struct Encrypt
  {
    typedef typename EncryptImpl<CharT, StringBuilder<CharT>, 0, P...>::Type
      Type;
  };
}
 
// Entry point
int main(int argc, char* argv[])
{
  // Debug output
  std::wcout << "Compile-time encryption test." << std::endl;
 
  // Perform tests
  typedef CompileCrypt::Encrypt<char,'T','e','s','t','i','n','g'>::Type
    TestingEncA;
  std::cout << "'Testing' (Narrow, Encryption): " << TestingEncA::Value <<
    std::endl;
  std::cout << "'Testing' (Narrow, Decryption): " << CompileCrypt::
    DecryptString(TestingEncA::Value, TestingEncA::Length) << std::endl;
  typedef CompileCrypt::Encrypt<wchar_t,L'T',L'e',L's',L't',L'i',L'n',L'g'>::
    Type TestingEncW;
  std::wcout << "'Testing' (Wide, Encryption): " << TestingEncW::Value <<
    std::endl;
  std::wcout << "'Testing' (Wide, Decryption): " << CompileCrypt::
    DecryptString(TestingEncW::Value, TestingEncW::Length) << std::endl;
}

Compile-time String Encryption with C++0x

August 18th, 2010 raptorfactor No comments

Time for more C++0x fun! Because you’re not always going to be able to operate only on hashes of strings (often you need to be able to manipulate your strings at runtime), my compile time string hashing code may be insufficient at times. This is when compile-time string encryption comes into play (Note: This is only a proof of concept so the words ‘encryption’ and ‘decryption’ are used VERY loosely. In this context ‘encryption’ really just means obfuscation, however it’s definitely possible to improve the algorithm to add actual encryption.).  Implemented using variadic templates (I know, again… But they’re so useful!), so you will need a compiler which supports this feature.

Proof of concept code provided below. See the comments for some extra information.

// Compile-time string 'encryption' example.
// By RaptorFactor.
// http://www.raptorfactor.com/compile-time-string-encryption-with-c0x/
// Thanks to 'Motti' and 'Georg Fritzsche' on StackOverflow for their help.
// Note: Using the same algoirthm Guy uses in the macro-based solution he
// posted on GD. (Linked below, split over two lines.)
// http://forum.gamedeception.net/threads/19561-C(-)-compile-time-string-
// encryption-library-no-custom-build-step-needed!
// Note: A better implementation would use 'constexpr', but at the time of
// writing there are no compilers which support this feature.
// Note: A better implementation would be implemented using a more complex
// algorithm (e.g. using the result of the previous operation as the seed
// for the next xor), however I'm unsure how to implement such a solution
// at the moment.
 
// C++ Standard Library
#include <string>
#include <iostream>
#include <algorithm>
 
// Decrypt character
template <typename CharT>
CharT DecryptChar(CharT c)
{
  return (((c - 1) ^ 0x55) ^ 0x12);
}
 
// Decrypt string at runtime
template <typename CharT>
std::basic_string<CharT> DecryptString(CharT const* const pEncrypted,
  std::size_t Length)
{
  std::basic_string<CharT> Decrypted;
  std::transform(pEncrypted, pEncrypted + Length,
    std::back_inserter(Decrypted), DecryptChar<CharT>);
 
  return Decrypted;
}
 
// Encrypt string at compile-time
template <typename T, T... Chars>
struct EncryptChars
{
  // Encrypt single character
  template <T C>
  struct EncryptChar
  {
    static char const Value = (((C ^ 0x12) ^ 0x55) + 1);
  };
 
  // Length of string
  static std::size_t const Length = sizeof...(Chars);
 
  // Encrypted string
  static T const Value[Length + 1];
};
 
// Encrypted string
template <typename T, T... Chars>
T const EncryptChars<T, Chars...>::Value[EncryptChars<T, Chars...>::
  Length + 1] =
{
  EncryptChar<Chars>::Value...
};
 
// Entry point
int main(int argc, char* argv[])
{
  // Debug output
  std::wcout << "Compile-time encryption test." << std::endl;
 
  // Perform tests
  typedef EncryptChars<char, 'T','e','s','t','i','n','g'> TestingEncA;
  std::cout << "'Testing' (Narrow, Encryption): " << TestingEncA::Value <<
    std::endl;
  std::cout << "'Testing' (Narrow, Decryption): " << DecryptString(
    TestingEncA::Value, TestingEncA::Length) << std::endl;
  typedef EncryptChars<wchar_t, L'T',L'e',L's',L't',L'i',L'n',L'g'>
  TestingEncW;
  std::wcout << "'Testing' (Wide, Encryption): " << TestingEncW::Value <<
    std::endl;
  std::wcout << "'Testing' (Wide, Decryption): " << DecryptString(
    TestingEncW::Value, TestingEncW::Length) << std::endl;
}