0

I've written a simple program that obfuscates a string using ASM x86. User must input their desired string, and then choose a key (Ekey) for the string to be obfuscated. (I know you can emulate left and right bit rotations with shift operators or table lookups, I'm just trying to familiarize myself with ASM)

I'm trying to alter the program so that it adopts an accepted C++ standard calling procedure such as Cdecl for passing parameters (EKey & tempChar) into the sub-routine obfuscate.

Despite numerous hours of research I've been unsuccessful so I have come here in hope that somebody with a little more experience would be kind enough to offer some guidance.

Here's the relevant function:

void obfusc_chars (int length, char EKey)
{   char tempChar;                      // char temporary store

    for (int i = 0; i < length; i++)    // encrypt characters one at a time
    {
        tempChar = OChars [i];          //
        __asm {                         //
            push   eax                  // save register values on stack to be safe
            push   ecx                  // pushes first string on stack
                                        //
            movzx  ecx,tempChar         // set up registers (Nb this isn't StdCall or Cdecl)
            lea    eax,EKey             //
            call   obfuscate            // obfuscate the character
            mov    tempChar,al          //
                                        //
            pop    ecx                  // restore original register values from stack
            pop    eax                  //
        }
        EChars [i] = tempChar;          // Store encrypted char in the encrypted chars array
    }
   return;

And the obfuscate subroutine:

// Inputs: register EAX = 32-bit address of Ekey,
//                  ECX = the character to be encrypted (in the low 8-bit field, CL).

// Output: register EAX = the encrypted value of the source character (in the low 8-bit field, AL).

       __asm {

     obfuscate: push    esi
                push    ecx
                mov     esi, eax
      and dword ptr     [esi], 0xFF
      ror byte ptr      [esi], 1
      ror byte ptr      [esi], 1
      add byte ptr      [esi], 0x01
                mov     ecx, [esi]
                pop     edx
            x17:ror     dl, 1
                dec     ecx
                jnz     x17
                mov     eax, edx
                add eax, 0x20
                xor eax, 0xAA
                pop esi
                ret
        }

Thanks in advance for taking the time to read.

  • What exactly is the problem here? You are trying to call an inline-assembly label from your code located somewhere else, right? That's just not possible. What you need to do is to write your code into a separate assembly file, set the symbol `obfuscate` global, compile it into an object file and link it together with your program (declaring `obfuscate` as `extern "C"`) – user35443 Apr 19 '15 at 12:34
  • @user35443 Apologies, the way I've formatted the code was pretty poor, I'm not actually trying to call from code written elsewhere. The `obfuscate` sub-routine is written inline directly below the `for` loop. See: http://pastebin.com/mQ0VrRnQ – dotonthewall1 Apr 19 '15 at 13:12
  • Still, it's a label known only to the compiler. If you want to use assembly routines from your C++ code, call them as external symbols and link the corresponding compiled assembly to the program. – user35443 Apr 19 '15 at 13:32
  • Well, I only wonder then, why have you come here asking. – user35443 Apr 19 '15 at 13:36
  • I don't have any issues with my code, so why are you telling me this? I simply want to change the way the parameters `tempChar` and `EKey` are being passed. – dotonthewall1 Apr 19 '15 at 13:53

0 Answers0