端口扫描分析(三)一个简单的扫描程序
来源/作者:Oliver
下面是一个端口扫描器的源程序,功能相当的简单,一个典型的TCPconnect()扫描。没有对返回的数据进行分析。
#include
#include
#include
#include
#include
#include
int main(intargc, char **argv)
{
intprobeport = 0;
structhostent *host;
int err, i,net;
structsockaddr_in sa;
if (argc !=2) {
printf("用法:%s hostname\n", argv[0]);
exit(1);
}
for (i = 1;i h_addr, sizeof sa.sin_addr);
else {
herror(argv[1]);
exit(2);
}
sa.sin_port= htons(i);
net =socket(AF_INET, SOCK_STREAM, 0);
if (net
#include
#include
#include"netdb.h"
structhostent *gethostbyaddr();
voidbad_addr();
main(argc,argv)
int argc;
char *argv[];
{
char addr[4];
int i,j,
a0, a1, a2,a3,
c,
classB,classC, single, hex;
char *fmt= "%d.%d.%d";
char **ptr;
structhostent *host;
externchar *optarg;
classB =classC = single = hex = 0;
while((c =getopt(argc,argv,"bcsx")) != EOF) {
switch(c) {
case 'b':
classB++;
break;
case 'c':
classC++;
break;
case 's':
single++;
break;
case 'x':
hex++;
break;
}
}
if(classB ==0 && classC == 0 && single == 0) {
fprintf(stderr,"usage: %s [-b||-c||-s] [-x] xxx.xxx[.xxx[.xxx]]\n", argv[0]);
exit(1);
}
if(classB)
if(hex) {
fmt ="%x.%x";
sscanf(argv[3],fmt, &a0, &a1);
} else {
fmt ="%d.%d";
sscanf(argv[2],fmt, &a0, &a1);
}
elseif(classC)
if(hex) {
fmt ="%x.%x.%x";
sscanf(argv[3],fmt, &a0, &a1, &a2);
} else {
fmt ="%d.%d.%d";
sscanf(argv[2],fmt, &a0, &a1, &a2);
}
elseif(single)
if(hex) {
fmt ="%x.%x.%x.%x";
sscanf(argv[3],fmt, &a0, &a1, &a2, &a3);
} else {
fmt ="%d.%d.%d.%d";
sscanf(argv[2],fmt, &a0, &a1, &a2, &a3);
}
sscanf(argv[1],fmt, &a0, &a1, &a2);
addr[0] =(unsigned char)a0;
addr[1] =(unsigned char)a1;
if(a0>255||a0255||a1%s\n", a0, a1, a2, a3, host->h_name);
ptr =host->h_aliases;
while (*ptr!= NULL) {
printf("%d.%d.%d.%d=> %s (alias)\n", a0, a1, a2, a3, *ptr);
ptr++;
}
}
if(single)
exit(0);
i++;
}
if(classC)
exit(0);
j++;
}
} elseif(classC) {
addr[2] =(unsigned char)a2;
if(a2>255||a2255||a2255||a3