From 8049744f5feb9761f3b771d685742a14747103b7 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 17 Feb 2017 19:07:02 -0700 Subject: [PATCH] setup query --- .gitignore | 3 + README.md | 69 +++++++++++++++++++ bin/dig.js | 187 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 26 +++++++ 4 files changed, 285 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 bin/dig.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e2b7cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +test/fixtures/*.json +node_modules +.*.sw* diff --git a/README.md b/README.md new file mode 100644 index 0000000..93ab931 --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ +dig.js +====== + +Create and capture DNS and mDNS query and response packets to disk as binary and/or JSON. +Options are similar to the Unix `dig` command. + +Install +------- + +```bash +npm install -g 'git+https://git@git.daplie.com/Daplie/dig.js.git#v1' +``` + +If you don't have `git` installed you can also try the npm repo: + +```bash +npm install -g dig.js +``` + +Usage +----- + +### Format + +```bash +dig.js [TYPE] +``` + +### Example + +```bash +dig.js daplie.com +``` + +### mDNS Browser Example + +This is pretty much an mDNS browser + +```bash +dig.js --mdns _services._dns-sd._udp.local +``` + +Really the `--mdns` option is just an alias for setting all of these options as the default: + +```bash +dig.js -p 5353 @224.0.0.251 PTR _services._dns-sd._udp.local +``` + +### Moar Examples + +```bash +dig.js A daplie.com + +dig.js -t A daplie.com + +dig.js @8.8.8.8 A daplie.com +``` + +Options +------- + +``` +--debug +--mdns +-t (superfluous) +-c +-p +-q (superfluous) +``` diff --git a/bin/dig.js b/bin/dig.js new file mode 100755 index 0000000..b3399d0 --- /dev/null +++ b/bin/dig.js @@ -0,0 +1,187 @@ +'use strict'; + +var dnsjs = require('../../dns-lint'); +var cli = require('cli'); +cli.parse({ +// 'b': [ false, 'set source IP address (defaults to 0.0.0.0)', 'string' ] + 'class': [ 'c', 'class (defaults to IN)', 'string', 'IN' ] +, 'debug': [ 'false', 'more verbose output', 'boolean', false ] +//, 'insecure': [ false, 'turn off RaNDOm cAPS required for securing queries'] +//, 'ipv4': [ '4', 'use ipv4 exclusively (defaults to false)', 'boolean', false ] +//, 'ipv6': [ '6', 'use ipv6 exclusively (defaults to false)', 'boolean', false ] +//, 'json': [ false, 'output results as json', 'string' ] +//, 'lint': [ false, 'attack (in the metaphorical sense) a nameserver with all sorts of queries to test for correct responses', 'string', false ] +, 'mdns': [ false, "Alias for setting defaults to -p 5353 @224.0.0.251 -t PTR -q _services._dns-sd._udp.local and waiting for multiple responses", 'boolean', false ] +, 'output': [ 'o', 'output prefix to use for writing query and response(s) to disk', 'file' ] +, 'port': [ 'p', 'port (defaults to 53 for dns and 5353 for mdns)', 'int' ] +//, 'serve': [ 's', 'path to json file with array of responses to issue for given queries', 'string' ] +, 'type': [ 't', 'type (defaults to ANY for dns and PTR for mdns)', 'string' ] +, 'query': [ 'q', 'a superfluous explicit option to set the query as a command line flag' ] +}); + +var PromiseA = require('bluebird'); +var fs = PromiseA.promisifyAll(require('fs')); +var dgram = require('dgram'); +var commonTypes = [ 'A', 'AAAA', 'CNAME', 'MX', 'NS', 'PTR', 'SOA', 'SRV', 'TXT' ]; + +function hexdump(ab) { + var ui8 = new Uint8Array(ab); + var bytecount = 0; + var head = ' 0 1 2 3 4 5 6 7 8 9 A B C D E F'; + var trail; + var str = [].slice.call(ui8).map(function (i) { + var h = i.toString(16); + if (h.length < 2) { + h = '0' + h; + } + return h; + }).join('').match(/.{1,2}/g).join(' ').match(/.{1,48}/g).map(function (str) { + var lead = bytecount.toString(16); + bytecount += 16; + + while (lead.length < 7) { + lead = '0' + lead; + } + + return lead + ' ' + str; + }).join('\n'); + trail = ab.byteLength.toString(16); + while (trail.length < 7) { + trail = '0' + trail; + } + return head + '\n' + str + '\n' + trail; +} + +cli.main(function (args, cli) { + args.forEach(function (arg) { + if (-1 !== commonTypes.indexOf(arg.toUpperCase())) { + if (cli.type) { + console.error("'type' was specified more than once"); + process.exit(1); + return; + } + cli.type = cli.t = arg.toUpperCase(); + } + + if (/^@/.test(arg)) { + if (cli.nameserver) { + console.error("'@server' was specified more than once"); + process.exit(1); + return; + } + cli.nameserver = cli.n = arg; + } + + if (cli.query) { + console.error("'@server' was specified more than once"); + process.exit(1); + return; + } + cli.query = cli.q = arg; + + }); + + if (cli.mdns) { + if (!cli.type) { + cli.type = cli.t = 'PTR'; + } + if (!cli.port) { + cli.port = cli.p = 5353; + } + } + + if (!cli.type) { + cli.type = cli.t = 'ANY'; + } + if (!cli.port) { + cli.port = cli.p = 53; + } + if (!cli.class) { + cli.class = cli.c = 'IN'; + } + if (!cli.query) { + console.error(''); + console.error('Usage:'); + console.error('dig.js [@server] [TYPE] [domain]'); + console.error(''); + console.error('Example:'); + console.error('dig.js daplie.com'); + console.error(''); + process.exit(1); + } + + var query = { + header: { + id: require('crypto').randomBytes(2).readUInt16BE(0) + , qr: 0 + , opcode: 0 + , aa: 0 // NA + , tc: 0 // NA + , rd: 1 + , ra: 0 // NA + , rcode: 0 // NA + } + , question: [ + { name: cli.query + , typeName: cli.type + , className: cli.class + } + ] + }; + var queryAb = dnsjs.DNSPacket.write(query); + + if (cli.debug) { + console.log(''); + console.log('DNS Question:'); + console.log(''); + console.log(query); + console.log(''); + console.log(hexdump(queryAb)); + console.log(''); + console.log(dnsjs.DNSPacket.parse(queryAb)); + console.log(''); + } + + + + + process.exit(1); + var server = dgram.createSocket({ + type: 'udp4' + , reuseAddr: true + }); + + var handlers = {}; + handlers.onError = function (err) { + console.error("error:", err.stack); + server.close(); + }; + handlers.onMessage = function (buffer) { + var path = require('path'); + var filename = type + '-' + count + '.mdns.bin'; + var fullpath = path.join('samples', filename); + + count += 1; + + fs.writeFileAsync(fullpath, buffer).then(function () { + console.log('wrote ' + buffer.length + ' bytes to ' + fullpath); + }); + }; + handlers.onListening = function () { + /*jshint validthis:true*/ + var server = this; + console.log(server.address()); + + server.setBroadcast(true); + server.addMembership('224.0.0.251'); + }; + + + server.on('error', handlers.onError); + server.on('message', handlers.onMessage); + server.on('listening', handlers.onListening); + + // 53 dns + // 5353 mdns + server.bind(5353); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..95248b5 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "dig.js", + "version": "1.0.0", + "description": "Create and capture DNS and mDNS query and response packets to disk as binary and/or JSON. Options are similar to the Unix `dig` command.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git@git.daplie.com:Daplie/dig.js.git" + }, + "keywords": [ + "dig", + "dns", + "mdns", + "lint", + "capture", + "create", + "bin", + "binary", + "json" + ], + "author": "AJ ONeal (https://coolaj86.com/)", + "license": "(MIT OR Apache-2.0)" +}