forked from Har-Kuun/DomainMegaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DomainScan.c
220 lines (202 loc) · 5.91 KB
/
DomainScan.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<unistd.h>
int DomainScan(char * , char *, char *, char *);
int hostname_to_ip(char * , char *);
int whois_query(char * , char * , char **);
int Str_Split(char *str, char c, char ***arr);
char* Str_Conn(const char *s1, const char *s2);
int main(int argc , char *argv[]) {
FILE * fp = fopen("tld","r");
if (fp==NULL) {
printf("TLD database not found!\n");
exit(1);
}
char *line = NULL; char Ext[10];
size_t len = 0; ssize_t read;
char **arr = NULL;
char DomainExt[10]={'0'}; char NoMatchPattern[20]; char WhoisQueryServer[40];
printf("\n\nThank for using DomainScan. \n\nPlease note that this bot does not guarante the availability.\n\n");
printf("Please specify TLD: ");
scanf("%s", Ext);
while ((read = getline(&line, &len, fp)) != -1) {
Str_Split(line, '=', &arr);
if (strcmp(arr[0],Ext)==0) {
strcpy(DomainExt,arr[0]); strcpy(NoMatchPattern,arr[2]); strcpy(WhoisQueryServer,arr[1]);
break;
}
}
if (*DomainExt == '0') {
printf("TLD not supported!\n");
exit(2);
}
fclose(fp);
char DictFile[256];
printf("Please specify dicitionary file: ");
scanf("%s",DictFile);
FILE * fp_Dict = fopen(DictFile,"r");
if (fp_Dict==NULL) {
printf("dicitionary file not found!\n");
exit(3);
}
char c; size_t n=0;
char DomainPrefix[70] , domain[100], *data = NULL;
FILE * fp_Result = fopen(Str_Conn(DomainExt,"_results.dat"),"w");
if (fp_Result==NULL) {
printf("Failed to write results to file!\n");
exit(4);
}
fprintf(fp_Result,"\nThank for using DomainScan!\n");
fclose(fp_Result);
while (1) {
n=0;
*DomainPrefix='\0';
while ((c = fgetc(fp_Dict)) != '\n') {
if (c==EOF) {
printf("Task finished!\n\n");
fp_Result=fopen(Str_Conn(DomainExt,"_results.dat"),"a");
fprintf(fp_Result,"\nThanks you for using DomainScan!\n");
fclose(fp_Result);
fclose(fp_Dict);
exit(0);
}
DomainPrefix[n++] = c;
}
DomainPrefix[n]='\0';
strcpy(domain,Str_Conn(Str_Conn(DomainPrefix,"."),DomainExt));
DomainScan(domain, NoMatchPattern, WhoisQueryServer, DomainExt);
}
return 0;
}
int DomainScan(char *domain , char * NoMatchPattern, char * WhoisQueryServer, char * DomainExt) {
char *response = NULL ; FILE * fpR;
do {
whois_query(WhoisQueryServer, domain, &response);
if (response) break;
sleep(1);
}
while(1);
if (strstr(response,NoMatchPattern)!=NULL) {
printf("%s Available for registration!\n", domain);
fpR=fopen(Str_Conn(DomainExt,"_results.dat"),"a");
fprintf(fpR,"%s\n",domain);
fclose(fpR);
}
else
printf("%s Not available.\n", domain);
return 0;
}
int whois_query(char *server , char *query , char **response) {
char ip[32] , message[100] , buffer[1500];
int sock , read_size , total_size = 0;
struct sockaddr_in dest;
sock = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP);
memset( &dest , 0 , sizeof(dest) );
dest.sin_family = AF_INET;
if(hostname_to_ip(server , ip)) {
printf("Falied to resolve hostname.");
return 1;
}
dest.sin_addr.s_addr = inet_addr( ip );
dest.sin_port = htons( 43 );
if(connect( sock , (const struct sockaddr*) &dest , sizeof(dest) ) < 0) {
perror("Failed to reach whois server.");
}
sprintf(message , "%s\r\n" , query);
if( send(sock , message , strlen(message) , 0) < 0) {
perror("Failed to send query.");
}
while( (read_size = recv(sock , buffer , sizeof(buffer) , 0) ) ) {
*response = (char*)realloc(*response , read_size + total_size);
if(*response == NULL) {
printf("Falied to access system memory.");
}
memcpy(*response + total_size , buffer , read_size);
total_size += read_size;
}
fflush(stdout);
*response = (char*)realloc(*response , total_size + 1);
*(*response + total_size) = '\0';
close(sock);
return 0;
}
int hostname_to_ip(char * hostname , char* ip) {
struct hostent *he;
struct in_addr **addr_list;
int i;
if ( (he = gethostbyname( hostname ) ) == NULL) {
herror("gethostbyname");
return 1;
}
addr_list = (struct in_addr **) he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++) {
strcpy(ip , inet_ntoa(*addr_list[i]) );
return 0;
}
return 0;
}
int Str_Split(char *str, char c, char ***arr) {
int count = 1;
int token_len = 1;
int i = 0;
char *p; char *t;
p = str;
while (*p != '\0') {
if (*p == c)
count++;
p++;
}
*arr = (char**) malloc(sizeof(char*) * count);
if (*arr == NULL) {
printf("Unable to access system memory.");
exit(9);
}
p = str;
while (*p != '\0') {
if (*p == c) {
(*arr)[i] = (char*) malloc( sizeof(char) * token_len );
if ((*arr)[i] == NULL){
printf("Unable to access system memory.");
exit(9);
}
token_len = 0;
i++;
}
p++;
token_len++;
}
(*arr)[i] = (char*) malloc( sizeof(char) * token_len );
if ((*arr)[i] == NULL) {
printf("Unable to access system memory.");
exit(9);
}
i = 0;
p = str;
t = ((*arr)[i]);
while (*p != '\0') {
if (*p != c && *p != '\0') {
*t = *p;
t++;
}
else {
*t = '\0';
i++;
t = ((*arr)[i]);
}
p++;
}
return count;
}
char* Str_Conn(const char *s1, const char *s2) {
const size_t len1 = strlen(s1);
const size_t len2 = strlen(s2);
char *result = (char*)malloc(len1+len2+1);
memcpy(result, s1, len1);
memcpy(result+len1, s2, len2+1);
return result;
}