|
| 1 | +# Copyright 2015 Observable Networks |
| 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 | +# python builtins |
| 16 | +import json |
| 17 | +import logging |
| 18 | +from tempfile import NamedTemporaryFile |
| 19 | +from vendor.libnmap.process import NmapProcess |
| 20 | +from vendor.libnmap.parser import NmapParser, NmapParserException |
| 21 | + |
| 22 | +# local |
| 23 | +from service import Service |
| 24 | +from utils import utc |
| 25 | + |
| 26 | +OBSERVATION_TYPE = 'scan_observation_v1' |
| 27 | +SCAN_TARGETS = 'scan-config' |
| 28 | +SCAN_INTERVAL_SECONDS = 60 * 60 # Hourly. |
| 29 | +DEFAULT_NMAP_ARGS = ['nmap', '-oG', '-'] |
| 30 | +MAX_SIMULTANEOUS_TARGETS = 10 |
| 31 | + |
| 32 | + |
| 33 | +def _run_scan(ips, now): |
| 34 | + nmap = NmapProcess(ips) |
| 35 | + rc = nmap.run() |
| 36 | + if rc != 0: |
| 37 | + logging.error("nmap failed with error {}".format(rc)) |
| 38 | + return |
| 39 | + |
| 40 | + try: |
| 41 | + report = NmapParser.parse(nmap.stdout) |
| 42 | + except NmapParserException as e: |
| 43 | + logging.error("nmap parsing error? " + str(e)) |
| 44 | + return |
| 45 | + |
| 46 | + for host in report.hosts: |
| 47 | + ports = ['{}/{}'.format(s.port, s.state) |
| 48 | + for s in host.services] |
| 49 | + yield { |
| 50 | + 'observation_type': OBSERVATION_TYPE, |
| 51 | + 'time': now.isoformat(), |
| 52 | + 'source': host.address, |
| 53 | + 'ports': ', '.join(ports), |
| 54 | + 'info_type': 'services', |
| 55 | + 'result': '', |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +class NmapperService(Service): |
| 60 | + def __init__(self, *args, **kwargs): |
| 61 | + kwargs.update({ |
| 62 | + 'poll_seconds': SCAN_INTERVAL_SECONDS, |
| 63 | + }) |
| 64 | + super(NmapperService, self).__init__(*args, **kwargs) |
| 65 | + |
| 66 | + def _get_target_ips(self): |
| 67 | + json_resp = self.api.get_data(SCAN_TARGETS).json() |
| 68 | + objects = json_resp.get('objects', []) |
| 69 | + if not objects: |
| 70 | + return [] |
| 71 | + |
| 72 | + is_enabled = objects[0].get('is_enabled', True) |
| 73 | + if not is_enabled: |
| 74 | + return [] |
| 75 | + |
| 76 | + target_ips = objects[0].get('scan_targets', []) |
| 77 | + return target_ips |
| 78 | + |
| 79 | + def _upload_results(self, filename, now): |
| 80 | + path = self.api.send_file('logs', filename, now, suffix='nmap') |
| 81 | + data = { |
| 82 | + 'path': path, |
| 83 | + 'log_type': 'observations', |
| 84 | + } |
| 85 | + self.api.send_signal('logs', data) |
| 86 | + |
| 87 | + def execute(self, now=None): |
| 88 | + logging.info('getting target ips') |
| 89 | + target_ips = self._get_target_ips() |
| 90 | + logging.info('got {} target ips'.format(len(target_ips))) |
| 91 | + |
| 92 | + # timezoneify now |
| 93 | + if now: |
| 94 | + now = now.replace(tzinfo=utc) |
| 95 | + |
| 96 | + while target_ips: |
| 97 | + ips = target_ips[0:MAX_SIMULTANEOUS_TARGETS] |
| 98 | + target_ips = target_ips[MAX_SIMULTANEOUS_TARGETS:] |
| 99 | + |
| 100 | + results = _run_scan(ips, now) |
| 101 | + with NamedTemporaryFile() as f: |
| 102 | + if not results: |
| 103 | + continue |
| 104 | + for r in results: |
| 105 | + f.write(json.dumps(r) + '\n') |
| 106 | + |
| 107 | + f.seek(0) |
| 108 | + self._upload_results(f.name, now) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + scanner = NmapperService() |
| 113 | + scanner.run() |
0 commit comments