PDA

View Full Version : Extremely simple c port scanner


jbm
11-25-2002, 07:48 AM
Ok, I understand what is wrong with this but am not sure what to do about it ;)
Basically I want to use the connect function to try to connect to a port, then if successful print a message, otherwise go to the next port. So something like this:


while(i < 100) {
servaddr.sin_port = htons(i);

if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) == 0) {
printf("Server found at port %d", servaddr.sin_port);
close(sockfd);
}
i++;
}


Now note servaddr is a sockaddr_in that is set up correctly. The connect works and all (I watched netstat for a few minutes) - it tries to connect, but waits around 70seconds to time out if unsuccessful, then goes to the next port. Obviously I want a quicker scanner than that... So I am wondering what I should use to get a quicker response. I don't think it's possible to change the timeout for connect(), so I'm wondering if there is some other funtion I should use to try this.

This is just something for self educational purposes.. trying to learn more about network programming, no l337 shit duders.

jemfinch
11-25-2002, 10:47 AM
I think you'll need to use SIGALRM and a signal handler.

Jeremy

jbm
11-25-2002, 10:53 AM
Hmm.. yeah.. I guess I need some non-blocking form of connect, which could consist of an alarm...

Thanks :)

jbm

hank freid love
06-27-2008, 04:10 AM
I don't plan on making this portscanner any better, I've already wrote a multi-threaded portscanner on Windows that was lightning fast, and showed up banners and everything, but we all know nmap is the best. It would be extremely difficult to write a sniffer wouldn't it?