日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux反调试代码,linux反调试方法

發布時間:2025/3/19 linux 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux反调试代码,linux反调试方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如何防止自己的程序被調試器跟蹤,這是一個很有趣的話題,也是反逆向工程中的一個重要話題。這里簡單介紹一下Linux平臺上的反調試技術。

(本文主要參考:http://blog.txipinet.com/2006/10/05/37-tecnicas-anti-debugging-sencillas-para-gnu-linux/。

做人要厚道,轉載請指明出處!)

一. int3指令

Intel Software Developer’s Manual Volume 2A中提到:

The INT 3 instruction generates a special one byte opcode (CC) that is intended for

calling the debug exception handler. (This one byte form is valuable because it can be

used to replace the first byte of any instruction with a breakpoint, including other one

byte instructions, without over-writing other code).

int3是一個特殊的中斷指令(從名字上也看得出來),專門用來給調試器使用。這時,我們應該很容易想到,要反調試,只要插入int3來迷惑調試器即可。不過,這會不會影響正常的程序?會!因為int3會在用戶空間產生SIGTRAP。沒關系,我們只要忽略這個信號就可以了。

#include

#include

voidhandler(intsigno)

{}

intmain(void)

{

signal(SIGTRAP,handler);

__asm__("nop\n\t"

"int3\n\t");

printf("Hello from main!\n");

return0;

}

二. 文件描述符

這是一個很巧妙的辦法,不過它只對gdb之類的調試器有效。方法如下:

#include

#include

#include

intmain(void)

{

if(close(3)== -1){

printf("OK\n");

}else{

printf("traced!\n");

exit(-1);

}

return0;

}

gdb要調試這個程序時會打開一個額外的文件描述符來讀這個可執行文件,而這個程序正是利用了這個“弱點”。當然,你應該能猜到,這個技巧對strace是無效的。

三. 利用getppid

和上面一個手法類似,不過這個更高明,它利用getppid來進行探測。我們知道,在Linux上要跟蹤一個程序,必須是它的父進程才能做到,因此,如果一個程序的父進程不是意料之中的bash等(而是gdb,strace之類的),那就說明它被跟蹤了。程序代碼如下:

#include

#include

#include

#include

#include

#include

#include

intget_name_by_pid(pid_tpid,char*name)

{

intfd;

charbuf[1024]={0};

snprintf(buf,1024,"/proc/%d/cmdline",pid);

if((fd=open(buf,O_RDONLY))== -1)

return-1;

read(fd,buf,1024);

strncpy(name,buf,1023);

return0;

}

intmain(void)

{

charname[1024];

pid_tppid=getppid();

printf("getppid: %d\n",ppid);

if(get_name_by_pid(ppid,name))

return-1;

if(strcmp(name,"bash")==0||

strcmp(name,"init")==0)

printf("OK!\n");

elseif(strcmp(name,"gdb")==0||

strcmp(name,"strace")==0||

strcmp(name,"ltrace")==0)

printf("Traced!\n");

else

printf("Unknown! Maybe traced!\n");

return0;

}

同樣的手法,一個更簡單的方式是利用session id。我們知道,不論被跟蹤與否,session id是不變的,而ppid會變!下面的程序就利用了這一點。

#include

#include

#include

intmain(void)

{

printf("getsid: %d\n",getsid(getpid()));

printf("getppid: %d\n",getppid());

if(getsid(getpid())!=getppid()){

printf("traced!\n");

exit(EXIT_FAILURE);

}

printf("OK\n");

return0;

}

四. 利用環境變量

bash有一個環境變量叫$_,它保存的是上一個執行的命令的最后一個參數。如果在被跟蹤的狀態下,這個變量的值是會發生變化的(為什么?)。下面列出了幾種情況:

argv[0] getenv("_")

shell ./test ./test

strace ./test /usr/bin/strace

ltrace ./test /usr/bin/ltrace

gdb /home/user/test (NULL)

所以我們也可以據此來判斷。

#include

#include

#include

intmain(intargc,char*argv[])

{

printf("getenv(_): %s\n",getenv("_"));

printf("argv[0]: %s\n",argv[0]);

if(strcmp(argv[0],(char*)getenv("_"))){

printf("traced!\n");

exit(-1);

}

printf("OK\n");

return0;

}

五. 利用ptrace

很簡單,如果被跟蹤了還再調用ptrace(PTRACE_TRACEME…)自然會不成功。

#include

#include

intmain(void)

{

if(ptrace(PTRACE_TRACEME,0,1,0)<0){

printf("traced!\n");

return1;

}

printf("OK\n");

return0;

}

Happy hacking!

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的linux反调试代码,linux反调试方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。