Jump to content
Science Forums

Recommended Posts

Posted

I'm trying to access the file corresponding to the actual executed code, in C, i tried to make : fopen(argv[0],"rw"), but this allow only for reading the file...Does anybody know if there is a system (compiler) variable, which is the handle to the actual executed program ? Thank you in advance.

Posted

Which platform? It isn't something defined by the language you see.

 

For Win32, look up hInstance, but that won't easily get you to the machine instructions that your code compiled to, you'd need to go through the details of COFF, better to cast the function name you're interested in into a pointer. I successfully tried such a hack.

Posted
do it with python or perl or something, much simpler...
But it's a whackin' lot more fun to make a compiled language program that modifies itself!

:hammer:

unsigned char *byte = (unsigned char *)functionname;

byte += ????;// you need to see the disassambly to know what to add

for(int i=0; i<something; i++){
 //write your machine code in! :D
}

Posted

but it is crazier to do the same in python, for example if you read about post intrusion ssh hijacking, there was a python script that would grab the executing binary code, do through it in this decompiler mode, find a place where a session is started and attatch another terminal to the ssh session... its mad crazy... i have that code somewhere too...

Posted

I'd agree with alex's suggestion of Lisp, because it was really built for it. A lot of languages have an "exec" function of course, but usually its not much use for anything other than evaluating expressions.

 

The real question is, *why* do you need to self-modify? If you code using datastructures to drive your code, then all you need to do is fill the data structures and execute based on it. Its obviously not as infinitely flexible as generating code and executing it on the fly, but you'd be amazed what you can do with the general approach. We just built a feature into our product that lets people put any data they want on a page in our application by storing the SQL to get it in tables, which they can add to visually. The program just gets the query, runs it and displays it, but its really wide open...

 

Cdr,

Buffy

Posted
The real question is, *why* do you need to self-modify? Cdr,

Buffy

 

Maybe if you want a program that can be used only some times, independently of the users or computer ?? Or not to loose data, by forgetting to copy the external data files ??? But, in fact it was just a curiosity driven question...

Posted

Self Modifying Language is the beginning of dreams in the machine. A data structure is mostly a static creature. Even dynamic structures are limited to the capabilities as they exist within the program. Theoretically you could write a program that would over time adjust and expand it's capabilities, as it needed to.

 

It's something like Input dependent versus Self-contained code with optional Input correction... or something like that. I don't know, I'm just a mad scientist.

Posted
Self Modifying Language is the beginning of dreams in the machine.
Yep, but its not the *only* way! To wit:
A data structure is mostly a static creature. Even dynamic structures are limited to the capabilities as they exist within the program.
Hardly! And all I need to do to show you that you *never* need to use self-modifying code is to say that:
  1. You can write a program that contains its own Turing Machine without resorting to self-modifying code.
  2. A Turing machine can represent any program.

Ergo, you can create a program that's not self modifying that can do anything you want it to, not just stuff you program into it before hand.

It's something like Input dependent versus Self-contained code with optional Input correction... or something like that.
No, its the source of the related Halting problem: given arbitrary input, you can't prove conclusively whether a given program will ever stop or not. Even if its not self-modifying!

 

Seriously, I'd argue that self-modifying code is a historical artifact from the days when 64k of RAM was "a lot", and data storage was cards or magnetic drums. In those days, self-modification was a really cool trick to get more done in less space. Now its just obfuscation.

 

Data structures are no longer limited in their definition and scope in many languages, and in some languages like Lisp, there's no limitation at all to what you can model the world (or whatever) completely on the fly, having the data "learn" and "transmogrify" over time, driven by inputs. This is even before you get to Lisp's strange feature of being able to *execute* any of those data structures you've built on the fly!

 

Arguably, this latter feature isn't even really "self-modification": An example of true self-modifying code is having a line like:

for i = 1 to 2

x = y + z

[last line binary operator] = "-"

next i

Where on the second loop the operation would be "x = y - z", which could be more efficient in terms of bytes of program code. Its also terribly obscure in terms of explaining what's happening, and defies formal unit testing.

 

The Lisp approach is arguably a good mechanism for evolutionary code, but can be emulated with the "Turing machine in a data structure" approach in other languages, and these two methods are arguably isomorphic. In general though, I'd always advise against the "old fashioned" kind of self-modifying code though...its just obfuscating bug bait.

 

for(i--;(i%665536);i=i+j) j = (j%2?j++:j);

