parent
fc46c63445
commit
4832530dfb
@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
BasedOnStyle: Microsoft
|
||||||
|
AlignAfterOpenBracket: Align
|
||||||
|
AllowAllArgumentsOnNextLine: 'true'
|
||||||
|
AllowAllParametersOfDeclarationOnNextLine: 'true'
|
||||||
|
AllowShortIfStatementsOnASingleLine: Never
|
||||||
|
BreakBeforeBraces: Allman
|
||||||
|
IndentWidth: '4'
|
||||||
|
Language: Cpp
|
||||||
|
NamespaceIndentation: All
|
||||||
|
SpacesInAngles: 'true'
|
||||||
|
SpacesInCStyleCastParentheses: 'true'
|
||||||
|
SpacesInContainerLiterals: 'true'
|
||||||
|
SpacesInParentheses: 'true'
|
||||||
|
SpacesInSquareBrackets: 'true'
|
||||||
|
UseTab: Never
|
||||||
|
|
||||||
|
...
|
@ -1,238 +1,213 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <map>
|
|
||||||
#include <Zydis/Zydis.h>
|
#include <Zydis/Zydis.h>
|
||||||
#include <stdexcept>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <map>
|
||||||
|
#include <stdexcept>
|
||||||
#include <vmutils.h>
|
#include <vmutils.h>
|
||||||
|
|
||||||
namespace vm
|
namespace vm
|
||||||
{
|
{
|
||||||
namespace transform
|
namespace transform
|
||||||
{
|
{
|
||||||
// taken from ida...
|
// taken from ida...
|
||||||
template<class T> inline T __ROL__(T value, int count)
|
template < class T > inline T __ROL__( T value, int count )
|
||||||
{
|
{
|
||||||
const unsigned int nbits = sizeof(T) * 8;
|
const unsigned int nbits = sizeof( T ) * 8;
|
||||||
|
|
||||||
if (count > 0)
|
if ( count > 0 )
|
||||||
{
|
{
|
||||||
count %= nbits;
|
count %= nbits;
|
||||||
T high = value >> (nbits - count);
|
T high = value >> ( nbits - count );
|
||||||
if (T(-1) < 0) // signed value
|
if ( T( -1 ) < 0 ) // signed value
|
||||||
high &= ~((T(-1) << count));
|
high &= ~( ( T( -1 ) << count ) );
|
||||||
value <<= count;
|
value <<= count;
|
||||||
value |= high;
|
value |= high;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
count = -count % nbits;
|
count = -count % nbits;
|
||||||
T low = value << (nbits - count);
|
T low = value << ( nbits - count );
|
||||||
value >>= count;
|
value >>= count;
|
||||||
value |= low;
|
value |= low;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// taken from ida...
|
// taken from ida...
|
||||||
inline u8 __ROL1__(u8 value, int count) { return __ROL__((u8)value, count); }
|
inline u8 __ROL1__( u8 value, int count )
|
||||||
inline u16 __ROL2__(u16 value, int count) { return __ROL__((u16)value, count); }
|
{
|
||||||
inline u32 __ROL4__(u32 value, int count) { return __ROL__((u32)value, count); }
|
return __ROL__( ( u8 )value, count );
|
||||||
inline u64 __ROL8__(u64 value, int count) { return __ROL__((u64)value, count); }
|
}
|
||||||
inline u8 __ROR1__(u8 value, int count) { return __ROL__((u8)value, -count); }
|
inline u16 __ROL2__( u16 value, int count )
|
||||||
inline u16 __ROR2__(u16 value, int count) { return __ROL__((u16)value, -count); }
|
{
|
||||||
inline u32 __ROR4__(u32 value, int count) { return __ROL__((u32)value, -count); }
|
return __ROL__( ( u16 )value, count );
|
||||||
inline u64 __ROR8__(u64 value, int count) { return __ROL__((u64)value, -count); }
|
}
|
||||||
|
inline u32 __ROL4__( u32 value, int count )
|
||||||
template <typename T>
|
{
|
||||||
using transform_t = std::function<T(T, T)>;
|
return __ROL__( ( u32 )value, count );
|
||||||
|
}
|
||||||
enum class type
|
inline u64 __ROL8__( u64 value, int count )
|
||||||
{
|
{
|
||||||
generic0,
|
return __ROL__( ( u64 )value, count );
|
||||||
rolling_key,
|
}
|
||||||
generic1,
|
inline u8 __ROR1__( u8 value, int count )
|
||||||
generic2,
|
{
|
||||||
generic3,
|
return __ROL__( ( u8 )value, -count );
|
||||||
update_key
|
}
|
||||||
};
|
inline u16 __ROR2__( u16 value, int count )
|
||||||
|
{
|
||||||
using map_t = std::map<transform::type, zydis_decoded_instr_t>;
|
return __ROL__( ( u16 )value, -count );
|
||||||
|
}
|
||||||
template <class T>
|
inline u32 __ROR4__( u32 value, int count )
|
||||||
inline const auto _bswap = [](T a, T b) -> T
|
{
|
||||||
{
|
return __ROL__( ( u32 )value, -count );
|
||||||
if constexpr (std::is_same_v<T, std::uint64_t>)
|
}
|
||||||
return _byteswap_uint64(a);
|
inline u64 __ROR8__( u64 value, int count )
|
||||||
if constexpr (std::is_same_v<T, std::uint32_t>)
|
{
|
||||||
return _byteswap_ulong(a);
|
return __ROL__( ( u64 )value, -count );
|
||||||
if constexpr (std::is_same_v<T, std::uint16_t>)
|
}
|
||||||
return _byteswap_ushort(a);
|
|
||||||
|
template < typename T > using transform_t = std::function< T( T, T ) >;
|
||||||
throw std::invalid_argument("invalid type size...");
|
|
||||||
};
|
enum class type
|
||||||
|
{
|
||||||
template <class T>
|
generic0,
|
||||||
inline const auto _add = [](T a, T b) -> T
|
rolling_key,
|
||||||
{
|
generic1,
|
||||||
return a + b;
|
generic2,
|
||||||
};
|
generic3,
|
||||||
|
update_key
|
||||||
template <class T>
|
};
|
||||||
inline const auto _xor = [](T a, T b) -> T
|
|
||||||
{
|
using map_t = std::map< transform::type, zydis_decoded_instr_t >;
|
||||||
return a ^ b;
|
|
||||||
};
|
template < class T >
|
||||||
|
inline const auto _bswap = []( T a, T b ) -> T {
|
||||||
template <class T>
|
if constexpr ( std::is_same_v< T, std::uint64_t > )
|
||||||
inline const auto _sub = [](T a, T b) -> T
|
return _byteswap_uint64( a );
|
||||||
{
|
if constexpr ( std::is_same_v< T, std::uint32_t > )
|
||||||
return a - b;
|
return _byteswap_ulong( a );
|
||||||
};
|
if constexpr ( std::is_same_v< T, std::uint16_t > )
|
||||||
|
return _byteswap_ushort( a );
|
||||||
template <class T>
|
|
||||||
inline const auto _neg = [](T a, T b) -> T
|
throw std::invalid_argument( "invalid type size..." );
|
||||||
{
|
};
|
||||||
return a * -1;
|
|
||||||
};
|
template < class T > inline const auto _add = []( T a, T b ) -> T { return a + b; };
|
||||||
|
|
||||||
template <class T>
|
template < class T > inline const auto _xor = []( T a, T b ) -> T { return a ^ b; };
|
||||||
inline const auto _not = [](T a, T b) -> T
|
|
||||||
{
|
template < class T > inline const auto _sub = []( T a, T b ) -> T { return a - b; };
|
||||||
return ~a;
|
|
||||||
};
|
template < class T > inline const auto _neg = []( T a, T b ) -> T { return a * -1; };
|
||||||
|
|
||||||
template <class T>
|
template < class T > inline const auto _not = []( T a, T b ) -> T { return ~a; };
|
||||||
inline const auto _ror = [](T a, T b) -> T
|
|
||||||
{
|
template < class T >
|
||||||
if constexpr (std::is_same_v<T, std::uint64_t>)
|
inline const auto _ror = []( T a, T b ) -> T {
|
||||||
return __ROR8__(a, b);
|
if constexpr ( std::is_same_v< T, std::uint64_t > )
|
||||||
if constexpr (std::is_same_v<T, std::uint32_t>)
|
return __ROR8__( a, b );
|
||||||
return __ROR4__(a, b);
|
if constexpr ( std::is_same_v< T, std::uint32_t > )
|
||||||
if constexpr (std::is_same_v<T, std::uint16_t>)
|
return __ROR4__( a, b );
|
||||||
return __ROR2__(a, b);
|
if constexpr ( std::is_same_v< T, std::uint16_t > )
|
||||||
if constexpr (std::is_same_v <T, std::uint8_t>)
|
return __ROR2__( a, b );
|
||||||
return __ROR1__(a, b);
|
if constexpr ( std::is_same_v< T, std::uint8_t > )
|
||||||
|
return __ROR1__( a, b );
|
||||||
throw std::invalid_argument("invalid type size...");
|
|
||||||
};
|
throw std::invalid_argument( "invalid type size..." );
|
||||||
|
};
|
||||||
template <class T>
|
|
||||||
inline const auto _rol = [](T a, T b) -> T
|
template < class T >
|
||||||
{
|
inline const auto _rol = []( T a, T b ) -> T {
|
||||||
if constexpr (std::is_same_v<T, std::uint64_t>)
|
if constexpr ( std::is_same_v< T, std::uint64_t > )
|
||||||
return __ROL8__(a, b);
|
return __ROL8__( a, b );
|
||||||
if constexpr (std::is_same_v<T, std::uint32_t>)
|
if constexpr ( std::is_same_v< T, std::uint32_t > )
|
||||||
return __ROL4__(a, b);
|
return __ROL4__( a, b );
|
||||||
if constexpr (std::is_same_v<T, std::uint16_t>)
|
if constexpr ( std::is_same_v< T, std::uint16_t > )
|
||||||
return __ROL2__(a, b);
|
return __ROL2__( a, b );
|
||||||
if constexpr (std::is_same_v <T, std::uint8_t>)
|
if constexpr ( std::is_same_v< T, std::uint8_t > )
|
||||||
return __ROL1__(a, b);
|
return __ROL1__( a, b );
|
||||||
|
|
||||||
throw std::invalid_argument("invalid type size...");
|
throw std::invalid_argument( "invalid type size..." );
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T>
|
template < class T > inline const auto _inc = []( T a, T b ) -> T { return a + 1; };
|
||||||
inline const auto _inc = [](T a, T b) -> T
|
|
||||||
{
|
template < class T > inline const auto _dec = []( T a, T b ) -> T { return a - 1; };
|
||||||
return a + 1;
|
|
||||||
};
|
template < class T >
|
||||||
|
inline std::map< ZydisMnemonic, transform_t< T > > transforms = {
|
||||||
template <class T>
|
{ ZYDIS_MNEMONIC_ADD, _add< T > }, { ZYDIS_MNEMONIC_XOR, _xor< T > }, { ZYDIS_MNEMONIC_BSWAP, _bswap< T > },
|
||||||
inline const auto _dec = [](T a, T b) -> T
|
{ ZYDIS_MNEMONIC_SUB, _sub< T > }, { ZYDIS_MNEMONIC_NEG, _neg< T > }, { ZYDIS_MNEMONIC_NOT, _not< T > },
|
||||||
{
|
{ ZYDIS_MNEMONIC_ROR, _ror< T > }, { ZYDIS_MNEMONIC_ROL, _rol< T > }, { ZYDIS_MNEMONIC_INC, _inc< T > },
|
||||||
return a - 1;
|
{ ZYDIS_MNEMONIC_DEC, _dec< T > } };
|
||||||
};
|
|
||||||
|
inline std::map< ZydisMnemonic, ZydisMnemonic > inverse = {
|
||||||
template <class T>
|
{ ZYDIS_MNEMONIC_ADD, ZYDIS_MNEMONIC_SUB }, { ZYDIS_MNEMONIC_XOR, ZYDIS_MNEMONIC_XOR },
|
||||||
inline std::map<ZydisMnemonic, transform_t<T>> transforms =
|
{ ZYDIS_MNEMONIC_BSWAP, ZYDIS_MNEMONIC_BSWAP }, { ZYDIS_MNEMONIC_SUB, ZYDIS_MNEMONIC_ADD },
|
||||||
{
|
{ ZYDIS_MNEMONIC_NEG, ZYDIS_MNEMONIC_NEG }, { ZYDIS_MNEMONIC_NOT, ZYDIS_MNEMONIC_NOT },
|
||||||
{ ZYDIS_MNEMONIC_ADD, _add<T> },
|
{ ZYDIS_MNEMONIC_ROR, ZYDIS_MNEMONIC_ROL }, { ZYDIS_MNEMONIC_ROL, ZYDIS_MNEMONIC_ROR },
|
||||||
{ ZYDIS_MNEMONIC_XOR, _xor<T> },
|
{ ZYDIS_MNEMONIC_INC, ZYDIS_MNEMONIC_DEC }, { ZYDIS_MNEMONIC_DEC, ZYDIS_MNEMONIC_INC } };
|
||||||
{ ZYDIS_MNEMONIC_BSWAP, _bswap<T> },
|
|
||||||
{ ZYDIS_MNEMONIC_SUB, _sub<T>},
|
inline void inverse_transforms( transform::map_t &transforms, transform::map_t &inverse )
|
||||||
{ ZYDIS_MNEMONIC_NEG, _neg<T>},
|
{
|
||||||
{ ZYDIS_MNEMONIC_NOT, _not<T>},
|
inverse[ transform::type::generic0 ] = transforms[ transform::type::generic0 ];
|
||||||
{ ZYDIS_MNEMONIC_ROR, _ror<T>},
|
inverse[ transform::type::generic0 ].mnemonic =
|
||||||
{ ZYDIS_MNEMONIC_ROL, _rol<T>},
|
transform::inverse[ transforms[ transform::type::generic0 ].mnemonic ];
|
||||||
{ ZYDIS_MNEMONIC_INC, _inc<T>},
|
|
||||||
{ ZYDIS_MNEMONIC_DEC, _dec<T>}
|
inverse[ transform::type::rolling_key ] = transforms[ transform::type::rolling_key ];
|
||||||
};
|
inverse[ transform::type::rolling_key ].mnemonic =
|
||||||
|
transform::inverse[ transforms[ transform::type::rolling_key ].mnemonic ];
|
||||||
inline std::map<ZydisMnemonic, ZydisMnemonic> inverse =
|
|
||||||
{
|
inverse[ transform::type::generic1 ] = transforms[ transform::type::generic1 ];
|
||||||
{ZYDIS_MNEMONIC_ADD, ZYDIS_MNEMONIC_SUB},
|
inverse[ transform::type::generic1 ].mnemonic =
|
||||||
{ZYDIS_MNEMONIC_XOR, ZYDIS_MNEMONIC_XOR},
|
transform::inverse[ transforms[ transform::type::generic1 ].mnemonic ];
|
||||||
{ZYDIS_MNEMONIC_BSWAP, ZYDIS_MNEMONIC_BSWAP},
|
|
||||||
{ZYDIS_MNEMONIC_SUB, ZYDIS_MNEMONIC_ADD},
|
inverse[ transform::type::generic2 ] = transforms[ transform::type::generic2 ];
|
||||||
{ZYDIS_MNEMONIC_NEG, ZYDIS_MNEMONIC_NEG},
|
inverse[ transform::type::generic2 ].mnemonic =
|
||||||
{ZYDIS_MNEMONIC_NOT, ZYDIS_MNEMONIC_NOT},
|
transform::inverse[ transforms[ transform::type::generic2 ].mnemonic ];
|
||||||
{ZYDIS_MNEMONIC_ROR, ZYDIS_MNEMONIC_ROL},
|
|
||||||
{ZYDIS_MNEMONIC_ROL, ZYDIS_MNEMONIC_ROR},
|
inverse[ transform::type::generic3 ] = transforms[ transform::type::generic3 ];
|
||||||
{ZYDIS_MNEMONIC_INC, ZYDIS_MNEMONIC_DEC},
|
inverse[ transform::type::generic3 ].mnemonic =
|
||||||
{ZYDIS_MNEMONIC_DEC, ZYDIS_MNEMONIC_INC}
|
transform::inverse[ transforms[ transform::type::generic3 ].mnemonic ];
|
||||||
};
|
|
||||||
|
inverse[ transform::type::update_key ] = transforms[ transform::type::update_key ];
|
||||||
inline void inverse_transforms(transform::map_t& transforms, transform::map_t& inverse)
|
inverse[ transform::type::update_key ].mnemonic =
|
||||||
{
|
transform::inverse[ transforms[ transform::type::update_key ].mnemonic ];
|
||||||
inverse[transform::type::generic0] = transforms[transform::type::generic0];
|
}
|
||||||
inverse[transform::type::generic0].mnemonic =
|
|
||||||
transform::inverse[transforms[transform::type::generic0].mnemonic];
|
inline auto inverse_transform( std::vector< zydis_decoded_instr_t > &instrs ) -> bool
|
||||||
|
{
|
||||||
inverse[transform::type::rolling_key] = transforms[transform::type::rolling_key];
|
for ( auto idx = 0u; idx < instrs.size() - 1; ++idx )
|
||||||
inverse[transform::type::rolling_key].mnemonic =
|
if ( !( instrs[ idx ].mnemonic = inverse[ instrs[ idx ].mnemonic ] ) )
|
||||||
transform::inverse[transforms[transform::type::rolling_key].mnemonic];
|
return false;
|
||||||
|
|
||||||
inverse[transform::type::generic1] = transforms[transform::type::generic1];
|
return true;
|
||||||
inverse[transform::type::generic1].mnemonic =
|
}
|
||||||
transform::inverse[transforms[transform::type::generic1].mnemonic];
|
|
||||||
|
// max size of a and b is 64 bits, a and b is then converted to
|
||||||
inverse[transform::type::generic2] = transforms[transform::type::generic2];
|
// the number of bits in bitsize, the transformation is applied,
|
||||||
inverse[transform::type::generic2].mnemonic =
|
// finally the result is converted back to 64bits... zero extended...
|
||||||
transform::inverse[transforms[transform::type::generic2].mnemonic];
|
inline auto apply( std::uint8_t bitsize, ZydisMnemonic op, std::uint64_t a, std::uint64_t b ) -> std::uint64_t
|
||||||
|
{
|
||||||
inverse[transform::type::generic3] = transforms[transform::type::generic3];
|
switch ( bitsize )
|
||||||
inverse[transform::type::generic3].mnemonic =
|
{
|
||||||
transform::inverse[transforms[transform::type::generic3].mnemonic];
|
case 8:
|
||||||
|
return transforms< std::uint8_t >[ op ]( a, b );
|
||||||
inverse[transform::type::update_key] = transforms[transform::type::update_key];
|
case 16:
|
||||||
inverse[transform::type::update_key].mnemonic =
|
return transforms< std::uint16_t >[ op ]( a, b );
|
||||||
transform::inverse[transforms[transform::type::update_key].mnemonic];
|
case 32:
|
||||||
}
|
return transforms< std::uint32_t >[ op ]( a, b );
|
||||||
|
case 64:
|
||||||
inline auto inverse_transform(std::vector<zydis_decoded_instr_t>& instrs) -> bool
|
return transforms< std::uint64_t >[ op ]( a, b );
|
||||||
{
|
default:
|
||||||
for (auto idx = 0u; idx < instrs.size() - 1; ++idx)
|
throw std::invalid_argument( "invalid bit size..." );
|
||||||
if (!(instrs[idx].mnemonic = inverse[instrs[idx].mnemonic]))
|
}
|
||||||
return false;
|
}
|
||||||
|
|
||||||
return true;
|
inline bool has_imm( zydis_decoded_instr_t *instr )
|
||||||
}
|
{
|
||||||
|
return instr->operand_count > 1 && ( instr->operands[ 1 ].type & ZYDIS_OPERAND_TYPE_IMMEDIATE );
|
||||||
// max size of a and b is 64 bits, a and b is then converted to
|
}
|
||||||
// the number of bits in bitsize, the transformation is applied,
|
} // namespace transform
|
||||||
// finally the result is converted back to 64bits... zero extended...
|
} // namespace vm
|
||||||
inline auto apply(std::uint8_t bitsize, ZydisMnemonic op,
|
|
||||||
std::uint64_t a, std::uint64_t b) -> std::uint64_t
|
|
||||||
{
|
|
||||||
switch (bitsize)
|
|
||||||
{
|
|
||||||
case 8:
|
|
||||||
return transforms<std::uint8_t>[op](a, b);
|
|
||||||
case 16:
|
|
||||||
return transforms<std::uint16_t>[op](a, b);
|
|
||||||
case 32:
|
|
||||||
return transforms<std::uint32_t>[op](a, b);
|
|
||||||
case 64:
|
|
||||||
return transforms<std::uint64_t>[op](a, b);
|
|
||||||
default:
|
|
||||||
throw std::invalid_argument("invalid bit size...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool has_imm(zydis_decoded_instr_t* instr)
|
|
||||||
{
|
|
||||||
return instr->operand_count > 1 &&
|
|
||||||
(instr->operands[1].type & ZYDIS_OPERAND_TYPE_IMMEDIATE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue