This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrutedns.c
229 lines (198 loc) · 5.3 KB
/
brutedns.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
221
222
223
224
225
226
227
228
229
/* BruteDNS by Chris Gragsone <maetrics@realwarp.net>
*
* compile: gcc -O2 -o brutedns brutedns.c
* execute: brutedns [domain [wordfile]]
*/
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define DICT "/usr/share/dict/words"
#define NUM_BETWEEN_DISPLAYS 5000
#define printusage(x) fprintf(stderr, Usage, x)
int brutedns(FILE *, char *);
int checkhost(char *);
char *mkhostname(char *, char *);
int printhost(char *, struct in_addr **);
char *UnknownHost="The specified host is unknown.";
char *NoIP="The requested name is valid but does not have an IP address.";
char *UnRecovery="A non-recoverable name server error occurred.";
char *TryAgain="A temporary error occurred on an authoritative name server."
" Try again later.";
char *NoDomain="Using default domain from /etc/resolv.conf";
char *CopyRight="BruteDNS -- Brute forces domains when you can't axfr\n"
"\tChris Gragsone <maetrics@users.sf.net>\n"
"\tJeffery Strubenator <strube@users.sf.net>\n"
"\t2003\n\n"; /* TODO: Need License */
char *Usage="Usage %s [domain] [file]\n"
"\tdomain\t\tThe target domain\n"
"\tfile\t\tThe word list [/usr/share/dict/words]\n\n";
int main(int argc, char **argv) {
FILE *stream=NULL;
char *domain=NULL;
char *path=NULL;
fprintf(stderr, "%s", CopyRight);
/* TODO: Will need getopt() to handle options */
if(argc == 3) {
path=*(argv+2);
} else {
path=DICT;
}
if(argc >= 2) {
domain=*(argv+1);
} else {
printusage(*argv);
fprintf(stderr, "%s\n", NoDomain);
}
if(strcmp(path, "-") == 0) {
stream=stdin;
} else {
stream=fopen(path, "r");
}
if(stream == NULL) {
perror(path);
return 0;
}
brutedns(stream, domain);
return 1;
}
/* brutedns()
* arguments:
* stream: file descriptor for the file containing test hostnames
* domain: string containing the target domain name
* calls:
* <stdio.h> fgets()
* mkhostname()
* returns:
* always returns 1 for now
*
* notes:
* grab a word from the word file
* create a hostname using the word and the domain name
* resolve the hostname
* print the results
* continue until there are no more words
*/
int brutedns(FILE *stream, char *domain) {
int size=254-strlen(domain);
char host[size];
char *hostname=NULL;
char *test=NULL;
int wordnum=0;
//First thing we do is resolve just the domain
checkhost(domain);
test = fgets(host, size, stream);
while(test != NULL) {
//Small hack to show some progress
if(wordnum == NUM_BETWEEN_DISPLAYS) {
printf("Current word :\t%s\n",test);
wordnum=0;
} else {
wordnum++;
}
hostname=mkhostname(host, domain);
checkhost(hostname);
test = fgets(host, size, stream);
}
//TODO: Return number of hosts found
return 1;
}
/* checkhost()
* arguments:
* hostname: string containing current host name to try
* calls:
* <netdb.h> gethostbyname()
* printhost()
* returns:
* always returns 1 for now
*
* notes:
* trys to resolve hostname
* if hostnotfound then it exits
* if the host resolves then calls printhost()
*/
int checkhost(char *hostname) {
struct hostent *ent = NULL;
ent = gethostbyname(hostname);
if(ent != NULL) {
printhost(hostname,(struct in_addr **)ent->h_addr_list);
//TODO: add functionality for recursive queries
} else if(h_errno != HOST_NOT_FOUND) {
fprintf(stderr,"(%s) %s\n",hostname,UnknownHost);
}
return 1;
}
/* mkhostname()
* arguments:
* host: string containing current host name to try
* domain: string containing target's domain name
* calls:
* <stdio.h> snprintf()
* returns:
* hostname: string containing a formated query name
*
* notes:
* remove the return charector from host
* check if domain is not null and concatonate host with domain with
* seperating and ending periods
* if domain doesnt exist copy host into hostname
*/
char *mkhostname(char *host, char *domain) {
static char hostname[256];
char *catfmt="%s.%s.";
//TODO: should check if host has a return charector before removing it
host[strlen(host)-1]='\0';
if(domain != NULL) {
(void) snprintf(hostname, 255, catfmt, host, domain);
} else {
snprintf(hostname, 255, "%s", host);
}
return hostname;
}
/* printhost()
* arguments:
* hostname: string containing the name which was queried
* in: an array of pointers to resolved addresses
* calls:
* <stdio.h> printf()
* <string.h> strlen()
* <arpa/inet.h> inet_ntoa()
* returns:
* -1: if there was an error in writing to stdout
* the number of resolved hosts (currently just returns 1
*
* notes:
* Print the hostnamed queried and the first resolved address
* if there are more addresses loop until there are no more
* during each iteration, print the next resolved address
* return the number of addresses resolved
*/
int printhost(char *hostname, struct in_addr **in) {
char *foundfmt="%s resolves to %s\n";
char *foundfmt2="\t- %s\n";
char *addr=NULL;
int x=0;
int y=0;
addr=inet_ntoa(**in);
x=printf(foundfmt, hostname, addr);
y=strlen(foundfmt)+strlen(hostname)+strlen(addr)-4;
if(x > y) {
printf("printed %d, strlen %d\n", x, y);
return -1;
}
in++;
while(*in != NULL) {
addr=inet_ntoa(**in);
x=printf(foundfmt2, addr);
y=strlen(foundfmt2)+strlen(addr)-2;
if(x > y) {
printf("printed %d, strlen %d\n", x, y);
return -1;
}
in++;
}
printf("\n");
return 1;
}