Buffy

Posted

What I mean, mostly is that a data structure comes as a result of an existing program capability and is as such limited by that. Self-modifying code could, in theory, grow new capabilities as it ran, and more importantly would have to check what it's doing, and what it has done and think about it. I think true AI won't be realized so long as machines don't constantly "think" about what they know and why they know it.

 

I'm not even saying that Self-moding code would get rid of Data structures, as with so much I say they are complimentry, and each has it's strength and weakness, so obviously, to me at least, the best way to get the best of both worlds is to have Data structure driven self modifying code.

 

Sure it would be complex and would eventually deviate from human readable form, but the point is to get the Machine to write it's own personality and to sit back and watch. It's like in video games right now, the content is growing disproportionately. Once upon a time Programs were about fifty-fifty, data and code. Now it's very much more skewed, mostly being of data.

 

I like to mirror processes I see in the real world in data. Right now we have deviated from the code purportionality princible, and I'm not saying that's a bad thing just that it makes for huge sprawling static systems.

 

My theory of Self-replicating Programs:

Data drives Code, Code shapes Data.

 

Einsteins theory of Spacetime:

Space-time Drives Energy, Energy Shapes Spacetime.

 

/Rant

Posted

In fact, I mean, there is modifying in memory, for exmample :

 

char *data=new char[nbytes];

 

void (*func)();

 

func=(void (*)())data;

 

(*func)();

 

which is a random code (crash guranteed, core dumbed...)

 

or modyfying the code on the medium it came from, I could do that in C++, with the following :

 

int main(int argc, char **argv)

{

char cmd[256];

strcpy(cmd,"cp ");

strcat(cmd,argv[0]);

strcat(cmd,"temp.cpy");

system(cmd);

 

///modify the file "temp.cpy"

 

// do the code

 

strcpy(cmd,"mv ");

strcat(cmd,"temp.cpy");

strcat(cmd,argv[0]);

system(cmd);

return(0);

}//end main

 

I suppose it works, because there is a delay in the system call which allow the file to be closed in execution and hence allows to write in it...so you can do for example a self-destroying program...(Impossible Mission style news)

Posted
for(i--;(i%665536);i=i+j) j = (j%2?j++:j);
Gawrsh, how can I get the cookie if ya don't give us the initial values of i and j? :lol:

 

Anyway j will never change and if it's 0 neither will i after the initial decrement. It'll go on forever unless it crosses a multiple of that rather odd looking number there...

Posted
What I mean, mostly is that a data structure comes as a result of an existing program capability and is as such limited by that.
Its true that static class definition would appear to be limiting, but the fact is that you can generalize the generation of data structures in any language that allows dynamic memory allocation (as all but the oldest languages do), and with the completely dynamic generation of data structures that are available in Lisp and XML, you can do anything you want in terms of defining data structures on the fly. And...
... so obviously, to me at least, the best way to get the best of both worlds is to have Data structure driven self modifying code.
Precisely my point dear: I apologize that I wasn't clearer... :lol:
In fact, I mean, there is modifying in memory, for exmample :

 

char *data=new char[nbytes];

void (*func)();

func=(void (*)())data;

(*func)();

That's the way we used to do it in the old days, but it only works with a hacked kernel, because most OS's do not like executing from memory marked as data space! Your code is allocating space on the heap, which the kernel says is data only: the trick in these programs is to actually get the address of the pre-defined code and modify what's there (which can be kind of tricky with virtual memory going on). This fragment you've written is really the "generate new code on the fly" form of "self-modification" rather than "modifying the pre-written code" which people used to do to fit stuff in 64k...

 

PDP-8 Desktop,

Buffy

Posted
Gawrsh, how can I get the cookie if ya don't give us the initial values of i and j? :lol:
And that was the point! Two cookies for the cookie cracking Q!

 

Obfuscode R' Us,

Buffy

Posted
...unless it crosses a multiple of that rather odd looking number there...
Okay, I guess I have to clue you in on this one, but I'll give you another chance: 665536 is a typo, and it should have been 65536. Get it now? :lol:

 

Cookies! Easter Eggs! Frobnitzi! Shme!

Buffy

Posted
Okay, I guess I have to clue you in on this one, but I'll give you another chance: 665536 is a typo, and it should have been 65536. Get it now?
Ahah! That was exactly my suspicion! Meaning that a bitwise and would have been enough!

 

Optimizeit,

Q :hihi:

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...