./tutorials/locdbtut.html

0.
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.
295.
296.
297.
298.
299.
300.
301.
302.
303.
304.
305.
306.
307.
308.
309.
310.
311.
312.
313.
314.
315.
316.
317.
318.
319.
320.
321.
322.
323.
324.
325.
326.
327.
328.
329.
330.
331.
332.
333.
334.
335.
336.
337.
338.
339.
340.
341.
342.
343.
344.
345.
346.
347.
348.
349.
350.
351.
352.
353.
354.
355.
356.
357.
358.
359.
360.
361.
362.
363.
364.
365.
366.
367.
368.
369.
370.
371.
372.
373.
374.
375.
376.
377.
<html>
<body>
<html>
 
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>How to build a local exploit database.</title>
</head>
 
<body>
<pre><font size="3"><h3>How to build a local exploit database.</h3>
<h4>by d3hydr8 &gt; www.darkc0de.com</h4>
Date: 03/07/08
 
In this tutorial I will show you several tools to create 
your own local exploit database and how to search through
it. The 2 sites we will be using to gather exploits are
milw0rm and packetstorm. Well, first lets discuss why you 
would need a local exploit database. Any answers? Here's mine,
because I can...
 
There are 3 scripts we will be using to create our database.  
The first thing you need to deside is where you want your 
exploits stored. Each script has a HOME_DIR option that needs
to be filled out according to your location (unless you like 
/home/d3hydr8).
 
The first script will extract all exploits from packetstorm 
from 2000-previous year. First, it will do a time check to 
make sure the last year in the YEARS list is the previous year.
 
Years list:
YEARS = ["00","01","02","03","04","05","06","07"]
 
This is the first time I have used raw_input in one of my 
scripts. It will show you the last year in the list and 
the current year. Then ask you if this is correct. Like 
shown below:
 
<pre><font size="2"><span style="background-color: #CCC">
d3hydr8@linuxbox:~> python packext.py
 
[+] Checking Years
 
Last year: 07 Current Year: 08
 
Is this correct? Yes or No
</span></font></pre>
 
Here is the c0de and a link:
 
<a href="/websites/darkc0de.com/misc/packext.py">http://www.darkc0de.com/misc/packext.py</a>
 
<pre><font size="2"><span style="background-color: #CCC">
#!/usr/bin/python
#This script will extract all exploits
#from packetstorm from 2000-previous year.
 
#http://darkc0de.com
#d3hydr8[at]gmail[dot]com
 
import urllib, tarfile, os, sys, time
 
HOME_DIR = "/home/d3hydr8/"
YEARS = ["00","01","02","03","04","05","06","07"]
 
#Time check
now = time.strftime("%Y", time.localtime())[2:]
print "\n[+] Checking Years\n"
if now == YEARS[-1]:
	print "[!] The last year in YEARS cannot be the present year.\n"
	sys.exit(1)
else:
	print "Last year:",YEARS[-1],"Current Year:",now
	resp = raw_input("\nIs this correct? Yes or No ").lower()
	if resp == "no":
		print "\n[!] Modify YEARS list correctly.\n"
		sys.exit(1)
	elif resp == "yes":
		print "\n[+] Starting extraction"
	else:
		print "\nHUH?\n"
		sys.exit(1)
 
#Make sure HOME_DIR ends with a "/"
if HOME_DIR[-1] != "/":
	HOME_DIR = HOME_DIR+"/"
print "[+] Dir:",HOME_DIR,"\n"
 
#Extraction process	
for year in YEARS:
	print "Start: 20"+year
	try:
		os.chdir(HOME_DIR+year+"-exploits")
	except(OSError):
		os.mkdir(HOME_DIR+year+"-exploits")
		os.chdir(HOME_DIR+year+"-exploits")
	page = "http://packetstormsecurity.org/"+year+"12-exploits/20"+year+"-exploits.tgz"
	urllib.urlretrieve(page, "20"+year+"-exploits.tgz")
 
	tar = tarfile.open("20"+year+"-exploits.tgz")
	tar.extractall()
	tar.close()
 
	os.remove("20"+year+"-exploits.tgz")
	print "Done: 20"+year
 
print "\n[++] Operation Complete\n"
</span></font></pre>
 
Here is what you should see when your done:
 
