找不到预写功能的程序集

时间:2018-09-05 16:49:37

标签: c assembly networking

我是网络协议和libnet的忠实拥护者,这就是为什么我一直在尝试模仿libnet未包含的某些网络协议。到目前为止,捕获数据包,模仿标头等都有效。现在,我需要一种将这些确切的数据包实际写入我的网卡的方法。我试过libnet_adv_write_rawipv4()和-link()都不能用。由于愚蠢的错误和错误,我无法使用libnet_adv_cull_header()剔除标题。所以我想知道,可以通过少量组装来解决该问题:获得实际的libnet_build()和libnet_write()调用的组装代码,更改一些字节,然后瞧:将原始字节写入网卡。所以我写了一个虚拟程序:

#include <stdio.h>
#include <stdlib.h>
#include <libnet.h>

int main() {
libnet_t *l;


l = libnet_init(LIBNET_RAW4, 0, NULL);


libnet_build_tcp(2000, 450, 0, 1234, TH_SYN, 254, 0, NULL, LIBNET_TCP_H + 5, 
"aaaaa", 5, l, 0);
libnet_build_ipv4(LIBNET_TCP_H + LIBNET_IPV4_H + 5, 0, 1, 0, 64, 6, 0, 
2186848448, 22587584, NULL, 0, l, 0);

libnet_write(l);
return 0;
}

到目前为止有效。现在,我使用的是程序的汇编版本

gcc -o program program.c -S

这是实际问题开始的地方:

 .LC0:
 .string    "aaaaa"
 .text
 .globl main
 .type  main, @function
  main:
 .LFB2:
 .cfi_startproc
  pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq    %rsp, %rbp
.cfi_def_cfa_register 6
subq    $16, %rsp
movl    $0, %edx
movl    $0, %esi
movl    $1, %edi
call    libnet_init
movq    %rax, -8(%rbp)
subq    $8, %rsp
pushq   $0
pushq   -8(%rbp)
pushq   $5
pushq   $.LC0
pushq   $25
pushq   $0
pushq   $0
movl    $254, %r9d
movl    $2, %r8d
movl    $1234, %ecx
movl    $0, %edx
movl    $450, %esi
movl    $2000, %edi
call    libnet_build_tcp
addq    $64, %rsp
subq    $8, %rsp
pushq   $0
pushq   -8(%rbp)
pushq   $0
pushq   $0
pushq   $22587584
pushq   $-2108118848
pushq   $0
movl    $6, %r9d
movl    $64, %r8d
movl    $0, %ecx
movl    $1, %edx
movl    $0, %esi
movl    $45, %edi
call    libnet_build_ipv4
addq    $64, %rsp
movq    -8(%rbp), %rax
movq    %rax, %rdi
call    libnet_write
movl    $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2:
.size   main, .-main

看到这个吗?

call    libnet_build_ipv4

我无法复制这些build()或write()调用的汇编代码,因为所有内容都引用了它们。现在,在哪里可以找到libnet-functions.h(libnet_build_ipv4(),libnet_build_tcp(),libnet_write())中包含的这些预写函数的汇编代码?????

1 个答案:

答案 0 :(得分:1)

在这种情况下,GDB是您的朋友。您无需说明所使用的平台,以下示例可在Ubuntu上运行,但在其他发行版上也应类似地工作。

首先,请确保您已安装libnet的调试符号:

sudo apt install libnet1-dbg

找出libnet的安装位置:

~$ dpkg -L libnet1 | grep \.so
/usr/lib/x86_64-linux-gnu/libnet.so.1.7.0
/usr/lib/x86_64-linux-gnu/libnet.so.1

使用GDB打开它(或您自己的应用程序):

~$ gdb /usr/lib/x86_64-linux-gnu/libnet.so.1.7.0
Reading symbols from /usr/lib/x86_64-linux-gnu/libnet.so.1.7.0...Reading symbols from /usr/lib/debug//usr/lib/x86_64-linux-gnu/libnet.so.1.7.0...done.
done.

使用disassemble命令检查您喜欢的任何东西:

(gdb) disassemble libnet_build_ipv4
Dump of assembler code for function libnet_build_ipv4:
   0x0000000000007d60 <+0>: push   %r15
   0x0000000000007d62 <+2>: push   %r14
   0x0000000000007d64 <+4>: push   %r13
   0x0000000000007d66 <+6>: push   %r12
   0x0000000000007d68 <+8>: push   %rbp
   0x0000000000007d69 <+9>: push   %rbx
   0x0000000000007d6a <+10>:    sub    $0x48,%rsp
   0x0000000000007d6e <+14>:    mov    0xa8(%rsp),%rbx
   0x0000000000007d76 <+22>:    mov    %edx,0x8(%rsp)
   0x0000000000007d7a <+26>:    mov    %fs:0x28,%rax
   0x0000000000007d83 <+35>:    mov    %rax,0x38(%rsp)
   0x0000000000007d88 <+40>:    xor    %eax,%eax
   0x0000000000007d8a <+42>:    mov    %ecx,0x14(%rsp)
   0x0000000000007d8e <+46>:    mov    0x80(%rsp),%r14d
   0x0000000000007d96 <+54>:    test   %rbx,%rbx
   0x0000000000007d99 <+57>:    mov    0x98(%rsp),%r15
   0x0000000000007da1 <+65>:    je     0x810a <libnet_build_ipv4+938>
   0x0000000000007da7 <+71>:    mov    %esi,%r13d
   0x0000000000007daa <+74>:    mov    0xb0(%rsp),%esi
   0x0000000000007db1 <+81>:    mov    %edi,%ebp
   0x0000000000007db3 <+83>:    mov    $0xd,%ecx
   0x0000000000007db8 <+88>:    mov    $0x14,%edx
   0x0000000000007dbd <+93>:    mov    %rbx,%rdi
   0x0000000000007dc0 <+96>:    mov    %r9d,0x1c(%rsp)
   0x0000000000007dc5 <+101>:   mov    %r8d,0x18(%rsp)
   0x0000000000007dca <+106>:   callq  0xea10 <libnet_pblock_probe>
   0x0000000000007dcf <+111>:   test   %rax,%rax
---Type <return> to continue, or q <return> to quit---q
Quit
(gdb)