課題5:ポートスキャンの実験
自分の実験環境(端末)の使用/未使用ポート(ウェルノウンポートのみでok)を確認するポートスキャンプログラムを作成せよ.さらに,任意のリモート端末の使用/未使用ポートを確認するように改良せよ(加点ポイント).なお,スクリプトを使って内部で'netstat -l'コマンドを実行し,その結果を利用するのは不可とする(ソケットプログラムを作成すること).
ソースプログラム
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
main(int argc, char **argv)
{
struct hostent *h;
struct sockaddr_in host;
if(argc != 2){
printf("Usage:scan hostname\n");
}
h = gethostbyname(argv[1]);
if(!h){
printf("error:failed to serch the host\n");
exit(1);
}
int wellp;
bzero((char *)&host, sizeof(host));
host.sin_family = AF_INET;
host.sin_addr = *(struct in_addr*)h->h_addr;
for(wellp=0; wellp <= 1023; wellp++){
host.sin_port = htons(wellp);
int s = socket(AF_INET, SOCK_STREAM, 0);
if(s==-1)
printf("failed to socket\n");
if(connect(s, (struct sockaddr *)(&host), sizeof(host)) == 0)
/*{
printf("failed to connect %s port %d\n", argv[1], wellp);
perror("");
}
else*/
printf("success to connecct %s port %d\n", argv[1], wellp);
close(s);
}
exit(1);
}
実行結果:ローカルホスト
[nw0553:~/tcpip] j05053% ./a.out localhost
success to connecct localhost port 427
success to connecct localhost port 548
success to connecct localhost port 631
実行結果:リモート端末
[nw0553:~/tcpip] j05053% ./a.out 133.13.59.58
success to connecct 133.13.59.58 port 427
success to connecct 133.13.59.58 port 548