Werid question about "binary data execution" 
Author Message
 Werid question about "binary data execution"

Hello, maybe I don't know what I'm talking about, but
I'm wondering if it would be possible to execute runtime
loaded binary data in a C program...
As far as I know this is possible in asm.

An example if the above description
was not clear enough:

char exe_image[] = {
        #include "<BINARYDATA.EXE>"

Quote:
};

where BINARYDATA is a executable file converted in
HEX.

now is there a way to "jump" to the data ?

Please forgive my question if it is too dumb to ask.



Wed, 29 Jun 2005 08:43:32 GMT  
 Werid question about "binary data execution"


: Hello, maybe I don't know what I'm talking about, but
: I'm wondering if it would be possible to execute runtime
: loaded binary data in a C program...
: As far as I know this is possible in asm.
:
: An example if the above description
: was not clear enough:
:
: char exe_image[] = {
:         #include "<BINARYDATA.EXE>"
: };
:
: where BINARYDATA is a executable file converted in
: HEX.
:
: now is there a way to "jump" to the data ?
:
: Please forgive my question if it is too dumb to ask.
:
    You can do it, but not like that, and it depends on many things that are
OS compiler specific.  Therefore it's off-topic in the NG.   Search for a NG
that supports your compiler/OS combo, and read their F.A.Q. First.

    Charles



Wed, 29 Jun 2005 09:06:47 GMT  
 Werid question about "binary data execution"
in comp.lang.c i read:

Quote:


>: Hello, maybe I don't know what I'm talking about, but
>: I'm wondering if it would be possible to execute runtime
>: loaded binary data in a C program...
>    You can do it, but not like that, and it depends on many things that
>are OS compiler specific.

which includes the possibility that it cannot be done.

--
bringing you boring signatures for 17 years



Wed, 29 Jun 2005 14:05:36 GMT  
 Werid question about "binary data execution"
yeah there is. one you have the data in that character array: do the
following to execute it:

    void (*func)(); // creates a pointer to a function
    func=exe_image; // assignes the data of the exe to pointer, pointing

                    // to the exe data in memory
    func(); // calls the function (the exe)

in theory this should work, i have never tried it, but i have executed
data which i converted from asm to hex in this way.

mentRa

Quote:

> Hello, maybe I don't know what I'm talking about, but
> I'm wondering if it would be possible to execute runtime
> loaded binary data in a C program...
> As far as I know this is possible in asm.

> An example if the above description
> was not clear enough:

> char exe_image[] = {
>         #include "<BINARYDATA.EXE>"
> };

> where BINARYDATA is a executable file converted in
> HEX.

> now is there a way to "jump" to the data ?

> Please forgive my question if it is too dumb to ask.



Wed, 29 Jun 2005 23:24:20 GMT  
 Werid question about "binary data execution"


Quote:
> yeah there is. one you have the data in that character array: do
the
> following to execute it:

>     void (*func)(); // creates a pointer to a function
>     func=exe_image; // assignes the data of the exe to pointer,
pointing

>                     // to the exe data in memory
>     func(); // calls the function (the exe)

> in theory this should work, i have never tried it, but i have
executed
> data which i converted from asm to hex in this way.

Assuming that data pointers and function pointers are compatible
with each other, and probably a few other assumptions, this can
work.   Of course it isn't portable, since it depends on knowing
the machine code for the processor you are on.

-- glen



Thu, 30 Jun 2005 07:14:03 GMT  
 Werid question about "binary data execution"

Quote:

> yeah there is. one you have the data in that character array: do the
> following to execute it:

>     void (*func)(); // creates a pointer to a function
>     func=exe_image; // assignes the data of the exe to pointer, pointing

>                     // to the exe data in memory
>     func(); // calls the function (the exe)

> in theory this should work, i have never tried it, but i have executed
> data which i converted from asm to hex in this way.

    "In theory" there are no guarantees whatsoever about
what this might do.  None.

    "In practice" this will work on some machines (for
suitably restricted definitions of "work"), but most
definitely not on all.  On some machines I have known,
this will work some of the time and fail at other times,
and the probability of success or failure depends (among
other things) on how heavily the system is loaded.

--



Fri, 01 Jul 2005 23:37:40 GMT  
 Werid question about "binary data execution"

Quote:
>yeah there is. one you have the data in that character array: do the
>following to execute it:

>    void (*func)(); // creates a pointer to a function
>    func=exe_image; // assignes the data of the exe to pointer, pointing

>                    // to the exe data in memory

Without a cast, this statement requires a diagnostic.
With the proper cast, the code invokes undefined behaviour, because the
standard doesn't define the conversion between data pointers and function
pointers.

Quote:
>    func(); // calls the function (the exe)

>in theory this should work, i have never tried it, but i have executed
>data which i converted from asm to hex in this way.

With the proper fix, there is a chance that it might work, if the included
code is fully position independent and it is not executed on a Harvard
architecture (e.g. several MSDOS memory models).  The hardware + OS must
also make no distinction between the memory pages containing data and the
ones containing executable code.

So, you need "cooperating" compiler, hardware and operating system, even
if the included code was carefully written to be position independent.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sat, 02 Jul 2005 00:55:35 GMT  
 Werid question about "binary data execution"

Quote:

>yeah there is. one you have the data in that character array: do the
>following to execute it:

>    void (*func)(); // creates a pointer to a function
>    func=exe_image; // assignes the data of the exe to pointer, pointing

>                    // to the exe data in memory
>    func(); // calls the function (the exe)

>in theory this should work,

How it would work?
__________________
/* get.exe */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *getstr2(FILE*  fp, size_t*  si)
{char          *p, *tmp;
 char              *buf;
 size_t     sz=0, csz=0;
 p=buf=malloc(2048);
 if(p==NULL)
       goto label;
 while(!feof(fp) && !ferror(fp))/*se qualche errore fine programma*/
  {if(sz==2047)
     {csz += 2047;
      tmp=realloc(buf, csz+2048);
      if(tmp==0)
        {label:
         fprintf(stderr, "\nproblems in getting the string \n");
         perror(""); free(buf); abort();
        }
      buf=tmp; p=buf+csz; sz=0;
     }
   sz=fread(p, 1, 2047-sz, fp);
   p += sz; *p='\0';
  }
 if(ferror(fp))
       goto label;
 *si=csz+sz;
 return buf;

Quote:
}

int main(int c, char** a)
{void     (*f)(void);
 FILE            *fp;
 char            *cp;
 size_t          g=0;
 if(c<2)
     return 0;
 if( !(fp=fopen(a[1],"rb")) )
   {fprintf(stderr, "\nNon riesco a aprire \"%s\" \n", a[1]);
    return 0;
   }
 cp=getstr2(fp, &g);
 fclose(fp);
 puts(cp);
 printf("size=%lu\n", (long unsigned) g);
 f=cp; f();
 free(cp);
 return 0;
Quote:
}

_________
 > get  file.exe
 =#&?#>?


Wed, 13 Jul 2005 18:39:05 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Displaying binary data as ascii "1"'s and "0"'s

2. Displaying binary data as ascii "1"'s and "0"'s

3. "INSERT" binary data into a field

4. "Server execution failed" on Windows XP

5. "Server Execution Failed" Exception

6. Looking for binary "diff" and binary "diffupdt / patch"

7. Reading "float" in a binary file

8. "forgetful binary chop"

9. binaries (generated from "C" code)

10. "Windows data types" vs "c data types"

11. remove() vrs fopen("""w")

12. Looking for "Shroud"/"Obfus"

 

 
Powered by phpBB® Forum Software