├── COPYRIGHT ├── LICENSE ├── README.md ├── dnsdb-query └── dnsdb_query.py /COPYRIGHT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 by Farsight Security, Inc. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | Copyright (c) 2010-2012 by Internet Systems Consortium, Inc. ("ISC") 16 | 17 | Permission to use, copy, modify, and/or distribute this software for any 18 | purpose with or without fee is hereby granted, provided that the above 19 | copyright notice and this permission notice appear in all copies. 20 | 21 | THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 22 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 23 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 24 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 25 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 26 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 27 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | dnsdb-query 2 | =========== 3 | 4 | These clients are reference implementations of the [DNSDB HTTP API](https://api.dnsdb.info/). Output is 5 | compliant with the [Passive DNS Common Output Format](http://tools.ietf.org/html/draft-dulaunoy-kaplan-passive-dns-cof-01). 6 | 7 | Please see https://www.dnsdb.info/ for more information. 8 | 9 | Requirements 10 | ------------ 11 | * Linux, BSD, OS X 12 | * Curl 13 | * Python 2.7.x 14 | * Farsight DNSDB API key 15 | 16 | Installation 17 | ------------ 18 | 1. Create a directory 19 | 20 | ``` 21 | mkdir ~/dnsdb 22 | ``` 23 | 1. Download the software 24 | 25 | ``` 26 | curl https://codeload.github.com/dnsdb/dnsdb-query/tar.gz/debian/0.2-1 -o ~/dnsdb/0.2-1.tar.gz 27 | ``` 28 | 1. Extract the software 29 | 30 | ``` 31 | tar xzvf ~/dnsdb/0.2-1.tar.gz -C ~/dnsdb/ --strip-components=1 32 | ``` 33 | 1. Create a API key file 34 | 35 | ``` 36 | nano ~/.dnsdb-query.conf 37 | ``` 38 | 1. Cut and paste the following and replace '\' with your API Key 39 | 40 | ``` 41 | APIKEY="" 42 | ``` 43 | 1. Test the Python client 44 | 45 | ``` 46 | $ python dnsdb/dnsdb_query.py -i 104.244.13.104 47 | ``` 48 | ``` 49 | ... 50 | www.farsightsecurity.com. IN A 104.244.13.104 51 | ``` 52 | 53 | dnsdb_query.py 54 | -------------- 55 | 56 | dnsdb_query.py is a Python client for the DNSDB HTTP API. It is similar 57 | to the dnsdb-query shell script but supports some additional features 58 | like sorting and setting the result limit parameter. It is also embeddable 59 | as a Python module. 60 | 61 | ``` 62 | Usage: dnsdb_query.py [options] 63 | 64 | Options: 65 | -h, --help show this help message and exit 66 | -c CONFIG, --config=CONFIG 67 | config file 68 | -r RRSET, --rrset=RRSET 69 | rrset [/[/BAILIWICK]] 70 | -n RDATA_NAME, --rdataname=RDATA_NAME 71 | rdata name [/] 72 | -i RDATA_IP, --rdataip=RDATA_IP 73 | rdata ip 74 | -s SORT, --sort=SORT sort key 75 | -R, --reverse reverse sort 76 | -j, --json output in JSON format 77 | -l LIMIT, --limit=LIMIT 78 | limit number of results 79 | -d, --debug print debug output 80 | 81 | --before=BEFORE only output results seen before this time 82 | --after=AFTER only output results seen after this time 83 | 84 | Time formats are: "%Y-%m-%d", "%Y-%m-%d %H:%M:%S", "%d" (UNIX timestamp), 85 | "-%d" (Relative time in seconds), BIND format relative timestamp (e.g. 1w1h, 86 | (w)eek, (d)ay, (h)our, (m)inute, (s)econd) 87 | ``` 88 | 89 | Or, from Python: 90 | 91 | ``` 92 | from dnsdb_query import DnsdbClient 93 | 94 | server='https://api.dnsdb.info' 95 | apikey='d41d8cd98f00b204e9800998ecf8427e' 96 | 97 | client = DnsdbClient(server,apikey) 98 | for rrset in client.query_rrset('www.dnsdb.info'): 99 | # rrset is a decoded JSON blob 100 | print repr(rrset) 101 | ``` 102 | 103 | Other configuration options that may be set: 104 | 105 | `DNSDB_SERVER` 106 | The base URL of the DNSDB HTTP API, minus the /lookup component. Defaults to 107 | `https://api.dnsdb.info.` 108 | 109 | `HTTP_PROXY` 110 | The URL of the HTTP proxy that you wish to use. 111 | 112 | `HTTPS_PROXY` 113 | The URL of the HTTPS proxy that you wish to use. 114 | 115 | dnsdb-query 116 | ----------- 117 | 118 | dnsdb-query is a simple curl-based wrapper for the DNSDB HTTP API. 119 | 120 | The script sources the config file `/etc/dnsdb-query.conf` as a shell fragment. 121 | If the config file is not present in `/etc`, the file `$HOME/.dnsdb-query.conf` 122 | is sourced instead. 123 | 124 | The config file MUST set the value of the APIKEY shell variable to the API 125 | key provided to you by Farsight Security. 126 | 127 | For example, if your API key is d41d8cd98f00b204e9800998ecf8427e, place the 128 | following line in `/etc/dnsdb-query.conf` or `$HOME/.dnsdb-query.conf`: 129 | 130 | ``` 131 | APIKEY="d41d8cd98f00b204e9800998ecf8427e" 132 | ``` 133 | 134 | Other shell variables that may be set via the config file or command line 135 | are: 136 | 137 | `DNSDB_SERVER` 138 | The base URL of the DNSDB HTTP API, minus the /lookup component. Defaults to 139 | `https://api.dnsdb.info.` 140 | 141 | `DNSDB_FORMAT` 142 | The result format to use, either text or json. Defaults to text. 143 | 144 | `HTTP_PROXY` 145 | The URL of the HTTP proxy that you wish to use. 146 | 147 | `HTTPS_PROXY` 148 | The URL of the HTTPS proxy that you wish to use. 149 | 150 | dnsdb-query supports the following usages: 151 | 152 | ``` 153 | Usage: dnsdb-query rrset [/[/]] 154 | Usage: dnsdb-query rdata ip 155 | Usage: dnsdb-query rdata name [/] 156 | Usage: dnsdb-query rdata raw [/] 157 | ``` 158 | 159 | If your rrname, bailiwick or rdata contains the `/` character you 160 | will need to escape it to `%2F` on the command line. eg: 161 | 162 | `./dnsdb_query.py -r 1.0%2F1.0.168.192.in-addr.arpa` 163 | 164 | retrieves the rrsets for `1.0/1.0.168.192.in-addr.arpa`. 165 | -------------------------------------------------------------------------------- /dnsdb-query: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | # Copyright (c) 2013 by Farsight Security, Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | if [ -r "/etc/dnsdb-query.conf" ]; then 18 | . /etc/dnsdb-query.conf 19 | fi 20 | 21 | if [ -r "$HOME/.dnsdb-query.conf" ]; then 22 | . $HOME/.dnsdb-query.conf 23 | fi 24 | 25 | if [ -z "$APIKEY" ]; then 26 | echo "$(basename $0): APIKEY not defined in /etc/dnsdb-query.conf or $HOME/.dnsdb-query.conf" 27 | exit 1 28 | fi 29 | 30 | if [ -z "$DNSDB_SERVER" ]; then 31 | DNSDB_SERVER="https://api.dnsdb.info" 32 | fi 33 | 34 | if [ -z "$CURL" ]; then 35 | CURL="curl" 36 | fi 37 | 38 | if [ -z "$DNSDB_FORMAT" ]; then 39 | DNSDB_FORMAT="text" 40 | fi 41 | 42 | export HTTP_PROXY HTTPS_PROXY 43 | 44 | case "$DNSDB_FORMAT" in 45 | json) 46 | ACCEPT="application/json" 47 | ;; 48 | text) 49 | ACCEPT="text/plain" 50 | ;; 51 | *) 52 | echo "$(basename $0): unknown value for DNSDB_FORMAT: $DNSDB_FORMAT" 53 | exit 1 54 | esac 55 | 56 | DNSDB_RRSET_LOOKUP="${DNSDB_SERVER}/lookup/rrset" 57 | DNSDB_RDATA_LOOKUP="${DNSDB_SERVER}/lookup/rdata" 58 | 59 | usage() { 60 | echo "Usage: $(basename $0) rrset [/[/]]" 61 | echo "Usage: $(basename $0) rdata ip " 62 | echo "Usage: $(basename $0) rdata name [/]" 63 | echo "Usage: $(basename $0) rdata raw [/]" 64 | exit 1 65 | } 66 | 67 | case "$1" in 68 | rrset) 69 | oname="$2" 70 | if [ -z "$oname" ]; then 71 | usage 72 | fi 73 | $CURL --path-as-is -Ss -H "X-API-Key: ${APIKEY}" -H "Accept: ${ACCEPT}" ${DNSDB_RRSET_LOOKUP}/name/$oname 74 | ;; 75 | 76 | rdata) 77 | lookup_type="$2" 78 | rdata="$3" 79 | if [ -z "$lookup_type" -o -z "$rdata" ]; then 80 | usage 81 | fi 82 | case "$lookup_type" in 83 | ip) 84 | rdata="$(echo $rdata | sed -e 's#/#,#')" 85 | ;; 86 | esac 87 | case "$lookup_type" in 88 | ip|name|raw) 89 | $CURL --path-as-is -Ss -H "X-API-Key: ${APIKEY}" -H "Accept: ${ACCEPT}" ${DNSDB_RDATA_LOOKUP}/$lookup_type/$rdata 90 | ;; 91 | *) 92 | usage 93 | ;; 94 | esac 95 | ;; 96 | 97 | *) 98 | usage 99 | ;; 100 | esac 101 | -------------------------------------------------------------------------------- /dnsdb_query.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright (c) 2013 by Farsight Security, Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | import calendar 18 | import errno 19 | import locale 20 | import optparse 21 | import os 22 | import re 23 | import sys 24 | import time 25 | import urllib 26 | import urllib2 27 | from cStringIO import StringIO 28 | 29 | try: 30 | import json 31 | except ImportError: 32 | import simplejson as json 33 | 34 | DEFAULT_CONFIG_FILES = filter(os.path.isfile, ('/etc/dnsdb-query.conf', os.path.expanduser('~/.dnsdb-query.conf'))) 35 | DEFAULT_DNSDB_SERVER = 'https://api.dnsdb.info' 36 | DEFAULT_HTTP_PROXY = '' 37 | DEFAULT_HTTPS_PROXY = '' 38 | 39 | cfg = None 40 | options = None 41 | debug = False # set to True to print raw queries and responses 42 | 43 | locale.setlocale(locale.LC_ALL, '') 44 | 45 | class QueryError(Exception): 46 | pass 47 | 48 | class DnsdbClient(object): 49 | def __init__(self, server, apikey, limit=None, http_proxy=None, https_proxy=None): 50 | self.server = server 51 | self.apikey = apikey 52 | self.limit = limit 53 | self.http_proxy = http_proxy 54 | self.https_proxy = https_proxy 55 | 56 | def query_rrset(self, oname, rrtype=None, bailiwick=None, before=None, after=None): 57 | if bailiwick: 58 | if not rrtype: 59 | rrtype = 'ANY' 60 | path = 'rrset/name/%s/%s/%s' % (quote(oname), rrtype, quote(bailiwick)) 61 | elif rrtype: 62 | path = 'rrset/name/%s/%s' % (quote(oname), rrtype) 63 | else: 64 | path = 'rrset/name/%s' % quote(oname) 65 | return self._query(path, before, after) 66 | 67 | def query_rdata_name(self, rdata_name, rrtype=None, before=None, after=None): 68 | if rrtype: 69 | path = 'rdata/name/%s/%s' % (quote(rdata_name), rrtype) 70 | else: 71 | path = 'rdata/name/%s' % quote(rdata_name) 72 | return self._query(path, before, after) 73 | 74 | def query_rdata_ip(self, rdata_ip, before=None, after=None): 75 | path = 'rdata/ip/%s' % rdata_ip.replace('/', ',') 76 | return self._query(path, before, after) 77 | 78 | def _query(self, path, before=None, after=None): 79 | res = [] 80 | url = '%s/lookup/%s' % (self.server, path) 81 | 82 | params = {} 83 | if self.limit: 84 | params['limit'] = self.limit 85 | if before and after: 86 | params['time_first_after'] = after 87 | params['time_last_before'] = before 88 | else: 89 | if before: 90 | params['time_first_before'] = before 91 | if after: 92 | params['time_last_after'] = after 93 | if params: 94 | url += '?{0}'.format(urllib.urlencode(params)) 95 | 96 | req = urllib2.Request(url) 97 | req.add_header('Accept', 'application/json') 98 | req.add_header('X-Api-Key', self.apikey) 99 | 100 | proxy_args = {} 101 | if self.http_proxy: 102 | proxy_args['http'] = self.http_proxy 103 | if self.https_proxy: 104 | proxy_args['https'] = self.https_proxy 105 | proxy_handler = urllib2.ProxyHandler(proxy_args) 106 | opener = urllib2.build_opener(proxy_handler) 107 | 108 | if debug: 109 | print >>sys.stderr, ";; query URL =", url 110 | 111 | try: 112 | http = opener.open(req) 113 | while True: 114 | line = http.readline() 115 | if not line: 116 | break 117 | if debug: 118 | print >>sys.stderr, ";; response =", line.strip() 119 | yield json.loads(line) 120 | except (urllib2.HTTPError, urllib2.URLError), e: 121 | raise QueryError, str(e), sys.exc_traceback 122 | 123 | def quote(path): 124 | return urllib.quote(path, safe='') 125 | 126 | def sec_to_text(ts): 127 | return time.strftime('%Y-%m-%d %H:%M:%S -0000', time.gmtime(ts)) 128 | 129 | def rrset_to_text(m): 130 | s = StringIO() 131 | 132 | try: 133 | if 'bailiwick' in m: 134 | s.write(';; bailiwick: %s\n' % m['bailiwick']) 135 | 136 | if 'count' in m: 137 | s.write(';; count: %s\n' % locale.format('%d', m['count'], True)) 138 | 139 | if 'time_first' in m: 140 | s.write(';; first seen: %s\n' % sec_to_text(m['time_first'])) 141 | if 'time_last' in m: 142 | s.write(';; last seen: %s\n' % sec_to_text(m['time_last'])) 143 | 144 | if 'zone_time_first' in m: 145 | s.write(';; first seen in zone file: %s\n' % sec_to_text(m['zone_time_first'])) 146 | if 'zone_time_last' in m: 147 | s.write(';; last seen in zone file: %s\n' % sec_to_text(m['zone_time_last'])) 148 | 149 | if 'rdata' in m: 150 | for rdata in m['rdata']: 151 | s.write('%s IN %s %s\n' % (m['rrname'], m['rrtype'], rdata)) 152 | 153 | s.seek(0) 154 | return s.read() 155 | finally: 156 | s.close() 157 | 158 | def rdata_to_text(m): 159 | return '%s IN %s %s' % (m['rrname'], m['rrtype'], m['rdata']) 160 | 161 | def parse_config(cfg_files): 162 | config = {} 163 | 164 | if not cfg_files: 165 | raise IOError(errno.ENOENT, 'dnsdb_query: No config files found') 166 | 167 | for fname in cfg_files: 168 | for line in open(fname): 169 | key, eq, val = line.strip().partition('=') 170 | val = val.strip('"') 171 | config[key] = val 172 | 173 | return config 174 | 175 | def time_parse(s): 176 | try: 177 | epoch = int(s) 178 | return epoch 179 | except ValueError: 180 | pass 181 | 182 | try: 183 | epoch = int(calendar.timegm(time.strptime(s, '%Y-%m-%d'))) 184 | return epoch 185 | except ValueError: 186 | pass 187 | 188 | try: 189 | epoch = int(calendar.timegm(time.strptime(s, '%Y-%m-%d %H:%M:%S'))) 190 | return epoch 191 | except ValueError: 192 | pass 193 | 194 | m = re.match(r'^(?=\d)(?:(\d+)w)?(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s?)?$', s, re.I) 195 | if m: 196 | return -1*(int(m.group(1) or 0)*604800 + \ 197 | int(m.group(2) or 0)*86400+ \ 198 | int(m.group(3) or 0)*3600+ \ 199 | int(m.group(4) or 0)*60+ \ 200 | int(m.group(5) or 0)) 201 | 202 | raise ValueError('Invalid time: "%s"' % s) 203 | 204 | def epipe_wrapper(func): 205 | def f(*args, **kwargs): 206 | try: 207 | return func(*args, **kwargs) 208 | except IOError as e: 209 | if e.errno == errno.EPIPE: 210 | sys.exit(e.errno) 211 | raise 212 | return f 213 | 214 | @epipe_wrapper 215 | def main(): 216 | global cfg 217 | global options 218 | global debug 219 | 220 | parser = optparse.OptionParser(epilog='Time formats are: "%Y-%m-%d", "%Y-%m-%d %H:%M:%S", "%d" (UNIX timestamp), "-%d" (Relative time in seconds), BIND format (e.g. 1w1h, (w)eek, (d)ay, (h)our, (m)inute, (s)econd)') 221 | parser.add_option('-c', '--config', dest='config', 222 | help='config file', action='append') 223 | parser.add_option('-r', '--rrset', dest='rrset', type='string', 224 | help='rrset [/[/BAILIWICK]]') 225 | parser.add_option('-n', '--rdataname', dest='rdata_name', type='string', 226 | help='rdata name [/]') 227 | parser.add_option('-i', '--rdataip', dest='rdata_ip', type='string', 228 | help='rdata ip ') 229 | parser.add_option('-t', '--rrtype', dest='rrtype', type='string', 230 | help='rrset or rdata rrtype') 231 | parser.add_option('-b', '--bailiwick', dest='bailiwick', type='string', 232 | help='rrset bailiwick') 233 | parser.add_option('-s', '--sort', dest='sort', type='string', help='sort key') 234 | parser.add_option('-R', '--reverse', dest='reverse', action='store_true', default=False, 235 | help='reverse sort') 236 | parser.add_option('-j', '--json', dest='json', action='store_true', default=False, 237 | help='output in JSON format') 238 | parser.add_option('-l', '--limit', dest='limit', type='int', default=0, 239 | help='limit number of results') 240 | parser.add_option('-d', '--debug', dest='debug', action='store_true', default=False, 241 | help='print debug output') 242 | 243 | parser.add_option('', '--before', dest='before', type='string', help='only output results seen before this time') 244 | parser.add_option('', '--after', dest='after', type='string', help='only output results seen after this time') 245 | 246 | options, args = parser.parse_args() 247 | if args: 248 | parser.print_help() 249 | sys.exit(1) 250 | 251 | debug = options.debug 252 | 253 | try: 254 | if options.before: 255 | options.before = time_parse(options.before) 256 | except ValueError, e: 257 | print 'Could not parse before: {}'.format(options.before) 258 | 259 | try: 260 | if options.after: 261 | options.after = time_parse(options.after) 262 | except ValueError, e: 263 | print 'Could not parse after: {}'.format(options.after) 264 | 265 | try: 266 | cfg = parse_config(options.config or DEFAULT_CONFIG_FILES) 267 | except IOError, e: 268 | print >>sys.stderr, str(e) 269 | sys.exit(1) 270 | 271 | if not 'DNSDB_SERVER' in cfg: 272 | cfg['DNSDB_SERVER'] = DEFAULT_DNSDB_SERVER 273 | if not 'HTTP_PROXY' in cfg: 274 | cfg['HTTP_PROXY'] = DEFAULT_HTTP_PROXY 275 | if not 'HTTPS_PROXY' in cfg: 276 | cfg['HTTPS_PROXY'] = DEFAULT_HTTPS_PROXY 277 | if not 'APIKEY' in cfg: 278 | sys.stderr.write('dnsdb_query: APIKEY not defined in config file\n') 279 | sys.exit(1) 280 | 281 | client = DnsdbClient(cfg['DNSDB_SERVER'], cfg['APIKEY'], 282 | limit=options.limit, 283 | http_proxy=cfg['HTTP_PROXY'], 284 | https_proxy=cfg['HTTPS_PROXY']) 285 | if options.rrset: 286 | if options.rrtype or options.bailiwick: 287 | qargs = (options.rrset, options.rrtype, options.bailiwick) 288 | else: 289 | qargs = (options.rrset.split('/', 2)) 290 | 291 | results = client.query_rrset(*qargs, before=options.before, after=options.after) 292 | fmt_func = rrset_to_text 293 | elif options.rdata_name: 294 | if options.rrtype: 295 | qargs = (options.rdata_name, options.rrtype) 296 | else: 297 | qargs = (options.rdata_name.split('/', 1)) 298 | 299 | results = client.query_rdata_name(*qargs, before=options.before, after=options.after) 300 | fmt_func = rdata_to_text 301 | elif options.rdata_ip: 302 | results = client.query_rdata_ip(options.rdata_ip, before=options.before, after=options.after) 303 | fmt_func = rdata_to_text 304 | else: 305 | parser.print_help() 306 | sys.exit(1) 307 | 308 | if options.json: 309 | fmt_func = json.dumps 310 | 311 | try: 312 | if options.sort: 313 | results = list(results) 314 | if len(results) > 0: 315 | if not options.sort in results[0]: 316 | sort_keys = results[0].keys() 317 | sort_keys.sort() 318 | sys.stderr.write('dnsdb_query: invalid sort key "%s". valid sort keys are %s\n' % (options.sort, ', '.join(sort_keys))) 319 | sys.exit(1) 320 | results.sort(key=lambda r: r[options.sort], reverse=options.reverse) 321 | for res in results: 322 | sys.stdout.write('%s\n' % fmt_func(res)) 323 | except QueryError, e: 324 | print >>sys.stderr, e.message 325 | sys.exit(1) 326 | 327 | if __name__ == '__main__': 328 | main() 329 | --------------------------------------------------------------------------------