-
Notifications
You must be signed in to change notification settings - Fork 1
/
[C]simpler_Portscanner_mit_Serviceerkennung.c
69 lines (58 loc) · 1.66 KB
/
[C]simpler_Portscanner_mit_Serviceerkennung.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
int getservice( unsigned int portnr )
{
struct servent *service;
service = getservbyport(htons(portnr),NULL );
if (!(service == NULL))
{
printf("\t (%s | %s)\n", service->s_name, service->s_proto);
}
else
{
printf("\n");
}
return 0;
}
int main(int argc, char *argv[])
{
int sockfd;
unsigned int port;
struct hostent *he;
if (argc != 2)
{
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) // get the host info
{
perror("gethostbyname");
exit(1);
}
for(port=0; port<=65000; port++)
{
struct sockaddr_in their_addr; // connector's address information
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(port); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)
{
close(sockfd);
}
else
{
printf("%d",port);
getservice( port );
close(sockfd);
}
}
}