forked from szabo-tibor/cam-scan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CamScan.py
242 lines (157 loc) · 6.12 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
import requests
from shodan import Shodan
import os
import threading
from urllib.parse import urlparse
import webbrowser
import csv
class CamScan:
def __init__(self, dirname='Images', search=None,
path=None, timeout=4, pages=0, verbose=False):
self.search = search
self.path = path
self.dirname = dirname
self.timeout = timeout
self.pages = pages
self.verbose = verbose
self.api = None
try:
keyfile = open('shodan_api_key','r')
key = keyfile.readline()
keyfile.close()
self.api = Shodan(key)
except FileNotFoundError:
print('Key file not found')
DIR_NUMBER = 2
while os.path.exists(self.dirname):
self.dirname = self.dirname.strip('0987654321') + str(DIR_NUMBER)
DIR_NUMBER += 1
def initShodan(self, key):
with open('shodan_api_key','w') as file:
file.write(key)
self.api = Shodan(key)
def chooseFromCSV(self, file):
if os.path.exists(file):
f = open(file, newline='')
data = csv.DictReader(f)
searches = {}
for x in data:
searches[x['searchQuery']] = x['imagePath']
f.close()
print('CSV file input. Select search from below:\n')
y = 0
for search in searches:
print(str(y) + ') ' + search)
y += 1
choice = int(input('\nChoose search: '))
self.search = list(searches.keys())[choice]
self.path = list(searches.values())[choice]
else:
raise FileNotFoundError
def pagesCount(self):
hosts = self.api.count(self.search)['total']
return int(hosts / 100) + 1
def setPages(self, pages):
if type(pages) in [int, range, type(None)]:
self.pages = pages
else:
raise Exception('Wrong type. pages value can be set to int, range, or None')
def requestAndDownload(self, url):
try:
r = requests.get(url, timeout=self.timeout)
if r.status_code == 200:
if self.verbose:
print(url, ' - Success')
filename = urlparse(url).netloc.replace(':','-') + '.png'
with open(filename, 'wb') as img:
img.write(r.content)
else:
if self.verbose:
print(url, r.status_code, 'Error')
except requests.exceptions.ReadTimeout:
if self.verbose:
print(url, '- Timed out')
except Exception as e:
#print(e)
if self.verbose:
print(url, '- Connection Error')
def runOnPage(self, pagenumber):
results = None
tries = 0
while results is None and tries < 10:
try:
results = self.api.search(self.search, page=pagenumber)
except Exception as e:
tries += 1
print('Shodan error')
if tries == 10:
print('Giving up')
raise Exception(e.args[0])
threads = []
for result in results['matches']:
url = 'http://' + str(result['ip_str']) + ':' + str(result['port']) + self.path
x = threading.Thread(target=self.requestAndDownload, args=(url,))
threads.append(x)
x.start()
for thread in threads:
thread.join()
def run(self):
if self.api == None:
raise Exception('No Shodan key')
os.mkdir(self.dirname)
os.chdir(self.dirname)
print('Saving images to', os.getcwd(), '\n')
if self.pages is None:
print('Running every page')
for page in range(self.pagesCount()):
print('Starting page:', page)
self.runOnPage(page)
elif type(self.pages) is int:
print('Running page', self.pages)
self.runOnPage(self.pages)
elif type(self.pages) is range:
for page in self.pages:
print('Starting page:', page)
self.runOnPage(page)
def generatePage(self):
html = '''
<!DOCTYPE html>
<html>
<head>
<title>Saved Images</title>
<script>
let emptyImages = [];
function removeEmpty() {
let images = document.getElementsByTagName('img');
for (let i = 0; i < images.length; i++) {
if (images[i].naturalHeight == 0) {
emptyImages.push(images[i]);
}
}
for (let i = 0; i < emptyImages.length; i++) {
emptyImages[i].remove();
}
}
</script>
</head>
<body style="background-color:black">
<button onclick="removeEmpty()">Remove Empty Images</button>
<p style="color:white;">Click on an image to open stream</p>
'''
with open('images.html', 'w') as page:
page.write(html)
for name_of_file in os.listdir():
if '.png' in name_of_file:
link = 'http://' + name_of_file.replace('-', ':').strip('.png')
page.write('\n\t<a href="{}" target="_blank">'.format(link))
page.write('\n\t\t<img src="{}" height="480" width="720">'.format(name_of_file))
page.write('\n\t</a>')
page.write('\n</body>\n</html>')
def showImages(self):
webbrowser.open('images.html')
def info(self):
print('search:', self.search)
print('path:', self.path)
print('dirname', self.dirname)
print('timeout:', self.timeout)
print('pages:', self.pages)