1/*
2 * Copyright (c) 2014-2021 by Farsight Security, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17Table of Contents:
18
19 * Introduction
20 * Dependencies needed
21 * Installing dependencies
22 * Building and installing
23 * Getting Started
24 * Background on ASINFO/CIDR lookups and annotations
25
26Introduction:
27
28 This is a pure C99 program that accesses passive DNS database
29 systems such as:
30 * the DNSDB API server at Farsight Security
31 * the CIRCL pDNS server at Computer Incident Response Center
32 (LU)
33
34 An API key is required for operation. The command syntax was
35 inspired by a python script called dnsdb_query, but significant
36 departure has occured, largely inspired by a modern understanding
37 of "time fencing" and a desire for new features such as CSV output
38 and JSON reprocessing.
39
40 NOTE: Prior to version 2.5.3, the default pDNS system supported
41 was Farsight Security APIv1, and it was called "dnsdb". As of
42 version 2.5.3, the default system is Farsight Security APIv2 and
43 system "dnsdb2" is synonymous with "dnsdb". For APIv1, specify
44 "dnsdb1" using the command line -u option or the DNSDBQ_SYSTEM
45 variable.
46
47
48Dependencies needed:
49 jansson (2.5 or later)
50 libcurl (7.28 or later)
51 modern compiler (clang or GCC)
52
53
54Installing dependencies:
55
56 On Debian 8 Linux:
57 apt-get install libcurl4-openssl-dev
58 apt-get install libjansson-dev
59
60 On CentOS 6 Linux:
61 # Based on PHP instructions for installing libcurl...
62 wget http://curl.haxx.se/download/curl-7.28.1.tar.gz
63 tar xvzf curl-7.28.1.tar.gz
64 cd curl-7.28.1/
65 ./configure --with-libssh2 --enable-ares=/usr/local/ --enable-ipv6
66 make
67 make install
68
69 # lib jansson
70 wget http://www.digip.org/jansson/releases/jansson-2.5.tar.gz
71 tar -xpzf jansson-2.5.tar.gz
72 cd jansson-2.5
73 ./configure
74 make
75 make install
76
77 echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf
78 ldconfig
79
80 On CentOS 7 Linux:
81 yum install libcurl-devel.x86_64
82 yum install jansson-devel.x86_64
83 yum install centos-release-scl
84 yum install devtoolset-8
85 scl enable devtoolset-8 bash
86 # do the build from that bash
87
88 On CentOS 8 Linux:
89 dnf install gcc
90 dnf install jansson.x86_64 jansson-devel.x86_64
91 dnf install libcurl-devel.x86_64
92
93 On FreeBSD 10:
94 pkg install curl jansson
95
96 On macOS:
97 brew install jansson
98
99
100Building and installing:
101
102 (Assumes you have "git")
103
104 git clone https://github.com/dnsdb/dnsdbq.git
105 cd dnsdbq
106 make install clean
107
108 On FreeBSD, you may need to remove -lresolv in the LIBS line of
109 the Makefile.
110
111 On macOS, if you want an almost static dnsdbq binary on macOS,
112 that is, one without any non-System library dependencies, you can
113 rebuild dnsdbq with a static jansson library. That binary could
114 then be deployed on any identical macOS version and architecture.
115
116 1. Find the static jansson library, probably
117 /usr/local/lib/libjansson.a as installed by brew.
118 2. Change the Makefile's line
119 JANSLIBS = -L/usr/local/lib -ljansson
120 to instead specify the static library location, probably to:
121 JANSLIBS = /usr/local/lib/libjansson.a
122 3. Then run make
123
124
125Getting Started:
126
127 Add the API key to ~/.dnsdb-query.conf in the below given format,
128 APIKEY="YOURAPIKEYHERE"
129
130 If you're interested in purchasing a Farsight DNSDB subscription,
131 please contact sales@farsightsecurity.com. Farsight also has a
132 grant program for selected researchers, investigative journalists,
133 and cybersecurity workers at some public benefit non-profits.
134 See https://www.farsightsecurity.com/grant-access/
135
136 Here's an example query and output after dnsdbq is compiled:
137
138 $ ./dnsdbq -r farsightsecurity.com/A -l 1
139 ;; record times: 2013-09-25 15:37:03 .. 2015-04-01 06:17:25 (~1y ~187d)
140 ;; count: 6350; bailiwick: farsightsecurity.com.
141 farsightsecurity.com. A 66.160.140.81
142
143Background on ASINFO/CIDR lookups and annotations:
144
145 Annotating IP addresses with ASN information can help an analyst
146 focus their attention on unusual or unexpected ASNs (for example,
147 perhaps a domestic US corporation's IP address inexplicably ended
148 up being originated by a foreign consumer ISP).
149
150 This code has been tested against three sources of ASN information,
151 each of which are valid arguments to the -D parameter.
152
153 1. asn.routeviews.org (the default value for the -D parameter).
154
155 Given an IPv4 in reverse order, this returns a space
156 separated three-tuple:
157 "ASN" "CIDR prefix" "prefix length"
158
159 For badly formatted addresses, it returns:
160 "4294967295" "0" "0"
161
162 For unknown addresses, it returns no answers.
163
164 Examples:
165 $ dig +short 34.168.254.125.asn.routeviews.org TXT
166 "23724" "125.254.168.0" "21"
167 $ dig +short a.b.c.d.asn.routeviews.org TXT
168 "4294967295" "0" "0"
169 $ dig +short 0.0.0.128.aspath.routeviews.org TXT
170 $
171
172 2. aspath.routeviews.org
173
174 The same as asn.routeviews.org, except it returns a
175 three-tuple: AS path, CIDR prefix, prefix length.
176
177 Examples:
178 $ dig +short 0.0.0.4.aspath.routeviews.org TXT
179 "3303 3356" "4.0.0.0" "9"
180
181 3. origin.asn.cymru.com
182
183 Given an IPv4 in reverse order, this returns a pipe-separated
184 five-tuple:
185 ASN | CIDR prefix/prefix length | CC | Registry | Allocated date
186
187 Example:
188 $ dig +short 0.0.0.4.origin.asn.cymru.com TXT
189 "3356 | 4.0.0.0/9 | US | arin | 1992-12-01"
190
191 Function asinfo_from_dns() in asinfo.c has specific code to parse
192 those formats. asn.routeviews.org and aspath.routeviews.org do
193 not currently handle IPv6 addresses properly, so dnsdbq does not
194 support IPv6 annotation now.
195
196 There is a complication that arises when doing those sort of IP to
197 ASN mappings, however: publicly available IP to ASN zones are
198 based on currently observed IP to ASN mappings, while the IPs that
199 are being mapped may have been seen in passive DNS months or even
200 years earlier, when that IP may have been originated by a
201 different ASN.
202
203 Often the IP to ASN mappings are quite static, in which case
204 historical IPs will map just fine using the current IP to ASN
205 data. On the other hand, some IPs may have been hijacked and used
206 without authorization, or transferred, or otherwise ended up going
207 from one ASN to another.
208
209 Therefore the IP to ASN mapping should be viewed as an
210 experimental best effort feature, and interpreted with care.
211
212 In addition to the issue of potential ASN misalignment, the size
213 and origin of the reported origin IP and prefix length may also
214 have changed over time. For example, a /19 may have been
215 de-aggregated into a set of more specific /24's. Again, we report
216 the state of the world as it is currently seen by the service used
217 for the IP to ASN mapping.
218
219 Other miscellaneous notes:
220
221 * dnsdbq does not support mapping IPv6 addresses to ASNs at this
222 time.
223
224 * In the case of Multiple Origin ASNs, typically IP to ASN
225 services will report one of the multiple ASNs; other origin
226 ASNs may also exist but not be reported.
227
228 * We do not map ASNs to their owner or the owner description
229 string. Please see WHOIS for information about the entity
230 currently assigned a given ASN.
231
232 We welcome feedback on this feature. Do you use it and find it
233 useful? Are there IP-to-ASN service providers we've inadvertently
234 omitted? Share your feedback by writing
235 <support@farsightsecurity.com>.
236