c - 找不到预写函数的程序集
问题描述
我是网络协议和 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()) 中包含的这些预先编写的函数的汇编代码??????
解决方案
在这种情况下,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)
推荐阅读
- ckeditor - 如何启用 TYPO3 CKEditor RTE 本地锚链接
- c# - 如何从字符串中提取整数值
- java - Maven父POM问题
- mysql - laravel 在 MySQL 中搜索 JSON 字段值
- reactjs - 如何隐藏特定路由的页眉和页脚
- c++ - 如何从二叉搜索树中获取有序排序的数组?(在 C++ 中)
- google-cloud-platform - 来自 GCS 的文件大小可以在 Cloud Function 中处理多少?
- python - GCP KMS 使用 Python 签署 JWT
- python - Discord.py:实现错误处理程序时命令损坏
- go - 使用 fmt.Scan()/fmt.Scanf()/fmt.Scanln() 扫描包含空格的字符串?