GPI wrote:One Problem would be, that many Virusscanner would detect this as malware.
This might sound like a good theory but is completely impractical because simply by detecting a writeable code section as malware would be
false alarm city!!!
Remember a lot of commercial programs also use packers, and some software simply cannot function without this capability. It is an important capability and you can do some amazing things with it (consider Alexia Massalin and her
Synthesis kernel!)
Of the 10 primary uses listed at Wikipedia's self-modifying code page, only 1 is for potential "bad" purposes ie hiding code (but that exact same purpose is also used to protect commercial software), so please let's not be close-minded about this:
1. Semi-automatic optimizing of a state dependent loop.
2. Run-time code generation, or specialization of an algorithm in runtime or loadtime (which is popular, for example, in the domain of real-time graphics) such as a general sort utility – preparing code to perform the key comparison described in a specific invocation.
3. Altering of inlined state of an object, or simulating the high-level construction of closures.
4. Patching of subroutine (pointer) address calling, usually as performed at load/initialization time of dynamic libraries, or else on each invocation, patching the subroutine's internal references to its parameters so as to use their actual addresses. (i.e. Indirect 'self-modification').
5. Evolutionary computing systems such as genetic programming.
6. Hiding of code to prevent reverse engineering (by use of a disassembler or debugger) or to evade detection by virus/spyware scanning software and the like.
7. Filling 100% of memory (in some architectures) with a rolling pattern of repeating opcodes, to erase all programs and data, or to burn-in hardware.
8. Compressing code to be decompressed and executed at runtime, e.g., when memory or disk space is limited.
9. Some very limited instruction sets leave no option but to use self-modifying code to perform certain functions. For example, a one instruction set computer (OISC) machine that uses only the subtract-and-branch-if-negative "instruction" cannot do an indirect copy (something like the equivalent of "*a = **b" in the C language) without using self-modifying code.
10. Altering instructions for fault-tolerance.
Also it is possible that future or current os would not support this.
Why? Just because some malware use packers? Again, so does a lot of commercial software, and again some things can
only be done when the code section is writeable.
And im not writing for future OS, just current... and even if future OS did for whatever reason say "ok no more writeable code sections", thats not a problem because we don't
have to use it for regular programs! Its optional - you would have to enable it manually with a statement like my "EnableCodeWrite" example

Code sections are ALLOWED to be writeable for a reason, and simply banning them would be on par with banning DeleteFile API because malware might use it to delete your data.
Unlike the DeleteFile API however, by very definition self-modifying code can't do any harm to anything other than itself!
And im not even sure making your exe harder to disassemble counts as harm -- that's the "worst" that self-modifying code can do. That's really it's only "crime", the only real reason it sometimes gets a bad wrap.
In my personal opinion this is a very bad programing style.
That's fine, because naturally it would be off by default - exactly as it is now, so you wouldn't even be using it, so there's no problem and you don't have to complain about other people using a feature they want to use that you dont
Again I need this functionality for legitimate reasons. There is no need for OPINION when it comes to this
So let's put opinion aside and see what actually happens when we scan with 50+ virus scanners at virustotal.com heehee ...
I created this single line program: MessageRequester("Hello","Hello") .... I disabled debugger, compiled in ascii mode, for both 32 and 64.
Its important to note here that a PB exe has two main code sections - ".text" where the entrypoint and initialization code is, and then that jumps over to the ".code" where all our user code is.
Both sections by default are RW, but not X. By comparison a typical UPX-packed exe also has two code sections .UPX0 and .UPX1, but they're both RWX. So by your theory every single UPX-packed exe would be flagged by scanners
I'll try 1) original PB exes (32bit + 64bit) that simply say MessageRequester "Hello"
2) the originals, but with .code section RW -> RWX
3) the originals, but with .code + .text sections RW -> RWX
4) the originals, but UPX packed (so, both .UPX0 and .UPX1 as RWX)
Virustotal Results:
32bit original unmodified:
1/54:
Qihoo-360: "HEUR/QVM05.1.Malware.Gen" .... i hadnt heard of Qihoo-360, its a Chinese AV. It can only be used to scan 32bit exes, it doesnt support 64 - lame!
64bit original unmodified:
0/54 (100% clean) ..... ok good, our 64bit msgbox hello world is clean.
32bit, .code section patched RW -> RWX
4/55:
Avast: "Win32:Evo-gen [Susp]"
Qihoo-360: "HEUR/QVM05.1.Malware.Gen"
TrendMicro: "PAK_Generic.001"
TrendMicro-HouseCall: "PAK_Generic.001"
... so really only TrendMicro is flagging it. Avast we can ignore (not as Generic, but as a false alert), because as we'll soon see Avast doesnt flag the same for 64bit exes, so its probably also a combination of that + something else in the exe. Qihoo we can ignore.
64bit, .code section RW -> RWX
0/53 (100% clean)
Now lets try BOTH the .code + .text sections as RWX, so that includes the section with the Entrypoint ...
32bit, .code + .text section RW -> RWX
3/55:
Qihoo-360: "HEUR/QVM05.1.Malware.Gen"
TrendMicro: "PAK_Generic.001"
TrendMicro: "PAK_Generic.001"
64bit, .code + .text section RW -> RWX
0/53 (100% clean)
Now lets try the original again, but UPX'd (latest - v3.91w) ...
32bit original, UPX'd
4/53:
Qihoo-360: "HEUR/QVM11.1.Malware.Gen"
TheHacker: "Posible_Worm32" <- anybody heard of "TheHacker" scanner? their spelling is as bad as their detection
TrendMicro: "PAK_Generic.005"
TrendMicro: "PAK_Generic.005"
64bit original, UPX'd
0/54 (100% clean)
---
So regardless of whether it's UPX-packed or we've simply manually made a Writeable executable section (or used the assembler itself ie. "section '.code' code readable writeable executable align 4096"),
the answer is clearly NO - 99% of AV's will not falsely alert (only TrendMicro, and not as infected but just "generic packed" warning), so yes it's perfectly fine to use
I will still be contacting the above companies anyway to alert them of course, as all developers should do whenever they encounter a false alert from any scanner, and as recommended in this
Purebasic blog 