9cdb35554ba75e1d5dc9d88ece3d7487ec8e44cd
Hanno Böck make code compatible with p...

Hanno Böck authored 10 years ago

1) #!/usr/bin/python -tO
Hanno Böck initial commit

Hanno Böck authored 17 years ago

2) 
3) # freewvs 0.1 - the free web vulnerability scanner
4) #
5) # http://source.schokokeks.org/freewvs/
6) #
Hanno Böck License change to cc0

Hanno Böck authored 12 years ago

7) # Written 2007-2012 by schokokeks.org Hosting, http://www.schokokeks.org
Hanno Böck initial commit

Hanno Böck authored 17 years ago

8) #
9) # Contributions by
Hanno Böck License change to cc0

Hanno Böck authored 12 years ago

10) # Hanno Boeck, http://hboeck.de/
11) # Fabian Fingerle, http://www.fabian-fingerle.de/
12) # Bernd Wurst, http://bwurst.org/
Hanno Böck initial commit

Hanno Böck authored 17 years ago

13) #
Hanno Böck License change to cc0

Hanno Böck authored 12 years ago

14) # To the extent possible under law, the author(s) have dedicated all copyright
15) # and related and neighboring rights to this software to the public domain
16) # worldwide. This software is distributed without any warranty.
Hanno Böck initial commit

Hanno Böck authored 17 years ago

17) #
Hanno Böck License change to cc0

Hanno Böck authored 12 years ago

18) # You should have received a copy of the CC0 Public Domain Dedication along
19) # with this software. If not, see 
20) # http://creativecommons.org/publicdomain/zero/1.0/
21) # Nevertheless, in case you use a significant part of this code, we ask (but
22) # not require, see the license) that you keep the authors' names in place and
23) # return your changes to the public. We would be especially happy if you tell
24) # us what you're going to do with this code.
Hanno Böck initial commit

Hanno Böck authored 17 years ago

25) 
Hanno Böck make code compatible with p...

Hanno Böck authored 10 years ago

26) try: # python3
27) 	import configparser
28) except ImportError: # python2
29) 	import ConfigParser as configparser
Bernd Wurst support python-2.6 again, w...

Bernd Wurst authored 10 years ago

30) 	# overwrite default open() function
31) 	# this one supports encoding='...'
32) 	from codecs import open
Hanno Böck make code compatible with p...

Hanno Böck authored 10 years ago

33) 
34) import os, glob, pprint, re, optparse, sys, gettext
Bernd Wurst Add XML string escaping

Bernd Wurst authored 15 years ago

35) from xml.sax.saxutils import escape
Hanno Böck initial commit

Hanno Böck authored 17 years ago

36) 
Hanno Böck add fancy output

Hanno Böck authored 16 years ago

37) gettext.textdomain('freewvs')
38) _ = gettext.gettext
Hanno Böck initial commit

Hanno Böck authored 17 years ago

39) 
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

40) def versioncompare(safe_version, find_version):
41)     if safe_version == [""]:
42)         return True
43)     for i in range(min(len(find_version), len(safe_version))):
44)         if int(find_version[i])<int(safe_version[i]):
45)             return True
46)         if int(find_version[i])>int(safe_version[i]):
47)             return False
48)     return (len(find_version)<len(safe_version))
Hanno Böck initial commit

Hanno Böck authored 17 years ago

49) 
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

50) def vulnprint(appname, version, safeversion, vuln, vfilename, subdir, style = None):
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

51)     appdir = '/'.join(os.path.abspath(vfilename).split('/')[:-1-subdir])
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

52)     if not style:
Hanno Böck replace print with function...

Hanno Böck authored 13 years ago

53)         print ("%(appname)s %(version)s (%(safeversion)s) %(vuln)s %(appdir)s" \
54)               % vars())
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

55)     elif style=='fancy':
Hanno Böck more print to function conv...

Hanno Böck authored 13 years ago

56)         print (_("Directory: %(appdir)s") % vars())
Hanno Böck indentation fix

Hanno Böck authored 16 years ago

57)         if safeversion!="ok":
Hanno Böck add support for unfixed apps

Hanno Böck authored 16 years ago

58)             if safeversion!="":
Hanno Böck more print to function conv...

Hanno Böck authored 13 years ago

59)                 print (_("Vulnerable %(appname)s %(version)s found, please update to " \
60)                         "%(safeversion)s or above.") % vars())
Hanno Böck add support for unfixed apps

Hanno Böck authored 16 years ago

61)             else:
Hanno Böck more print to function conv...

