News for hackers. Stuff that matters.
Re:Self-modifying code using GCC (2011) by slaymaker1907 on Monday August 07, 2023 @01:03PM (#37037261)
> One of my research topics last year was self-modifying code mainly for *obfuscation*.

I think obfuscation is generally a really bad idea, but that was brought up in the article. One practical application for self-modifying code is hotpatching. Sometimes you really don't want to stop the process but want to change the implementation for some function.

Re:Self-modifying code using GCC (2011) by Borg3 on Monday August 07, 2023 @02:12PM (#37038321)
Hold on, How you can actually do hotpatching on x86 protected mode? code is usualy executable but read only. data + stack is usualy NX but writable.
Re:Self-modifying code using GCC (2011) by LegionMammal978 on Monday August 07, 2023 @02:24PM (#37038498)
The processor itself doesn't enforce W^X protection; even though writable pages are NX by convention, nothing inherently stops you from using (the equivalent of) mmap(PROT_READ | PROT_WRITE | PROT_EXEC) and copying the instructions in, or doing the same with existing pages using mprotect(), at which point you can hotpatch as much as you want. Of course, some OSes forbid this as a hardening measure, but allowing it remains very common.
Re:Self-modifying code using GCC (2011) by slaymaker1907 on Monday August 07, 2023 @03:03PM (#37039187)
You do the same thing JIT does and change the permissions on the page to NX, apply whatever update you want, then remove the write permission and make it executable again. Even though it seems like it doesn't make a difference, it's much more difficult for an attacker to change the page permissions than it is for the hotpatcher/JIT. I think this could still fail though depending on how strict your OS is in which case you might need to use explicit function pointers which are obviously writable.

Some systems apparently allow you to add executable without removing write as that is what the OP does with "mprotect(page, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC);". However,

Re:Self-modifying code using GCC (2011) by metadat on Monday August 07, 2023 @04:21PM (#37040437)
Why is it more difficult for an attacker to change the code compared the JIT?
Re:Self-modifying code using GCC (2011) by matheusmoreira on Monday August 07, 2023 @09:41PM (#37043414)
Attackers love overflowing buffers but since that memory is not executable they can't put code in there directly. The JIT compiler is almost always the only component which writes to executable pages so they need to somehow trick it into emitting the code they want. It's my understanding that JIT spray attacks are harder to develop.
Re:Self-modifying code using GCC (2011) by matheusmoreira on Monday August 07, 2023 @09:33PM (#37043356)
You can just change the permissions with mprotect.