Skip to main content

A minimalistic library for doing unsigned integer operations when reversing x86 code.

Project description

ByteOps

A library for doing logical and arithmetical operations based on x86 assembly. It also contains code for the following operations for BYTE, WORD, DWORD, QWORD, XMMWORD, YMMWORD and ZMMWORD:

  • add
  • sub
  • mul
  • and
  • or
  • xor
  • not
  • rol
  • ror
  • shl
  • shr

The library is used for reimplementing string decryption routines in the context of malware analysis. It can save you time debugging where you needed to mask bytes or do modulo operations again and where not. The code can also be used to copy a shift or rotate operation which tends to be ugly in python.

Example:

Pseudocode from IDA

strcpy(vStackString, "nAA:P<;n@XRFa=cXO;>@sxt?avd\x1DJX");
v28 = (char *)v62;
do
{
  vStackString[index] = 0x14 - ((index ^ (vStackString[index] + 0x67)) - 4);
  ++index;
}
while ( index != 31 );
vStackString[20] = 0;

Function in Python:

from byteops import ByteOps

def decode():
    vStackString = bytes([
        0x6E, 0x41, 0x41, 0x3A, 0x50, 0x3C, 0x3B, 0x6E, 0x40, 0x58, 0x52, 0x46, 0x61, 0x3D, 0x63, 0x58,
        0x4F, 0x3B, 0x3E, 0x40, 0x73, 0x78, 0x74, 0x3F, 0x61, 0x76, 0x64, 0x1D, 0x4A, 0x58, 0x00, 0x00
    ])
    result = bytearray(31)
    for index in range(31):
        temp = bytes([vStackString[index]])
        index_bytes = index.to_bytes(1, 'little')

        # (vStackString[index] + 0x67)
        add_result = ByteOps.add_byte(temp, bytes.fromhex("67"))

        # (index ^ (vStackString[index] + 0x67))
        xor_result = ByteOps.xor_byte(index_bytes, add_result)

        # ((index ^ (vStackString[index] + 0x67)) - 4)
        sub_result = ByteOps.sub_byte(xor_result, bytes.fromhex("04"))

        # 0x14 - ((index ^ (vStackString[index] + 0x67)) - 4)
        final_result = ByteOps.sub_byte(bytes.fromhex("14"), sub_result)

        # first and also only byte to the result store
        if index < 20:
            result[index] = final_result[0]

    print("".join(chr(i) for i in result))

decode()

Tests

The operations are tested with static values and a randomized test case where the implemented instructions are tested against the Python implementation of the operation (mostly, the original operators). The original result is masked, and it is expected to be identical to the "custom" implementation for e.g. add or sub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

byteops-0.1.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

byteops-0.1.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file byteops-0.1.0.tar.gz.

File metadata

  • Download URL: byteops-0.1.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for byteops-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bfb0177679e55015b159d8db607b24bcaaac95d84d0bfa9843bd9ca2152c4c42
MD5 daa4afc26d7177d7bf2f3c80f4e58e81
BLAKE2b-256 a63d98bfa28d4ea676a12eb26db2459202998e1fe301349c1d17d61850c687c6

See more details on using hashes here.

File details

Details for the file byteops-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: byteops-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for byteops-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 18dd0b48137bbf16b668b1d3c3e86a07afffa17044246aabc52a483f044b81f9
MD5 200e280b533bac5cf39560d425035005
BLAKE2b-256 b122d31003fb1d0f69f0f205e8ae1ee72a0f90c946e0094b43ebbf79810131ea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page