You are not logged in.
Pages: 1
Look:
char machcode[]=
"\xb0\x01\x31\xdb\xcd\x80";
/*
the machcode is:
mov $0x1,%al
xor %ebx,%ebx
int $0x80
*/
typedef void (*PF)();
int main(void)
{
PF pf=(PF)machcode;
(*pf)();
return 0;
}but I run this codes,it display:Segmentation fault,
what is error in my codes?how to correct it?
Offline
Aside from that being one of the most hideous things I have ever seen, why do you want to do that?
If you must write assembly language, why not let the tools do the assembly and convert the mnemonics to machine code for you?
Is this a homework problem?
Do you think a return at the end of the function might be helpful?
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way
Offline
I also wrote hideous code like that, he probably tries to learn how to write shellcode, to exploit buffer overflow vulnerabilities. Idk, if you want to do bad things with this knowledge, or if you only
need this for a course (learned this in a few Internet Security lectures).
Whatever the reason, your problem probably is the fact that you aren't properly initializing the register eax.
Your first instruction is mov $0x01, %al. Which means, that you write a 1 into the lower 8-bit part of a 32 bit register. Since you didn't initialize eax (32 bit access),
you still have garbage in eax. As I see it, you try to invoke syscall 1(system exit), but this fails, because you have garbage in eax, not 0x01.
Aside from that, I'd suggest you to write it in assembly, and let the disassembler take care of printing you the machine code.
section .text
global _start
_start:
xor eax,eax
mov al,0x01
xor ebx,ebx
int 0x80PS:
Ok, its not the not properly initialized register, but rather the nonexec stack or something.
Last edited by dontbugme (2012-04-14 14:44:09)
Offline
... Ok, its not the not properly initialized register, but rather the nonexec stack or something.
Like running off the end of the code because there is no return from subroutine instruction?
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way
Offline
Nope, it instantly crashes at the first instruction it executes (tried it in gdb), the IP doesn't even reach the syscall. Since kernel 2.6.(whateversion), stuff like noexec etc. prevents executing code on the stack and the heap.
I just tried it, it has to be compiled like this:
gcc test.c -fno-stack-protector -z execstack -o testLast edited by dontbugme (2012-04-14 16:49:49)
Offline
Nope, it instantly crashes at the first instruction it executes (tried it in gdb), the IP doesn't even reach the syscall. Since kernel 2.6.(whateversion), stuff like noexec etc. prevents executing code on the stack and the heap.
Okay, but ... machcode is static. It is not on the stack, or is it in the heap. I poked around for a few minutes but info on noexecstack did not jump out at me. It must block execution from static space as well.
Edit: I still think one needs an RTS
Last edited by ewaller (2012-04-14 17:53:52)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way
Offline
Okay, but ... machcode is static. It is not on the stack, or is it in the heap. I poked around for a few minutes but info on noexecstack did not jump out at me. It must block execution from static space as well.
Ok,Ok, are you nit picking now? It's a global variable, so as such the memory for this variable is allocated in the data segment (.data) and these mem pages are, like the stack, heap and bss mapped nonexecutable.
If you don't believe me, check out the slides from the advanced internet security course I attended last semester (http://www.iseclab.org/InetSec2/slides/ … uption.pdf).
So without using the compiler flag -z execstack, this program doesn't work.
Edit: I still think one needs an RTS
: As I already stated above, I compiled it with -z execstack and the code was executed in the debugger step for step, and terminated the program after the int 0x80 call.
... Whatever an RTS is
.
Last edited by dontbugme (2012-04-15 10:26:18)
Offline
Ok,Ok, are you nit picking now? It's a global variable, so as such the memory for this variable is allocated in the data segment (.data) and these mem pages are, like the stack, heap and bss mapped nonexecutable.
If you don't believe me, ...
Whoa. Back the truck up..
I am not arguing. I had never encountered this before, it makes sense, and I am just trying to get my head around it. The static space could be in the same space as is program code -- that could help prevent its being inadvertently changed -- along with the executable. I really was looking for some feedback. As to the RTS, sorry I was thinking of the wrong processor. I meant RET.
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way
Offline
Hmhm. I also don't know what you mean with RET
. But I guess that doesn't matter anymore. Seems like we already scared him off with the complexity. I don't know how long others take, but it took me several days to understand the whole concept of buffer overflows, how to exploit them and so on.
Offline
Pages: 1