-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamScan.py
294 lines (263 loc) · 12.7 KB
/
CamScan.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import subprocess
import re
import os
import socket
import nmap
import bonjour
from scapy.all import *
import requests
from bs4 import BeautifulSoup
def scan_for_cameras(verbose=False):
"""
Scans for cameras using various techniques.
Args:
verbose (bool, optional): If True, prints additional information about the scanning process. Defaults to False.
Returns:
list: A list of potential cameras found.
"""
cameras = []
# 1. Scan for USB Cameras
if verbose:
print("Scanning for USB cameras...")
try:
usb_devices = subprocess.check_output(['lsusb']).decode('utf-8')
usb_camera_regex = re.compile(r'^(.*) (.*): (.*) (.*) (.*)')
for line in usb_devices.splitlines():
match = usb_camera_regex.match(line)
if match:
# Check for common camera vendor and product IDs
if match.group(3) in ["046d", "04f2", "05ac", "041e", "03f0", "17e9", "13d3", "045e", "05a9", "0525", "0409", "0424", "040c", "0410", "10c4", "0456", "0483", "0403"]:
cameras.append(f"USB Camera: {match.group(1)} {match.group(2)} (Vendor ID: {match.group(3)}, Product ID: {match.group(4)})")
except FileNotFoundError:
if verbose:
print("lsusb command not found. Unable to scan for USB cameras.")
except Exception as e:
if verbose:
print(f"Error scanning for USB cameras: {e}")
# 2. Scan for Network Cameras using Bonjour (mDNS)
if verbose:
print("Scanning for network cameras using Bonjour (mDNS)...")
try:
bonjour_services = subprocess.check_output(['avahi-browse', '-a']).decode('utf-8')
bonjour_camera_regex = re.compile(r' _rtsp._tcp local (.*) (.*) (.*) (.*)')
bonjour_camera_regex2 = re.compile(r' _onvif._tcp local (.*) (.*) (.*) (.*)')
for line in bonjour_services.splitlines():
match = bonjour_camera_regex.match(line)
if match:
cameras.append(f"Bonjour Camera: {match.group(1)}: {match.group(2)} (Type: RTSP, Address: {match.group(3)}, Port: {match.group(4)})")
else:
match = bonjour_camera_regex2.match(line)
if match:
cameras.append(f"Bonjour Camera: {match.group(1)}: {match.group(2)} (Type: ONVIF, Address: {match.group(3)}, Port: {match.group(4)})")
except FileNotFoundError:
if verbose:
print("avahi-browse command not found. Unable to scan for network cameras using Bonjour.")
except Exception as e:
if verbose:
print(f"Error scanning for Bonjour cameras: {e}")
# 3. Scan for Network Cameras using ARP
if verbose:
print("Scanning for network cameras using ARP...")
try:
arp_table = subprocess.check_output(['arp', '-a']).decode('utf-8')
arp_camera_regex = re.compile(r'.*\((.*)\)')
for line in arp_table.splitlines():
match = arp_camera_regex.search(line)
if match:
ip_address = match.group(1)
# Check if the IP address is in the range of common network cameras
if ip_address.startswith('192.168') or ip_address.startswith('10.') or ip_address.startswith('172.16'):
cameras.append(f"ARP Camera: {ip_address}")
except FileNotFoundError:
if verbose:
print("arp command not found. Unable to scan for network cameras using ARP.")
except Exception as e:
if verbose:
print(f"Error scanning for ARP cameras: {e}")
# 4. Scan for Network Cameras using Network IP Range Scanning
if verbose:
print("Scanning for network cameras using network IP range scanning...")
try:
nm = nmap.PortScanner
ip_ranges = ["192.168.0.0/24", "10.0.0.0/24", "172.16.0.0/12"]
for ip_range in ip_ranges:
nm = nmap.PortScanner()
nm.scan(hosts=ip_range, arguments='-sT -p 80,8080,554,555,8000,8001,8008,81,8090,8091 -T4')
for ip_address in nm.all_hosts():
if nm[ip_address]['status']['state'] == 'up':
for port in nm[ip_address]['tcp'].keys():
if port in [80, 8080, 554, 555, 8000, 8001, 8008, 81, 8090, 8091]:
cameras.append(f"Network Camera: {ip_address}: {port}")
except FileNotFoundError:
if verbose:
print("nmap or bonjour command not found. Unable to scan for network cameras using network discovery protocols.")
except Exception as e:
if verbose:
print(f"Error scanning for network cameras using IP range scanning: {e}")
# 5. Scan for Network Cameras using Port Scanning
if verbose:
print("Scanning for network cameras using port scanning...")
try:
nm = nmap.PortScanner
camera_ports = [80, 8080, 554, 555, 8000, 8001, 8008, 81, 8090, 8091, 8098, 8099, 8081, 8089, 9000, 9001, 9090, 8023, 8024, 9091]
interfaces = socket.getaddrinfo(socket.gethostname(), None)
for interface in interfaces:
if interface == socket.AF_INET:
network = interface
network_regex = re.compile(r'\d+\.\d+\.\d+\.')
network_ip = network_regex.search(network).group(0)
nm.scan(hosts=network_ip + "0/24", arguments='-sT -p ' + ",".join(str(port) for port in camera_ports) + ' -T4')
for ip_address in nm.all_hosts():
if nm[ip_address]['status']['state'] == 'up':
for port in nm[ip_address]['tcp'].keys():
if port in camera_ports:
cameras.append(f"Network Camera: {ip_address}: {port}")
except FileNotFoundError:
if verbose:
print("nmap command not found. Unable to scan for network cameras using port scanning.")
except Exception as e:
if verbose:
print(f"Error scanning for network cameras using port scanning: {e}")
# 6. Scan for Network Cameras using Known Camera URLs
if verbose:
print("Scanning for network cameras using known camera URLs...")
try:
camera_urls = [
"http://{ip_address}/",
"http://{ip_address}/axis-cgi/admin/index.cgi",
"http://{ip_address}/admin/",
"http://{ip_address}/view/index.shtml",
"http://{ip_address}/cgi-bin/viewer/video.jpg",
"http://{ip_address}/cgi-bin/snapshot.cgi",
"http://{ip_address}/mjpg/video.mjpg",
"http://{ip_address}/videostream.cgi",
"http://{ip_address}/onvif/device.xml",
"rtsp://{ip_address}/live",
"rtsp://{ip_address}/h264",
"rtsp://{ip_address}/media",
"rtsp://{ip_address}/stream1",
"rtsp://{ip_address}/stream2",
"rtsp://{ip_address}/stream3"
]
interfaces = socket.getaddrinfo(socket.gethostname(), None)
for interface in interfaces:
if interface == socket.AF_INET:
network = interface
network_regex = re.compile(r'\d+\.\d+\.\d+\.')
network_ip = network_regex.search(network).group(0)
for camera_url in camera_urls:
try:
url = camera_url.format(ip_address=network_ip + "1")
response = subprocess.check_output(['curl', '-s', '-m', '5', url]).decode('utf-8')
if "Axis" in response or "Foscam" in response or "Dahua" in response or "Hikvision" in response or "Amcrest" in response or "Reolink" in response or "IP Camera" in response or "onvif" in response or "rtsp" in response:
cameras.append(f"Network Camera: {network_ip + '1'} (URL: {url})")
except subprocess.CalledProcessError:
pass
except FileNotFoundError:
if verbose:
print("curl command not found. Unable to scan for network cameras using known camera URLs.")
except Exception as e:
if verbose:
print(f"Error scanning for network cameras using known camera URLs: {e}")
# 7. Scan for Network Cameras using Network Discovery Protocols
if verbose:
print("Scanning for network cameras using network discovery protocols...")
try:
nm = nmap.PortScanner
camera_ports = [80, 8080, 554, 555, 8000, 8001, 8008, 81, 8090, 8091]
interfaces = socket.getaddrinfo(socket.gethostname(), None)
for interface in interfaces:
if interface == socket.AF_INET:
network = interface
network_regex = re.compile(r'\d+\.\d+\.\d+\.')
network_ip = network_regex.search(network).group(0)
nm.scan(hosts=network_ip + "0/24", arguments='-sT -p ' + ",".join(str(port) for port in camera_ports) + ' -T4')
# Check for ONVIF Discovery
try:
service_info = bonjour.DNSService
regtype='_onvif._tcp'
domain='local.';
port=0;
name='';
type=bonjour.kDNSServiceType_SRV;
interfaceIndex=interface;
flags=0;
serviceName=None;
serviceType=None;
servicePort=None;
serviceInstance=None;
serviceDomain=None;
serviceText=None;
serviceTTL=None;
serviceProperties=None
service_info.start()
while True:
# Receive ONVIF discovery response
(_, flags, hostname, service, port, text) = service_info.get()
if network_ip == hostname:
cameras.append(f"Network Camera: {network_ip} (Protocol: ONVIF)")
break
except Exception as e:
if verbose:
print(f"Error scanning for ONVIF cameras: {e}")
# Check for RTSP Discovery
try:
service_info = bonjour.DNSService
regtype='_rtsp._tcp'
domain='local.';
port=0;
name='';
type=bonjour.kDNSServiceType_SRV;
interfaceIndex=interface;
flags=0;
serviceName=None;
serviceType=None;
servicePort=None;
serviceInstance=None;
serviceDomain=None;
serviceText=None;
serviceTTL=None;
serviceProperties=None
service_info.start()
while True:
# Receive RTSP discovery response
(_, flags, hostname, service, port, text) = service_info.get()
if network_ip == hostname:
cameras.append(f"Network Camera: {network_ip} (Protocol: RTSP)")
break
except Exception as e:
if verbose:
print(f"Error scanning for RTSP cameras: {e}")
except FileNotFoundError:
if verbose:
print("nmap or bonjour command not found. Unable to scan for network cameras using network discovery protocols.")
except Exception as e:
if verbose:
print(f"Error scanning for network cameras using network discovery protocols: {e}")
# 8. Scan for Cloud Cameras
if verbose:
print("Scanning for cloud cameras...")
try:
cloud_camera_platforms = [
"https://www.example.com", # Replace with actual cloud camera platform URLs
"https://www.anotherplatform.com",
"https://www.yetanotherplatform.com"
]
for platform in cloud_camera_platforms:
response = requests.get(platform)
soup = BeautifulSoup(response.content, 'html.parser')
camera_elements = soup.find_all('div', class_='camera') # Adjust selector as needed
for camera_element in camera_elements:
camera_name = camera_element.find('h2').text # Adjust selectors as needed
camera_url = camera_element.find('a', href=True)['href'] # Adjust selectors as needed
cameras.append(f"Cloud Camera: {camera_name} (URL: {camera_url})")
except Exception as e:
if verbose:
print(f"Error scanning for cloud cameras: {e}")
if verbose:
print("Scanning completed. Found Cameras:")
for camera in cameras:
print(camera)
return cameras
# Example usage:
scan_for_cameras(verbose=True)