forked from ChiChou/grab.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·63 lines (55 loc) · 1.56 KB
/
cli.js
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
#!/usr/bin/env node
'use strict'
const grabber = require('./lib')
const program = require('commander')
const fs = require('fs')
const path = require('path')
const readline = require('readline')
const read = filename => fs.readFileSync(grabber.payload(filename), 'utf8')
program
.version(require('./package.json').version)
.option('-p, --port <n>', 'port number', parseInt)
.option('-s, --tls', 'with tls')
.option('--payload <file>', 'payload file', read)
.option('--parser <parser>', 'parse with nmap rule')
.option('--encoding [encoding]', 'encode banner', /^(base64|hex)$/)
.option('--timeout <timeout>', 'timeout', parseInt)
.parse(process.argv)
function run(parser) {
let parse = data => data
if (parser) {
parse = data => {
let parsed = parser(data)
Object.keys(parsed).forEach(key => (data[key] = parsed[key]))
return data
}
}
const grab = ip => grabber.grab(ip, program.port, {
tls: program.tls,
payload: program.payload,
timeout: program.timeout
})
.run()
.then(parse)
.then(data => {
data.ip = ip
data.banner = program.encoding ?
data.banner.toString(program.encoding) :
grabber.escape(data.banner)
return data
})
.catch(err => ({ ip: ip, error: err.message }))
.then(JSON.stringify)
.then(console.log)
readline
.createInterface({ input: process.stdin })
.on('line', grab)
}
if (program.parser) {
grabber.parser(program.parser).then(run).catch(() => {
console.error('Invalid parser name')
process.exit(-1)
})
} else {
run()
}