3db242875a277477523e8fad68ee845119242f5e
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 16 years ago

2) 
3) # freewvs 0.1 - the free web vulnerability scanner
4) #
Hanno Böck convert all http URLs to https

Hanno Böck authored 6 years ago

5) # https://source.schokokeks.org/freewvs/
Hanno Böck initial commit

Hanno Böck authored 16 years ago

6) #
Hanno Böck convert all http URLs to https

Hanno Böck authored 6 years ago

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

Hanno Böck authored 16 years ago

8) #
9) # Contributions by
Hanno Böck convert all http URLs to https

Hanno Böck authored 6 years ago

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

Hanno Böck authored 16 years ago

13) #
Hanno Böck License change to cc0

Hanno Böck authored 11 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 16 years ago

17) #
Hanno Böck License change to cc0

Hanno Böck authored 11 years ago

18) # You should have received a copy of the CC0 Public Domain Dedication along
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

19) # with this software. If not, see
Hanno Böck convert all http URLs to https

Hanno Böck authored 6 years ago

20) # https://creativecommons.org/publicdomain/zero/1.0/
Hanno Böck License change to cc0

Hanno Böck authored 11 years ago

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 16 years ago

25) 
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

26) try:					# python3
27)     import configparser
28) except ImportError:			# python2
29)     import ConfigParser as configparser
30)     # overwrite default open() function
31)     # this one supports encoding='...'
32)     from codecs import open
33) 
34) import os
35) import glob
36) import pprint
37) import re
38) import optparse
39) import sys
40) import gettext
Bernd Wurst Add XML string escaping

Bernd Wurst authored 15 years ago

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

Hanno Böck authored 16 years ago

42) 
Hanno Böck add fancy output

Hanno Böck authored 16 years ago

43) gettext.textdomain('freewvs')
44) _ = gettext.gettext
Hanno Böck initial commit

Hanno Böck authored 16 years ago

45) 
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

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

Hanno Böck authored 16 years ago

47) def versioncompare(safe_version, find_version):
48)     if safe_version == [""]:
49)         return True
50)     for i in range(min(len(find_version), len(safe_version))):
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

51)         if int(find_version[i]) < int(safe_version[i]):
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

52)             return True
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

53)         if int(find_version[i]) > int(safe_version[i]):
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

54)             return False
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

55)     return (len(find_version) < len(safe_version))
56) 
Hanno Böck initial commit

Hanno Böck authored 16 years ago

57) 
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

58) def vulnprint(appname, version, safeversion, vuln, vfilename, subdir,
59)               style=None):
Hanno Böck pycodestyle fixes

Hanno Böck authored 6 years ago

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

Bernd Wurst authored 15 years ago

61)     if not style:
Hanno Böck missing space

Hanno Böck authored 7 years ago

62)         print("%(appname)s %(version)s (%(safeversion)s) %(vuln)s "
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

63)               "%(appdir)s" % vars())
64)     elif style == 'fancy':
65)         print(_("Directory: %(appdir)s") % vars())
66)         if safeversion != "ok":
67)             if safeversion != "":
68)                 print(_("Vulnerable %(appname)s %(version)s found, please "
69)                         "update to %(safeversion)s or above.") % vars())
Hanno Böck add support for unfixed apps

Hanno Böck authored 15 years ago

70)             else:
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

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

Hanno Böck authored 15 years ago

73)             if vuln[:3] == "CVE":
Hanno Böck convert all http URLs to https

Hanno Böck authored 6 years ago

74)                 print(_("https://cve.mitre.org/cgi-bin/cvename.cgi?name="
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

75)                         "%(vuln)s") % vars())
Hanno Böck some i18n fixes

Hanno Böck authored 15 years ago

76)             else:
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

77)                 print(vuln)
Hanno Böck add fancy output

Hanno Böck authored 16 years ago

78)         else:
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

79)             print(_("%(appname)s %(version)s found.") % vars())
80)         print("")
81)     elif style == 'xml':
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

82)         state = 'vulnerable'
83)         if safeversion == 'ok':
84)             state = 'ok'
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

85)         print('  <app state="%s">' % state)
86)         print('    <appname>%s</appname>' % escape(appname))
87)         print('    <version>%s</version>' % escape(version))
88)         print('    <directory>%s</directory>' % escape(appdir))
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

89)         if state == 'vulnerable':
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

90)             print('    <safeversion>%s</safeversion>' % escape(safeversion))
91)             print('    <vulninfo>%s</vulninfo>' % escape(vuln))
92)         print('  </app>')
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

93) 
Hanno Böck initial commit

Hanno Böck authored 16 years ago

94) 
95) pp = pprint.PrettyPrinter(indent=4)
96) 
97) # Command-line options
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

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

Hanno Böck authored 16 years ago

100) parser.add_option("-a", "--all", action="store_true", dest="ALL",
101)                   help="Show all webapps found, not just vulnerable")
102) parser.add_option("-d", "--debug", action="store_true", dest="DEBUG",
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

103)                   help="Show lots of debugging output, mainly useful"
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

104)                   "for development")
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

105) parser.add_option("-f", "--fancy", action="store_const", dest="OUTPUT",
106)                   const="fancy", help="Show more fancy output")
107) parser.add_option("-x", "--xml", action="store_const", dest="OUTPUT",
108)                   const="xml", help="Output results as XML")
Hanno Böck initial commit

Hanno Böck authored 16 years ago

109) opts, args = parser.parse_args()
110) 
111) # Parse vulnerability database
Hanno Böck make code compatible with p...

Hanno Böck authored 10 years ago

112) config = configparser.ConfigParser()
Hanno Böck catch error message on pars...

Hanno Böck authored 7 years ago