Hanno Böck authored 13 years ago

62)                 print (_("Vulnerable %(appname)s %(version)s found, no fixed version available." \
63)                         ) % vars())
Hanno Böck some i18n fixes

Hanno Böck authored 16 years ago

64)             if vuln[:3] == "CVE":
Hanno Böck more print to function conv...

Hanno Böck authored 13 years ago

65)                 print (_("http://cve.mitre.org/cgi-bin/cvename.cgi?name=%(vuln)s") \
66)                         % vars())
Hanno Böck some i18n fixes

Hanno Böck authored 16 years ago

67)             else:
68)                 print (vuln)
Hanno Böck add fancy output

Hanno Böck authored 16 years ago

69)         else:
Hanno Böck more print to function conv...

Hanno Böck authored 13 years ago

70)             print (_("%(appname)s %(version)s found." ) % vars())
Hanno Böck make code compatible with p...

Hanno Böck authored 10 years ago

71)         print ("")
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

72)     elif style=='xml':
73)         state = 'vulnerable'
74)         if safeversion == 'ok':
75)             state = 'ok'
Hanno Böck more print to function conv...

Hanno Böck authored 13 years ago

76)         print ('  <app state="%s">' % state)
77)         print ('    <appname>%s</appname>' % escape(appname))
78)         print ('    <version>%s</version>' % escape(version))
79)         print ('    <directory>%s</directory>' % escape(appdir))
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

80)         if state == 'vulnerable':
Hanno Böck more print to function conv...

Hanno Böck authored 13 years ago

81)             print ('    <safeversion>%s</safeversion>' % escape(safeversion))
82)             print ('    <vulninfo>%s</vulninfo>' % escape(vuln))
83)         print ('  </app>')
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

84) 
Hanno Böck initial commit

Hanno Böck authored 17 years ago

85) 
86) pp = pprint.PrettyPrinter(indent=4)
87) 
88) # Command-line options
Bernd Wurst add correct usage notice

Bernd Wurst authored 10 years ago

89) parser = optparse.OptionParser(usage="usage: %prog [options] <path> [<path2> ...]")
Hanno Böck initial commit

Hanno Böck authored 17 years ago

90) parser.add_option("-a", "--all", action="store_true", dest="ALL",
91)                   help="Show all webapps found, not just vulnerable")
92) parser.add_option("-d", "--debug", action="store_true", dest="DEBUG",
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

93)                   help="Show lots of debugging output, mainly useful"+ \
94)                   "for development")
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

95) parser.add_option("-f", "--fancy", action="store_const", dest="OUTPUT", const="fancy",
Hanno Böck add fancy output

Hanno Böck authored 16 years ago

96)                   help="Show more fancy output")
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

97) parser.add_option("-x", "--xml", action="store_const", dest="OUTPUT", const="xml",
98)                   help="Output results as XML")
Hanno Böck initial commit

Hanno Böck authored 17 years ago

99) opts, args = parser.parse_args()
100) 
101) # Parse vulnerability database
Hanno Böck make code compatible with p...

Hanno Böck authored 10 years ago

102) config = configparser.ConfigParser()
Hanno Böck initial commit

Hanno Böck authored 17 years ago

103) config.read(glob.glob('/usr/share/freewvs/*.freewvs'))
104) config.read(glob.glob('/usr/local/share/freewvs/*.freewvs'))
105) config.read(glob.glob(os.path.dirname(sys.argv[0])+'/freewvsdb/*.freewvs'))
106) 
107) vdb = []
108) for sect in config.sections():
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

109)     item = {}
110) 
111)     # base options
112)     item['name'] = sect
113)     item['safe'] = config.get(sect, 'safe')
114)     item['file'] = config.get(sect, 'file')
115)     item['vuln'] = config.get(sect, 'vuln')
116)     item['subdir'] = int(config.get(sect, 'subdir'))
117) 
118)     # match magic
119)     item['variable'] = []
120)     for var in config.get(sect,'variable').split(","):
121)         item['variable'].append(re.compile(re.escape(var)+
Bernd Wurst Version-Pattern darf nicht...

Bernd Wurst authored 13 years ago

122)                                 r"[^0-9.\n\r]*[.]*([0-9.]*[0-9])[^0-9.]"))
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

