You are not logged in.

#1 2012-04-14 04:47:12

leetow2003
Member
Registered: 2012-04-08
Posts: 25

Why can't PC run machine instruction?

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

#2 2012-04-14 04:56:54

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,741

Re: Why can't PC run machine instruction?

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

#3 2012-04-14 05:56:10

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: Why can't PC run machine instruction?

  1. You're being a help vampire.

  2. Learn how to use a debugger.

  3. DSFR

Offline

#4 2012-04-14 14:37:27

dontbugme
Banned
Registered: 2011-11-04
Posts: 166
Website

Re: Why can't PC run machine instruction?

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 0x80

PS:
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

#5 2012-04-14 15:37:28

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,741

Re: Why can't PC run machine instruction?

dontbugme wrote:

... 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

#6 2012-04-14 16:49:01

dontbugme
Banned
Registered: 2011-11-04
Posts: 166
Website

Re: Why can't PC run machine instruction?

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 test

Last edited by dontbugme (2012-04-14 16:49:49)

Offline

#7 2012-04-14 17:53:19

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,741

Re: Why can't PC run machine instruction?

dontbugme wrote:

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

#8 2012-04-15 10:23:09

dontbugme
Banned
Registered: 2011-11-04
Posts: 166
Website

Re: Why can't PC run machine instruction?

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 wink.

Last edited by dontbugme (2012-04-15 10:26:18)

Offline

#9 2012-04-15 15:34:29

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,741

Re: Why can't PC run machine instruction?

dontbugme wrote:

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.. smile  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

#10 2012-04-16 09:17:58

dontbugme
Banned
Registered: 2011-11-04
Posts: 166
Website

Re: Why can't PC run machine instruction?

Hmhm. I also don't know what you mean with RET smile. 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

Board footer

Powered by FluxBB