def main(): # {{{ resource.setrlimit(resource.RLIMIT_AS, (1 << 32, 1 << 32)) resource.setrlimit(resource.RLIMIT_FSIZE, (1 << 35, 1 << 35)) parser = create_parser() opts = parser.parse_args() with open(opts.configfn, 'r') as fd: sim.config = json.load(fd) logfile = sim.config.get('logfile', 'log.txt') loglevel = getattr(logging, sim.config.get('loglevel', 'DEBUG')) logging.basicConfig( filename=logfile, format='%(TIME)f %(filename)s:%(lineno)d/%(funcName)s %(message)s', level=loglevel) logger = logging.getLogger() logger.addFilter(SimulationTimeFilter()) logging.info('%s', json.dumps(sim.config)) sim.dist_host_on_time = parse_dist(sim.config['dists']['host_on_time'], 'on-time') sim.dist_host_off_time = parse_dist(sim.config['dists']['host_off_time'], 'off-time') logging.info('initialized on/off time distributions') sim.targeting_factory = targeting.create_factory(sim.config) logging.info('%s', sim.targeting_factory) sim.bot_factory = bots.create_factory(sim.config) logging.info('%s', sim.bot_factory) sim.host_tracker = hosts.HostTracker(sim.config) logging.info('%s', sim.host_tracker) sim.e2e_latency = hosts.E2ELatency(sim.config) logging.info('%s', sim.e2e_latency) master = hosts.Host(0, hosts.STATUS_VULNERABLE) master.on_time = 1e100 master.infect() sim.host_tracker.add(master) logging.info('initialized master bot') for hid in range(sim.host_tracker.vulnerable_period, sim.config['maxhid'] + 1, sim.host_tracker.vulnerable_period): off_time = sim.dist_host_off_time() ev = (sim.now + off_time, hosts.Host.bootup, hid) sim.enqueue(ev) logging.info('created %d bootup events', len(sim.evqueue)) assert (len(sim.evqueue) == (sim.config['maxhid'] // sim.host_tracker.vulnerable_period) + 1) while sim.evqueue and sim.now < sim.config['endtime']: _now, fn, data = sim.dequeue() logging.debug('dequeue len %d', len(sim.evqueue)) fn(data)
def shutdown(self, _none): infection = sim.now if self.status == STATUS_INFECTED: self.bot.teardown() infection = self.infection_time logging.info('hid %d on_time %f infected %f', self.hid, self.on_time, (sim.now - infection) / self.on_time) off_time = sim.dist_host_off_time() # pylint: disable=not-callable ev = (sim.now + off_time, Host.bootup, self.hid) sim.enqueue(ev) sim.host_tracker.delete(self)
def attempt_infect_end(self, delay): ev = (sim.now + delay, self.attempt_auth, None) sim.enqueue(ev)
def attempt_auth_failure(self, delay): ev = (sim.now + delay, self.attempt_auth, None) sim.enqueue(ev)
def attempt_auth_success(self, delay, hid): ev = (sim.now + delay, self.attempt_infect, hid) sim.enqueue(ev)
def start(self): for _i in range(self.nthreads): ev = (sim.now, self.attempt_auth, None) sim.enqueue(ev)
def attempt_auth_begin(self): ev = (sim.now + 1 / self.rate, self.attempt_auth, None) sim.enqueue(ev)
def start(self): ev = (sim.now, self.attempt_auth, None) sim.enqueue(ev)
def bootup(hid): host = Host(hid, STATUS_VULNERABLE) logging.info('hid %d on_time %f', hid, host.on_time) sim.host_tracker.add(host) ev = (sim.now + host.on_time, host.shutdown, None) sim.enqueue(ev)