c - C中的自定义shellcode?
问题描述
我正在尝试开发一个可以执行以下命令/bin/ls /
(列出根目录)的 shellcode。
所以,我首先尝试在汇编中开发一个脚本来为我完成这项工作,下面是脚本:
SECTION .data
buf: db "./", 0
SECTION .text
global _start
_start:
xor eax, eax
xor edx, edx
push eax
push long 0x736c2f2f ; "sl/"
push long 0x6e69622f ; "nib/"
mov ebx, esp
push eax
push byte 0x2f
mov esi, esp
push eax
push esi
push ebx
mov ecx, esp
mov eax, 0x0b
int 0x80
mov eax, 1
int 0x80
然后我编译如下:nasm -f elf -g shell.asm && ld -s -o shell shell.o -m elf_i386
. 这完美地工作,如果执行,它会列出根目录。
然后,我使用反汇编二进制文件objdump
来获取操作码:
#objdump -d ./shell
./shell: file format elf32-i386
Disassembly of section .text:
08049000 <.text>:
8049000: 31 c0 xor %eax,%eax
8049002: 31 d2 xor %edx,%edx
8049004: 50 push %eax
8049005: 68 2f 2f 6c 73 push $0x736c2f2f
804900a: 68 2f 62 69 6e push $0x6e69622f
804900f: 89 e3 mov %esp,%ebx
8049011: 50 push %eax
8049012: 6a 2f push $0x2f
8049014: 89 e6 mov %esp,%esi
8049016: 50 push %eax
8049017: 56 push %esi
8049018: 53 push %ebx
8049019: 89 e1 mov %esp,%ecx
804901b: b8 0b 00 00 00 mov $0xb,%eax
8049020: cd 80 int $0x80
8049022: b8 01 00 00 00 mov $0x1,%eax
8049027: cd 80 int $0x80
为了让生活更轻松,我做了以下事情objdump -d ./shell | awk -F " " '{print $1}' |awk -F ":" '{print $2}' | tr -d " " | tr -d "\n" | tr -d "\t" | xclip -selection c
:基本上它将操作码复制到剪贴板。31c031d250682f2f6c73682f62696e89e3506a2f89e650565389e1b80b000000cd80b801000000cd80
.
然后我要测试我的 shellcode,我将它注入到 ac 程序中,如下所示:
#include<stdio.h>
#include<string.h>
unsigned char shellcode[] = "\x31\xc0\x31\xd2\x50\x68\x2f\x2f\x6c\x73\x68\x2f\x62\x69\x6e\x89\xe3\x50\x6a\x2f\x89\xe6\x50\x56\x53\x89\xe1\xb8\x0b\x00\x00\x00\xcd\x80\xb8\x01\x00\x00\x00\xcd\x80";
int main(int argc, char **argv) {
int *ret;
ret = (int *)&ret + 2;
(*ret) = (int)shellcode;
}
# gcc -g -o foo foo.c -z execstack -fno-stack-protector
foo.c: In function ‘main’:
foo.c:9:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
9 | (*ret) = (int)shellcode;
|
完成后,如果我执行我的 foo 二进制文件,它会执行程序但不列出根目录。
我正在使用具有以下弧的 nasm:Linux kali 5.2.0-kali2-amd64 #1 SMP Debian 5.2.9-2kali1 (2019-08-22) x86_64 GNU/Linux
解决方案
推荐阅读
- cron - 为什么 Cron Job 没有启动
- c3.js - c3.js 删除时间序列之间不连续天数的空格
- javascript - 使用 Webpack 设置延迟加载组件的正确路径 - ES6
- angularjs - angularjs - 从控制器更新 ng-model 值?
- ansible - Ansible Inventory 中主机之间的暂停时间
- c - 如何在我的自研操作系统中控制显卡?
- android - 使用 JobService 启动 Service 的正确方法
- clojure - 如何让两个 Datomic Cloud 客户端保持同步?
- javascript - 在 Javascript 类中使用 const
- gradle - 将 liquibase 参数传递给 gradle liquibase 'update' 任务