6
6
from ibid.compat import defaultdict
7
7
from httplib import HTTPConnection, HTTPSConnection
8
8
from os.path import exists
9
from socket import gethostbyname, gaierror
9
10
from subprocess import Popen, PIPE
10
11
from sys import version_info
11
12
from urllib import getproxies_environment
21
from ibid.plugins import Processor, match
22
from ibid.plugins import Processor, match, authorise
22
23
from ibid.config import Option, IntOption, FloatOption, DictOption
23
24
from ibid.utils import file_in_path, unicode_output, human_join, \
25
url_to_bytestring, get_process_output
25
26
from ibid.utils.html import get_country_codes
502
503
event.addresponse(u"I don't know about any protocols using that port")
505
help['nmap'] = u'Finds open network ports on a host or scans a subnet for active hosts.'
506
class Nmap(Processor):
507
"""port scan <hostname>
508
net scan <network>/<prefix>"""
512
min_prefix = IntOption('min_prefix', 'Minimum network prefix that may be scanned', 24)
515
if not file_in_path('nmap'):
516
raise Exception("Cannot locate nmap executable")
518
@match(r'^(?:port\s+scan|nmap)\s+([0-9a-z.-]+)$')
520
def host_scan(self, event, host):
522
ip = gethostbyname(host)
524
event.addresponse(unicode(e.args[1]))
527
if ip.startswith('127.'):
528
event.addresponse(u"I'm not allowed to inspect my host's internal interface.")
531
output, error, code = get_process_output(['nmap', '--open', '-n', host])
535
for line in output.splitlines():
539
port, state, service = line.split()
540
ports.append('%s (%s)' % (port, service))
542
if line.startswith('Note: Host seems down.'):
543
event.addresponse(u'That host seems to be down')
545
if line.startswith('PORT'):
549
event.addresponse(human_join(ports))
551
event.addresponse(u'No open ports detected')
553
@match(r'^(?:net(?:work)?\s+scan|nmap)\s+((?:[0-9]{1,3}\.){3}[0-9]{1,3})/([0-9]{1,2})$')
555
def net_scan(self, event, network, prefix):
556
if int(prefix) < self.min_prefix:
557
event.addresponse(u"Sorry, I can't scan networks with a prefix less than %s", self.min_prefix)
560
output, error, code = get_process_output(['nmap', '-sP', '-n', '%s/%s' % (network, prefix)])
563
for line in output.splitlines():
564
if line.startswith('Host '):
565
hosts.append(line.split()[1])
568
event.addresponse(human_join(hosts))
570
event.addresponse(u'No hosts responded to pings')
504
572
# vi: set et sta sw=4 ts=4: