flat assembler
Message board for the users of flat assembler.

Index > Main > Self-writing code

Author
Thread Post new topic Reply to topic
DustWolf



Joined: 26 Jan 2006
Posts: 373
Location: Ljubljana, Slovenia
DustWolf 01 Feb 2006, 16:24
Hello,

I understand FASM is provided in FASM source, much like a C compiler often includes source for itself. In that light, is it possible to use the FASM source to create a program which is capable of adding instructions to itself and growing as such.

How hard would this be to accomplish? Furthermore, how hard would it be to have the program update it's file upon exiting?

Exploring the idea here. Thanks for any insight.
Post 01 Feb 2006, 16:24
View user's profile Send private message Reply with quote
polygon7



Joined: 14 Aug 2003
Posts: 62
Location: Poznan, Poland
polygon7 01 Feb 2006, 17:05
Hi,
if i understand You correctly then this technique is known as "Self modifying code".

_________________
best regards
p7
Post 01 Feb 2006, 17:05
View user's profile Send private message Visit poster's website Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend 01 Feb 2006, 17:34
Modyfing your own code is not hard - you just have to declare the section with code as writeable. But, adding new code each time is another task. First of all, in windows there are these sections which are aligned to some value. During compilation FASM evaluates all the code size, etc. and put it in the section info structure. If you add new code the characteristics change and so you would have to change them in the file (and of course first evaluate them). Second thing, addresses are continous ie. the end of one section (aligned with zeroes) is the beginning of the next one. And it is nearly impossible to relocate whole next section to find place for your added data to section before. Third, probably the hardest part, you would have to write some kind of an assembler of your own. FASM gets instructions operands from the text you type (like 'mov eax, 1'). Your program would have another source. I don't know what you are planning but even though you would have to pass all input and assemble it somehow to the code you want to add. In my opinion such thing is surely possible, but the effort is extremely huge!
Also for the part of saving the result in its own file. It is also possible but only using some special tricks for current os (ie. you'd have to use some bugs to get past the file security Smile) btw. there was some thread about that here I guess...
Post 01 Feb 2006, 17:34
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 01 Feb 2006, 20:19
You can make an additional section with say a MB of free room to begin with. Then if your "code knows" what its doing, it can add all data+code to this section and there will be no alignment nor relocation issues. Just that you can only ADD to the end and REMOVE from the end, but its impossible to MODIFY!

Why it is so hard to modify is the fact that you are now dealing with binary and there is no way you can tell which part it an instruction and what instruction in particular without disassembling the whole file at first Sad
Post 01 Feb 2006, 20:19
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
chris



Joined: 05 Jan 2006
Posts: 62
Location: China->US->China->?
chris 03 Feb 2006, 13:55
Is the code encryption an example of what you guys are talking about?
since the code have to be decrypted at runtime and hence self-modifying.
Post 03 Feb 2006, 13:55
View user's profile Send private message Reply with quote
bogdanontanu



Joined: 07 Jan 2004
Posts: 403
Location: Sol. Earth. Europe. Romania. Bucuresti
bogdanontanu 03 Feb 2006, 17:24
FORTH does that!

Each word you add to the vocabulary has the same power as any other word defined in the language. You can even overwrite and or erase basic language words. Just take care to setup FENCE.

Save an image of FORTH after you have compiled some new words in Forth --> And here you have a new Forth system/compiler/interpreter eventually with a new language ready to run Wink

Such features come with a price tag: the applications/code programs written in Forth are slower that the one compiled by FASM.

Also FASM itself is much faster than Forth at compiling.
Post 03 Feb 2006, 17:24
View user's profile Send private message Visit poster's website Reply with quote
dr_dred



Joined: 29 May 2005
Posts: 5
Location: Earth
dr_dred 16 Feb 2006, 13:30
Code:
format  PE GUI 4.0
include 'include\win32a.inc'


section '.code' code readable writeable executable
start:
        mov     esi, [ebp+8]
        lea     edi, [esi+12]
        mov     [ebp+8], edi
        movsd
        movsd
        movsd
    
Post 16 Feb 2006, 13:30
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi 16 Feb 2006, 14:05
forgot something? Smile
Code:
entry start    

_________________
When We Ride On Our Enemies
support reverse smileys |:
Post 16 Feb 2006, 14:05
View user's profile Send private message MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 16 Feb 2006, 14:18
for what you want, it would be best to create some interface which allows adding keyword at runtime. Just maintain some list of directives, which can be easily expanded.
Post 16 Feb 2006, 14:18
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY 16 Feb 2006, 15:03
dr_dred: What does that code?




Here is an example of SMC:
I posted here: http://board.flatassembler.net/topic.php?p=35277#35277[/url]
Post 16 Feb 2006, 15:03
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.