123) 
124)     # optional options
125)     if config.has_option(sect,'extra_match'):
126)         item['extra_match'] = config.get(sect,'extra_match')
127)     else:
128)         item['extra_match'] = False
129)     if config.has_option(sect,'add_minor'):
130)         item['add_minor'] = config.get(sect,'add_minor')
131)     else:
132)         item['add_minor'] = False
133)     if config.has_option(sect,'old_safe'):
134)         item['old_safe'] = config.get(sect,'old_safe').split(",")
135)     else:
136)         item['old_safe'] = []
137) 
138)     vdb.append(item)
139) if opts.DEBUG:
140)     pp.pprint(vdb)
Hanno Böck initial commit

Hanno Böck authored 17 years ago

141) 
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

142) if opts.OUTPUT == 'xml':
Hanno Böck more print to function conv...

Hanno Böck authored 13 years ago

143)   print ('<?xml version="1.0" ?>')
144)   print ('<freewvs>')
Hanno Böck initial commit

Hanno Böck authored 17 years ago

145) 
146) # start the search
147) 
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

148) for fdir in args:
149)     for root, NULL, files in os.walk(fdir):
150)         for filename in files:
151)             for item in vdb:
152)                 if filename == item['file']:
153)                     mfile = os.path.join(root, filename)
Bernd Wurst error handling when opening...

Bernd Wurst authored 12 years ago

154)                     try:
Hanno Böck don't enforce encoding on f...

Hanno Böck authored 7 years ago

155)                       file = open(mfile)
Bernd Wurst error handling when opening...

Bernd Wurst authored 12 years ago

156)                     except:
157)                       continue
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

158)                     filestr = file.read()
159)                     file.close()
160) 
161)                     if item['extra_match']:
162)                         ematch = (filestr.find(item['extra_match']) != -1)
163)                     else:
164)                         ematch = True
165) 
166)                     findversion = []
167)                     for var in item['variable']:
168)                         var = var.search(filestr)
169)                         if not var:
170)                             findversion = False
171)                             break
172)                         else:
173)                             findversion.append(var.group(1))
174) 
175)                     if findversion and ematch:
176)                         findversion = '.'.join(findversion)
177) 
178)                         # Very ugly phpbb workaround
179)                         if item['add_minor']:
180)                             findversion = findversion.split('.')
181)                             findversion[-1] = str(int(findversion[-1])+
182)                                             int(item['add_minor']))
183)                             findversion = '.'.join(findversion)
184) 
185)                         if not (versioncompare(item['safe'].split('.'), \
186)                                 findversion.split('.'))) or \
187)                                 item['old_safe'].count(findversion)>0:
188)                             if opts.ALL:
189)                                 if opts.DEBUG:
Hanno Böck replace print with function...

Hanno Böck authored 13 years ago

190)                                     print ("File "+mfile)
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

191)                                 vulnprint(item['name'], findversion, \
Hanno Böck add fancy output

Hanno Böck authored 16 years ago

192)                                           "ok", "", mfile, item['subdir'], \
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

193)                                           opts.OUTPUT)
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

194)                         else:
195)                             if opts.DEBUG:
Hanno Böck replace print with function...

Hanno Böck authored 13 years ago

196)                                 print ("File "+mfile)
Hanno Böck better code for old versions 6

Hanno Böck authored 16 years ago

197)                             safev="9999"
Hanno Böck print more intelligent warn...

Hanno Böck authored 16 years ago

198)                             for ver in item['old_safe']:
199)                                 if (versioncompare(ver.split('.'), \
Hanno Böck better code for old versions 3

Hanno Böck authored 16 years ago

200)                                     findversion.split('.') ) and \
Hanno Böck better code for old versions 6

Hanno Böck authored 16 years ago

201)                                     not versioncompare(ver.split('.'), \
Hanno Böck better code for old versions 3

Hanno Böck authored 16 years ago

202)                                     safev.split('.')) ):
Hanno Böck print more intelligent warn...

Hanno Böck authored 16 years ago

203)                                     safev=ver
Hanno Böck really fix safeversions

Hanno Böck authored 16 years ago

204)                             if safev=="9999":
Hanno Böck print more intelligent warn...

Hanno Böck authored 16 years ago

205)                                 safev=item['safe']
206) 
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

207)                             vulnprint (item['name'], findversion, \
Hanno Böck print more intelligent warn...

Hanno Böck authored 16 years ago

208)                                        safev, item['vuln'], \
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

209)                                        mfile, item['subdir'], opts.OUTPUT)
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

210) 
211)                     else:
212)                         if opts.DEBUG:
Hanno Böck replace print with function...

Hanno Böck authored 13 years ago

213)                             print ("regexp failed for " + \
214)                                   item['name'] + " on " + mfile)
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

215) 
216) if opts.OUTPUT == 'xml':