forked from udishamir/Domain-Analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomainanalyzer.c
More file actions
196 lines (174 loc) · 4.42 KB
/
domainanalyzer.c
File metadata and controls
196 lines (174 loc) · 4.42 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <regex.h>
#include <GeoIP.h>
#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include "common.h"
#include "libdoma.h"
#define MD5MAX (uint32_t) 32
#define SUCCESS (uint32_t) 0
void usage(const char * p)
{
fprintf(stderr, "domain analyzer: usage:\n%s domain/ip [-v]\n"
"%s -u/--update\n"
// get ASN && WLIST versions //
"version information:\n"
"\tASN: %s\n"
"\tWhitelist: %s\n", p, p,
ASN_VERSION, WHITELIST_VERSION);
}
int main(int argc, char *argv[])
{
if((argc != 2) && (argc != 3))
{
usage(argv[0]);
return -1;
}
char * host;
int verbose = 0;
if ((0 == strcmp(argv[1], "-u")) || (0 == strcmp(argv[1], "--update")))
{
// TODO: this is quite ugly, maybe change or aleast hide (move from here)
// get relative path to executable
char download_path[256];
strncpy(download_path, argv[0], sizeof(download_path));
// find rightmost /
int pos;
for (pos = strlen(download_path)-1; pos >= 0 && download_path[pos] != '/'; pos--);
// strip out file name from path
download_path[++pos] = '\0';
// update to domain analyzer directory
return update(download_path);
}
// TODO: add getopt
if (argc == 3)
{
if (0 == strcmp(argv[1], "-v"))
{
host = argv[2];
verbose = 1;
}
else if (0 == strcmp(argv[2], "-v"))
{
host = argv[1];
verbose = 1;
}
else
{
usage(argv[0]);
return -1;
}
}
else
{
host = argv[1];
}
// check if host is alive //
struct hostent * server = gethostbyname(host);
if (server == NULL)
{
fprintf(stderr, "%s: no such host\n", host);
return -1;
}
// verify white lists first //
int restatus = check_whitelist(host);
if (restatus == 0)
{
printf("%s is in white list\n", host);
return 0;
}
else
{
printf("%s not detected in white list.\n", host);
}
// calling ASN RESOLVER //
char * asn_name;
char * asn_details;
restatus = get_asn(host, &asn_name, &asn_details);
if(restatus < 0)
{
fprintf(stderr, "ASN resolver failed, status=%d\n", restatus);
}
else
{
printf("ASN=%s (%s)\n", asn_name, asn_details);
free(asn_details);
// verify black asn lists first //
restatus = check_asn(asn_name);
if (restatus == 0)
{
printf("*** ASN %s in black list! ***\n", asn_name);
free(asn_name);
return 0;
}
else if (restatus > 0)
{
printf("ASN not detected as black.\n");
free(asn_name);
}
else
{
fprintf(stderr, "Cannot determine ASN status.\n");
free(asn_name);
}
}
char * cc = get_cc_from_domain(host);
if (cc)
{
printf("Country: %s\n", cc);
free(cc);
}
else
{
fprintf(stderr, "Oops, failed to retrieve CC\n");
}
int rc;
struct flux_entry * flux;
rc = get_flux(host, &flux);
if (rc < 0)
{
fprintf(stderr, "Cannot retrieve fast flux information\n");
}
else
{
struct flux_entry * current = flux;
if (current->addr_str)
{
printf("Printing fast flux analysis for %s...\n", host);
}
int count = 0;
for (; current->addr_str; ++current, ++count)
{
printf("\t%s", current->addr_str);
free(current->addr_str);
if (current->cc[0])
{
printf(" (%c%c)", current->cc[0], current->cc[1]);
}
printf("\n");
}
if (count > 1)
{
printf("%s is a suspected flux domain\n", host);
}
free(flux);
}
rc = check_home(host, verbose);
if (rc < 0)
{
fprintf(stderr, "Failed retrieving home page for %s\n", host);
}
else if (rc > 0)
{
printf("Host %s does not match known signatures\n", host);
}
return rc;
}