English 中文(简体)
Assembly and System Calls
原标题:

Im having a bit of trouble understanding the more complex system calls in assembly. I wrote a exec system call and it worked great

 .bss

.text

.globl _start

_start:

#exit(0) system call

        movl $1, %rax
        movl $0, %rbx
        int $0X80

Though I am a bit insure and have not been able to find info pertaining to how you put strings in a register. So as an example I wanted to do a exec system call and it as its first parameter needs a filename to run and I want to run "/bin/bash", but how do I get that in rbx. How do I even know that I have to use rbx, in X86 I know I would use ebx, is it the same relationship in amd64 ebx=rbx, ecx=rcs, etc.

int execve(const char *filename, char *const argv[], char *const envp[]);

Thanks all

最佳回答

Here s a trick to make progress quickly with these aspects of assembly: ask a C compiler to show you how it does it! Write a C program that does what you want to do and type gcc -S.

Example:

Manzana:ppc pascal$ cat t.c
#define NULL ((void*)0)
char *args[] = { "foo", NULL } ;
char *env[] = { "PATH=/bin", NULL } ;


int execve(const char *filename, char *const argv[], char *const envp[]);

int main()
{

  execve("/bin/bash", args, env);

} 

then:

Manzana:ppc pascal$ gcc -S -fno-PIC t.c  # added no-PIC for readability of generated code
Manzana:ppc pascal$ cat t.s
.globl _args
    .cstring
LC0:
    .ascii "foo"
    .data
    .align 2
_args:
    .long   LC0
    .long   0
.globl _env
    .cstring
LC1:
    .ascii "PATH=/bin"
    .data
    .align 2
_env:
    .long   LC1
    .long   0
    .cstring
LC2:
    .ascii "/bin/bash"
    .text
.globl _main
_main:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $24, %esp
    movl    $_env, 8(%esp)
    movl    $_args, 4(%esp)
    movl    $LC2, (%esp)
    call    _execve
    leave
    ret
    .subsections_via_symbols
问题回答

You don t put strings in a register. You should pass a pointer (the address) to a null (0) terminated string (C style) in the register for this function. Some system calls (like write) take a pointer (not necessarily terminated by ) and length in two registers.

# somewhere in the data section:
myString:
   .asciz "/bin/bash"

and pass $myString using the register.





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签