Thread: How to call assemble code

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    50

    How to call assemble code

    I get machine code for the function:
    Code:
    void ptr()
    {
      printf("Hello");
    }
    and I want to call them in another program,
    Look:
    Code:
    typedef void (*PF)();
    unsigned char buff[]="\x55\x89\xe5\x83\xec\x08\xc7\x04\x24\x50\x85\x04\x08\xe8\x02\xff\xff\xff\xc9\xc3";
    int main(void)
    {
      
      PF pf=(PF)buff
      pf();
      return 0;
    }
    but it always display Segmentation fault,
    how to call the code?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Note that buff[] is a char array while pf is a pointer to a function returning void - the two are mutually exclusive.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The first general assumption is that someone who posts code as hex strings is trying some kind of code injection hackery.

    The second general assumption is that such code is completely incompatible with the OS you're trying to run it on. For example, if that's 16-bit DOS code, then you're S-o-L if you're trying to run it on Linux.

    The third general assumption is that your OS protects against executing code which isn't in a read-only-executable segment. Random hex like that is at best read-only, and certainly never executable.

    So, please disassemble the code so we can see what you're up to.

    It's probably worth reading the forum rules at this point before you go any further.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You say the code implements printf("Hello"), but it doesn't contain the proper ascii codes ....
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by oogabooga View Post
    You say the code implements printf("Hello"), but it doesn't contain the proper ascii codes ....
    Even if it did have the right ascii codes, it wouldn't work on *nix machines (dunno about windoze) because on *nix only the text segment is executable, while buff[] lies in the data segment.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I imagine that Windows works the same as eunuchs in that way. My point was that his code is not implementing what he said it was.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Assuming its correct and meant to be 32-bit x86, it disassembles to:
    Code:
    push    ebp
    mov     ebp, esp
    sub     esp, 8
    mov     dword ptr [eax+ecx], 0xFFFF02E8
    dec     ecx
    retn
    Me neither

  8. #8
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Judging by similar code on Wiki:

    push ebp ; save calling function's stack frame (ebp)
    mov ebp, esp ; make a new stack frame on top of our caller's stack
    sub esp, 8 ; allocate 8 bytes of stack space for this function's local variables

    and the next bits appear to just grab data from a hard-coded pointer address and decrement it (and, I suspect, return the decremented address).

    Nothing too "scary", but definitely something a little dodgy about not using a damn ASSEMBLER, especially if it's just on its own, though.

    Seriously - this will not work for the majority of compilers / OS because of memory protections like DEP, etc. If you want to run assembler in your C code, use an ASSEMBLER (e.g. nasm), then compile the code in that, then link that compiled code (.o file) into your C program and call it as a normal C function. That way, it *won't* be left in an area of memory marked non-executable and cause all sorts of errors, and you won't have to hard-code compiled machine code by a hex string (which is a ridiculous way to try to program).

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I would say judging by this person's post history, they're up to no good. Some sort of remote spy application.

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > I would say judging by this person's post history, they're up to no good.

    Does anything look sketchy about any of these threads:
    - How to reopen stdout? (exec hacks)
    - Ask two question about embedded assembler (shellcode?)
    - How to transfer a big file only one time? (sending a virus maybe?)
    - How to open or close "My Computer" with command? (obviously precursor to a malicious program)
    - How to get the document about the struct SECURITY_DESCRIPTOR? (requesting information about windows security)
    - Ask for a strange question (packet forging)
    - How to get packet IP? (raw socket trickery)
    - Why couldn't scan the active port in my PC? (trying to portscan)

    Yeah, we're not helping you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to assemble the output of gcc -S ?
    By manasij7479 in forum Tech Board
    Replies: 6
    Last Post: 10-13-2011, 04:52 AM
  2. Sytem call internal and code
    By ankur0921 in forum Linux Programming
    Replies: 2
    Last Post: 05-14-2010, 06:47 AM
  3. how to properly call an executable from C code?
    By remy06 in forum C Programming
    Replies: 3
    Last Post: 05-14-2009, 03:48 AM
  4. Call managed code (c#) from unmanaged code (VC++ 6.0)
    By playxn in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2008, 12:11 PM
  5. Computers what kind of idiots assemble them (i mean the shops)
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-28-2003, 08:43 AM