def generation_normal(file_path): if os.path.exists(file_path): ip_count = ip_number(file_path) name = str(date_util.get_now_timestamp()) for port in port_protocols.keys(): protocol_str = "" for protocol in port_protocols.get(port): if protocol in unfinished: continue if protocol_str != "": protocol_str += "," protocol_str += protocol file1 = settings.zmap_result_path + name + "_" + str(port) + ".csv" command = [ 'zmap', '-w', file_path, '--probe-module=icmp_echoscan', '-r', settings.normal_scan_rate, '-p', str(port), ' | ztee', file1 ] com_str = " ".join(command) _id = get_sha1(com_str) models.ScanTask.objects.create(id=_id, command=com_str, port=port, protocol=protocol_str, ip_range="*", ip_count=ip_count, port_result_path=file1, issue_time=timezone.now(), execute_status=0, priority=5).save()
def generate_execute(scan_method, pps, protocols, ports, parent_id, all_path): execute_status = 0 if scan_method == "socket": execute_status = 2 protocol_list = protocols.split(",") for path in all_path: number = ip_number(path) if number == 0: continue # ping扫不依赖端口如何处理?? if scan_method == "telnet" or scan_method == "socket": for port in ports: file1 = path + "_" + str(port) command = ['zmap', '-w', path, '-r', pps, '-p', str(port), ' | ztee', file1] command_str = " ".join(command) _id = get_sha1(command_str) banner_task_count = -1 if scan_method == "socket": banner_task_count = len(protocol_list) models.ScanTask.objects.create(id=_id, parent_id=parent_id, command=command_str, port=port, protocol=protocols, ip_range="*", ip_count=number, port_result_path=file1, issue_time=timezone.now(), execute_status=execute_status, banner_task_count=banner_task_count, priority=3).save() if scan_method == "socket": for protocol in protocol_list: if protocol in common.unfinished: continue banner_command = ["zgrab2", "-f", path, protocol, "-p", str(port), "-t 5s"] banner_id = hash_util.get_md5(" ".join(banner_command)) zgrab_result_path = settings.banner_save_path + protocol + "_" + str(port) + "_" + banner_id + ".json" banner_command.append('--output-file=' + zgrab_result_path) models.BannerTask.objects.create(id=banner_id, command=" ".join(banner_command), port=port, protocol=protocol, ip_count=number, scan_task_id=_id, banner_result_path=zgrab_result_path, priority=3, create_time=timezone.now()) elif scan_method == "ping": file1 = settings.zmap_result_path + '_' + path_get_filename(path) command = ['zmap', '-w', path, '-r', pps, ' | ztee', file1] _id = get_sha1(" ".join(command)) models.ScanTask.objects.create(id=_id, parent_id=parent_id, command=" ".join(command), port=format_list(ports), protocol=protocols, ip_range="*", ip_count=number, port_result_path=file1, issue_time=timezone.now(), execute_status=execute_status, priority=3).save()
def generate_task(request): """ 探测任务生成 :param request: :return: """ if request.method == 'POST': scan_type = request.POST.get("scan_type") if scan_type not in ["normal", "deep"]: return JsonResponse('{"msg", "非法请求!"}', safe=False) ip_list = request.POST.get("ip_list") if len(ip_list) == 0: return JsonResponse('{"msg", "缺少必要的参数!"}', safe=False) exclude_list = request.POST.get("exclude_list", "") pps = request.POST.get("pps", 666) scan_method = request.POST.get("scan_method", "telnet") if scan_method not in ["telnet", "ping", "socket"]: return JsonResponse('{"msg", "错误的扫描方式!"}', safe=False) protocols = request.POST.get("protocols") ports = request.POST.get("ports") ports_list = list() if scan_type == "normal": # 常规配置扫描 if len(protocols) == 0 or len(ports) == 0: return JsonResponse('{"msg", "缺少必要的参数!"}', safe=False) ports_list = str(ports).split(",") elif scan_type == "deep": protocols = format_list(protocol_ports.keys()) ports = "1-65535" ports_list = range(1, 65536) username = request.POST.get("username") # 处理扫描任务IP列表 general_id = get_sha1(username + scan_type + scan_method + pps + protocols + ip_list + exclude_list) if models.GeneralScanTask.objects.filter(id=general_id): return JsonResponse('{"msg", "任务已存在!"}', safe=False) all_path = generate_scan_file(ip_list, exclude_list) ip_count = 0 for path in all_path: ip_count += ip_number(path) all_task = len(ports_list)*len(protocols.split(",")) if scan_method != "socket": all_task += len(ports_list) models.GeneralScanTask.objects.create(id=general_id, scan_type=scan_type, scan_method=scan_method, ip_list=ip_list, port=ports, protocol=protocols, scan_speed=pps, exclude_list=exclude_list, issue_time=timezone.now(), ip_count=ip_count, username=username, execute_status=0, all_sub_task_count=all_task).save() _thread.start_new_thread(generate_execute, (scan_method, pps, protocols, ports_list, general_id, all_path)) return JsonResponse('{"msg", "success"}', safe=False) else: return JsonResponse('{"msg", "请使用POST方法!"}', safe=False)
for protocol in port_protocols.get(port): if protocol in unfinished: continue # if protocol_str != "": # protocol_str += "," # protocol_str += protocol file1 = '/opt/recon/zmap/172.16.0.0_' + str(protocol) + "_" + str( port) + ".csv" file2 = '/opt/recon/zgrab/172.16.0.0_' + str(protocol) + "_" + str( port) + ".json" command = [ 'zmap', '172.16.0.0/16', '--probe-module=icmp_echoscan', '-r', '2000', '-p', str(port), ' --output-fields=* | ztee', file1, '| zgrab2', protocol, '-p', str(port), '--output-file=' + file2 ] com_str = " ".join(command) _id = get_sha1(com_str) dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") insert = "INSERT INTO client_scantask VALUES" \ "('{}', '{}', {}, '{}', '{}', {}, 0, '{}', '{}', " \ "NULL, NULL, NULL, 0, 0, 0, -1, {})" \ .format(_id, com_str, port, "", "*", ip_count, file1, dt, 5) print(insert) client.execute(insert) db.commit()