def nmap_resume_runner(ip, dir): outfile = ip.replace("/", "..") outfile = os.path.join(dir, outfile) args = ["nmap", "--resume", outfile + ".out"] aux.mnmap_msg("starting: " + pro + " " + str(args)) redirect_output(dir) os.execvp(pro, args)
def nmap_start_runner(ip, dir): outfile = ip.replace("/", "..") outfile = os.path.join(dir, outfile) args = ["nmap", "-sV", "-O", "-Pn", "-oX", outfile + ".xml", "-o", outfile + ".out", ip] aux.mnmap_msg("starting: " + pro + " " + str(args)) redirect_output(dir) os.execvp(pro, args)
def load_state(threads, ips, running_ips, init_file): aux.mnmap_msg('load the saved state') dir = init_file + '.dir' state_file = os.path.join(dir, init_file + '.pickle') with open(state_file, 'rb') as f: sstate = pickle.load(f) for i in reversed(ips): ips.remove(i) for i in sstate.ips: ips.append(i) for i in list(running_ips): del running_ips[i] tmp_running_ips = {} for i in sstate.running_ips: tmp_running_ips[i] = sstate.running_ips[i] for i in threads: threads.remove(i) for v in tmp_running_ips.values(): # check the number of line of output file, if it's less than, equal 1, treat as new scan vout = v.replace('/', '..') + '.out' vout = os.path.join(dir, vout) with open(vout, 'r') as f: vlines = f.readlines() if len(vlines) <=1: t = Process(target = nmap_start_runner, args = (v, dir)) t.daemon = True t.start() else: t = Process(target = nmap_resume_runner, args = (v, dir)) t.daemon = True t.start() threads.append(t) running_ips[t.name] = v aux.mnmap_msg('threads after resumed: ' + str(threads)) return sstate.next_ips
def save_state(threads, ips, running_ips, next_ips, init_file): dir = init_file + '.dir' state_file = os.path.join(dir, init_file + '.pickle') aux.mnmap_msg('saving state') aux.mnmap_msg('killing all child-processes') aux.mnmap_msg('threads before mass-kill: ' + str(threads)) for t in threads: try: t.terminate() except: 'At ['+ str(datetime.now()) + '] script, do not even try to kill a ZOMBIES' sstate = state(ips, running_ips, next_ips, init_file) with open(state_file, 'wb') as f: pickle.dump(sstate, f)
def scan(max_thread, action, init_file): # if sys.argv[0] == start, new scan # if sys.argv[0] == resume, resume scan ips = [] threads = [] running_ips = {} next_ips = 0 dir = init_file + '.dir' if action == 'start': aux.mnmap_msg("reading init file '" + init_file + "'") ips = aux.read_init_file(init_file, True) # check if there're previous scan if os.path.exists(dir) == True: answer = raw_input('previous scan result existed, continue starting? (y or n): ') if answer == 'y': pass else: exit(0) shutil.rmtree(dir, True) os.mkdir(dir) aux.mnmap_msg('you have chosen to start new scanning') for i in range(max_thread): t = Process(target = nmap_start_runner, args = (ips[i], dir)) t.daemon = True t.start() threads.append(t) running_ips[t.name] = ips[i] aux.mnmap_msg('initial threads: ' + str(threads)) next_ips = max_thread elif action == 'resume': aux.mnmap_msg('you have chosen to resume an old scanning') try: os.remove(os.path.join(init_file+'.dir', 'save')) except: pass next_ips = state.load_state(threads, ips, running_ips, init_file) else: aux.mnmap_msg('please use start/resume for your action') exit(1) if os.path.exists('html') == False: os.mkdir('html') ports = ['80'] aux.kill_process_using_port(ports) os.chdir('html/') os.system('python -m SimpleHTTPServer 80 >/dev/null 2>/dev/null &') os.chdir('..') while True: state.check_for_save(threads, ips, running_ips, next_ips, init_file) aux.test_network(threads, ips, running_ips, next_ips, init_file) aux.print_status(next_ips, ips) web.generate_html(ips, init_file, running_ips, next_ips) aux.remove_thread(threads, running_ips) if len(threads) < max_thread: if scan.start_flag == True: if next_ips < len(ips): aux.mnmap_msg('currently, the number of thread running is less than specified, starting a new one') t = Process(target = nmap_start_runner, args = (ips[next_ips], dir)) t.daemon = True t.start() threads.append(t) running_ips[t.name] = ips[next_ips] next_ips = next_ips + 1 aux.mnmap_msg('threads after added new one: ' + str(threads)) aux.mnmap_msg('running_ips after added new one: ' + str(running_ips)) else: aux.mnmap_msg('we have iterate over all the init lines') break else: # do nothing pass while True: state.check_for_save(threads, ips, running_ips, next_ips, init_file) aux.test_network(threads, ips, running_ips, next_ips, init_file) aux.print_status(next_ips, ips) web.generate_html(ips, init_file, running_ips, next_ips) aux.remove_thread(threads, running_ips) if(len(threads)==0): aux.mnmap_msg('we have finished, get out!') exit(0)
def save_state_exit(threads, ips, running_ips, next_ips, init_file): save_state(threads, ips, running_ips, next_ips, init_file) aux.mnmap_msg('self-killing for exit. Good bye!') os.system('kill $PPID')