113) try:
114)     config.read(glob.glob('/usr/share/freewvs/*.freewvs'))
115)     config.read(glob.glob('/usr/local/share/freewvs/*.freewvs'))
Hanno Böck pycodestyle fixes

Hanno Böck authored 6 years ago

116)     config.read(glob.glob(os.path.dirname(sys.argv[0]) + '/freewvsdb/*.freewvs'))
Hanno Böck catch error message on pars...

Hanno Böck authored 7 years ago

117) except configparser.MissingSectionHeaderError as err:
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

118)     print("Error parsing config files: %s" % err)
Hanno Böck initial commit

Hanno Böck authored 16 years ago

119) 
120) vdb = []
121) for sect in config.sections():
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

122)     item = {}
123) 
124)     # base options
125)     item['name'] = sect
126)     item['safe'] = config.get(sect, 'safe')
127)     item['file'] = config.get(sect, 'file')
128)     item['vuln'] = config.get(sect, 'vuln')
129)     item['subdir'] = int(config.get(sect, 'subdir'))
130) 
131)     # match magic
132)     item['variable'] = []
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

133)     for var in config.get(sect, 'variable').split(","):
134)         item['variable'].append(re.compile(re.escape(var) +
Hanno Böck fix regexp somewhat, this s...

Hanno Böck authored 6 years ago

135)                                 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

136) 
137)     # optional options
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

138)     if config.has_option(sect, 'extra_match'):
139)         item['extra_match'] = config.get(sect, 'extra_match')
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

140)     else:
141)         item['extra_match'] = False
Hanno Böck a bit better detection for...

Hanno Böck authored 7 years ago

142)     if config.has_option(sect, 'extra_nomatch'):
143)         item['extra_nomatch'] = config.get(sect, 'extra_nomatch')
144)     else:
145)         item['extra_nomatch'] = False
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

146)     if config.has_option(sect, 'add_minor'):
147)         item['add_minor'] = config.get(sect, 'add_minor')
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

148)     else:
149)         item['add_minor'] = False
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

150)     if config.has_option(sect, 'old_safe'):
151)         item['old_safe'] = config.get(sect, 'old_safe').split(",")
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

152)     else:
153)         item['old_safe'] = []
154) 
155)     vdb.append(item)
156) if opts.DEBUG:
157)     pp.pprint(vdb)
Hanno Böck initial commit

Hanno Böck authored 16 years ago

158) 
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

159) if opts.OUTPUT == 'xml':
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

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

Hanno Böck authored 16 years ago

162) 
163) # start the search
164) 
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

165) for fdir in args:
166)     for root, NULL, files in os.walk(fdir):
167)         for filename in files:
168)             for item in vdb:
169)                 if filename == item['file']:
170)                     mfile = os.path.join(root, filename)
Bernd Wurst error handling when opening...

Bernd Wurst authored 11 years ago

171)                     try:
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

172)                         file = open(mfile)
Hanno Böck pycodestyle fixes

Hanno Böck authored 6 years ago

173)                     except Exception:
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

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

Hanno Böck authored 16 years ago

175)                     filestr = file.read()
176)                     file.close()
177) 
178)                     if item['extra_match']:
179)                         ematch = (filestr.find(item['extra_match']) != -1)
Hanno Böck a bit better detection for...

Hanno Böck authored 7 years ago

180)                     elif item['extra_nomatch']:
181)                         ematch = not (filestr.find(item['extra_nomatch']) != -1)
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

182)                     else:
183)                         ematch = True
184) 
185)                     findversion = []
186)                     for var in item['variable']:
187)                         var = var.search(filestr)
188)                         if not var:
189)                             findversion = False
190)                             break
191)                         else:
192)                             findversion.append(var.group(1))
193) 
194)                     if findversion and ematch:
195)                         findversion = '.'.join(findversion)
196) 
197)                         # Very ugly phpbb workaround
198)                         if item['add_minor']:
199)                             findversion = findversion.split('.')
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

200)                             findversion[-1] = str(int(findversion[-1]) +
201)                                                   int(item['add_minor']))
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

202)                             findversion = '.'.join(findversion)
203) 
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

204)                         if not (versioncompare(item['safe'].split('.'),
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

205)                                 findversion.split('.'))) or \
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

206)                                 item['old_safe'].count(findversion) > 0:
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

207)                             if opts.ALL:
208)                                 if opts.DEBUG:
Hanno Böck pycodestyle fixes

Hanno Böck authored 6 years ago

209)                                     print("File " + mfile)
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

210)                                 vulnprint(item['name'], findversion,
211)                                           "ok", "", mfile, item['subdir'],
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

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

Hanno Böck authored 16 years ago

213)                         else:
214)                             if opts.DEBUG:
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

215)                                 print("File " + mfile)
216)                             safev = "9999"
Hanno Böck print more intelligent warn...

Hanno Böck authored 15 years ago

217)                             for ver in item['old_safe']:
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

218)                                 if(versioncompare(ver.split('.'),
219)                                    findversion.split('.')) and
220)                                    not versioncompare(ver.split('.'),
221)                                    safev.split('.'))):
222)                                     safev = ver
223)                             if safev == "9999":
224)                                 safev = item['safe']
225) 
226)                             vulnprint(item['name'], findversion,
227)                                       safev, item['vuln'],
228)                                       mfile, item['subdir'], opts.OUTPUT)
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

229) 
230)                     else:
231)                         if opts.DEBUG:
Hanno Böck format syntax according to...

Hanno Böck authored 7 years ago

232)                             print("regexp failed for " +
Hanno Böck replace print with function...

Hanno Böck authored 13 years ago

233)                                   item['name'] + " on " + mfile)
Bernd Wurst add XML output format

Bernd Wurst authored 15 years ago

234) 
235) if opts.OUTPUT == 'xml':