<pre><font size="2"><span style="background-color: #CCC">
d3hydr8@linuxbox:~> python packext.py
 
[+] Checking Years
 
Last year: 07 Current Year: 08
 
Is this correct? Yes or No Yes
 
[+] Starting extraction
[+] Dir: /home/d3hydr8/exploits/
 
Start: 2000
Done: 2000
Start: 2001
Done: 2001
Start: 2002
Done: 2002
Start: 2003
Done: 2003
Start: 2004
Done: 2004
Start: 2005
Done: 2005
Start: 2006
Done: 2006
Start: 2007
Done: 2007
 
[++] Operation Complete
</span></font></pre>
 
<img src="./imgs/pic1.png">
 
Now that we have all of the exploits from packetstorm through the
previous year. Lets use milw0rm to get an archive of remote ports 
and platforms.
 
<a href="/websites/darkc0de.com/misc/milarchive.py">http://www.darkc0de.com/misc/milarchive.py</a>
 
<pre><font size="2"><span style="background-color: #CCC">
#!/usr/bin/python
#Extracts exploit archive for remote 
#ports and platforms from milw0rm.
 
import urllib, tarfile, os
 
HOME_DIR = "/home/d3hydr8/"
 
#Make sure HOME_DIR ends with a "/"
if HOME_DIR[-1] != "/":
	HOME_DIR = HOME_DIR+"/"
print "\n[+] Dir:",HOME_DIR,"\n"
 
try:
	os.chdir(HOME_DIR)
except(OSError):
	os.mkdir(HOME_DIR)
	os.chdir(HOME_DIR)
print "[!] Downloading file..."
page = "http://www.milw0rm.com/sploits/milw0rm.tar.bz2"
urllib.urlretrieve(page, "milw0rm.tar.bz2")
print "[!] Extracting files..."
tar = tarfile.open("milw0rm.tar.bz2")
tar.extractall()
tar.close()
os.remove("milw0rm.tar.bz2")
print "\n[!] Operation Complete\n"
 
#Or
#wget http://www.milw0rm.com/sploits/milw0rm.tar.bz2
#tar -xjvf milw0rm.tar.bz2
#rm milw0rm.tar.bz2
#:)
</span></font></pre>
 
Output:
 
What this does is create a milw0rm folder with the 
directories platforms and rports.
 
d3hydr8@linuxbox:~> python milarchive.py
 
[+] Dir: /home/d3hydr8/exploits/
 
[!] Downloading file...
[!] Extracting files...
 
[!] Operation Complete
 
<img src="./imgs/pic2.png">
 
So, now we have packetstorm, remote port and platforms
but what about webapp exploits? The script below will 
help you collect all the webapp exploits from milw0rm.
It will go page by page collecting 30 exploits at a
time and write them to your HOME_DIR+/milw0rm/. The script will 
finish when it gets a xplt list length of zero 3x.
 
Becareful with this tool, set your times accordingly. Milw0rm
does have a DDOS protection with automatic ip blocking.
 
<a href="/websites/darkc0de.com/misc/milwebappext.py">http://www.darkc0de.com/misc/milwebappext.py</a>
 
<pre><font size="2"><span style="background-color: #CCC">
#!/usr/bin/python
#Attempts to collect all webapp
#exploits from milw0rms DB.
 
import urllib2, time, os, re, urllib
 
HOME_DIR = "/home/d3hydr8/exploits"
site = "http://www.milw0rm.com/webapps.php?start="
#Time to wait between page loads (in secs)
TIME = "3"
 
#Make sure HOME_DIR ends with a "/"
if HOME_DIR[-1] != "/":
	HOME_DIR = HOME_DIR+"/"
print "\n[+] Dir:",HOME_DIR,"\n"
HOME_DIR = HOME_DIR+"milw0rm/"
 
try:
	os.chdir(HOME_DIR)
except(OSError):
	os.mkdir(HOME_DIR)
	os.chdir(HOME_DIR)
 
start = 0
error = 0
while error != 3:
	try:
		time.sleep(int(TIME))
		print "[+] Page:",start
		source = urllib2.urlopen(site+str(start), "80").read()
		xplts = re.findall("href=\"/exploits/\d+",source)
		print "[+] Exploits Found:",len(xplts)
	except(urllib2.URLError):
		xplt = []
		pass
	for xplt in xplts:
		time.sleep(0.5)
		xplt = xplt.replace("href=\"","")
		urllib.urlretrieve("http://www.milw0rm.com"+xplt, HOME_DIR+xplt.rsplit("/",1)[1])
	if len(xplt) == 0:
		error +=1
	start +=30
print "\n[!] Operation Complete\n"
</span></font></pre>
 
Looking more closely at that c0de I'm guessing
you might be able to just change some site info
like exploits >> papers and collect all papers 
(or whatever else).
 
This next script is just a utility for searching 
for exploits from the command line from milw0rm.
 
<a href="/websites/darkc0de.com/misc/milsearch.py">http://www.darkc0de.com/misc/milsearch.py</a>
 
<pre><font size="2"><span style="background-color: #CCC">
#!/usr/bin/python
#Milw0rm exploits search tool.
 
import urllib2, re, sys
 
if len(sys.argv) != 2:
	print "\nUsage: ./milsearch.py [search]"
	print "Ex: ./milsearch.py phpmyadmin\n"
	sys.exit(1)
 
site = "http://www.milw0rm.com/search.php?dong="
 
try:
	source = urllib2.urlopen(site+sys.argv[1], "80").read()
	xplts = re.findall("href=\"/exploits/\d+",source)
	print "\n[+] Results Found:",len(xplts),"\n"
except(urllib2.URLError):
	xplts = []
	pass
if len(xplts) >=1:
	for xplt in xplts:
		xplt = xplt.replace("href=\"","")
		print "http://milw0rm.com"+xplt
else:
	print "\nNo Results Found\n"
</span></font></pre>
 
Output:
 
<img src="./imgs/pic3.png">
 
Now your thinking, "Ok, I have a database of exploits
but how would I keep it up to date?". The script 
below will help with this task. It will check milw0rm
for new exploits and download them to your HOME_DIR
if found. The default time is set to check every 5 
minutes.
 
<a href="/websites/darkc0de.com/misc/milupdate.py">http://www.darkc0de.com/misc/milupdate.py</a>
 
<pre><font size="2"><span style="background-color: #CCC">
#!/usr/bin/python
#Checks milw0rm.com for exploit updates.
 
import urllib2, time, os, re, urllib
 
HOME_DIR = "/home/d3hydr8/exploits"
site = "http://www.milw0rm.com/"
#Time to wait for update checks (in secs)
TIME = "300"
 
#Make sure HOME_DIR ends with a "/"
if HOME_DIR[-1] != "/":
	HOME_DIR = HOME_DIR+"/"
print "\n[+] Dir:",HOME_DIR,"\n"
HOME_DIR = HOME_DIR+"milw0rm/"
 
try:
	os.chdir(HOME_DIR)
except(OSError):
	os.mkdir(HOME_DIR)
	os.chdir(HOME_DIR)
 
done = []
while 1:
	time.sleep(int(TIME))
	try:
		source = urllib2.urlopen(site, "80").readlines()
		for line in source:
			if re.search("class=\"style15", line):
				xplts = re.findall("href=\"/exploits/\d+", line)
	except(urllib2.URLError):
		xplts = []
		pass
	if len(xplts) >=1:
		for xplt in xplts:
			if xplt not in done:
				done.append(xplt)
				xplt = xplt.replace("href=\"","")
				print "[+] Adding: http://www.milw0rm.com"+xplt
				urllib.urlretrieve("http://www.milw0rm.com"+xplt, HOME_DIR+xplt.rsplit("/",1)[1])
		print "[+] Collected:",len(done),"exploits\n"
</span></font></pre>
 
If you haven't found an online module to search 
through your exploits and bought the domain "exploits4fun.com"
there are a couple ways to search them. The first is use your 
GUI search utilty that comes with your os. If using KDE's 
Find Files/Folders just change your Name/Location to your 
HOME_DIR and use the Contents tab to do the rest.
 
<img src="./imgs/pic4.png">
 
Or just use your find cmd. I hope this tutorial and tools
were fun. I realize this stuff is pretty much useless when 
you could just visit the sites but I had fun writing it. And 
remember,  because I can...
 
<a href="http://digg.com/programming/How_to_build_a_local_exploit_database"><img src="http://digg.com/img/badges/91x17-digg-button.gif" width="91" height="17" alt="Digg!"/></a>
</font></pre>
</body>
</html>
 
 

Back