836c4db37896ed72bb52fa326db82401c40e00c5
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
30) 
31) import os, glob, pprint, re, optparse, sys, gettext
Bernd Wurst Add XML string escaping

Bernd Wurst authored 16 years ago

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

Hanno Böck authored 17 years ago

33) 
Hanno Böck add fancy output

Hanno Böck authored 16 years ago

34) gettext.textdomain('freewvs')
35) _ = gettext.gettext
Hanno Böck initial commit

Hanno Böck authored 17 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 17 years ago

46) 
Bernd Wurst add XML output format

Bernd Wurst authored 16 years ago

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

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

Bernd Wurst authored 16 years ago

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

Hanno Böck authored 14 years ago

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

Bernd Wurst authored 16 years ago

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

Hanno Böck authored 14 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 14 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 14 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 14 years ago

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

Hanno Böck authored 16 years ago

64)             else:
65)                 print (vuln)
Hanno Böck add fancy output

Hanno Böck authored 16 years ago

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

Hanno Böck authored 14 years ago

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

Hanno Böck authored 10 years ago

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

Bernd Wurst authored 16 years ago

69)     elif style=='xml':
70)         state = 'vulnerable'
71)         if safeversion == 'ok':
72)             state = 'ok'
Hanno Böck more print to function conv...

Hanno Böck authored 14 years ago

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

Bernd Wurst authored 16 years ago

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

Hanno Böck authored 14 years ago

78)             print ('    <safeversion>%s</safeversion>' % escape(safeversion))
79)             print ('    <vulninfo>%s</vulninfo>' % escape(vuln))
80)         print ('  </app>')
Bernd Wurst add XML output format

Bernd Wurst authored 16 years ago

81) 
Hanno Böck initial commit

Hanno Böck authored 17 years ago

82) 
83) pp = pprint.PrettyPrinter(indent=4)
84) 
85) # Command-line options
Bernd Wurst add correct usage notice

Bernd Wurst authored 10 years ago

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

Hanno Böck authored 17 years ago

87) parser.add_option("-a", "--all", action="store_true", dest="ALL",
88)                   help="Show all webapps found, not just vulnerable")
89) 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

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

Bernd Wurst authored 16 years ago

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

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

Bernd Wurst authored 16 years ago

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

Hanno Böck authored 17 years ago

96) opts, args = parser.parse_args()
97) 
98) # Parse vulnerability database
Hanno Böck make code compatible with p...

Hanno Böck authored 10 years ago

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

Hanno Böck authored 17 years ago

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

Hanno Böck authored 16 years ago

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

Bernd Wurst authored 13 years ago

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

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

Hanno Böck authored 17 years ago

138) 
Bernd Wurst add XML output format

Bernd Wurst authored 16 years ago

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

Hanno Böck authored 14 years ago

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

Hanno Böck authored 17 years ago

142) 
143) # start the search
144) 
Hanno Böck fix lot's of pylint issues...

Hanno Böck authored 16 years ago

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

Bernd Wurst authored 12 years ago

151)                     try:
Hanno Böck make code compatible with p...

Hanno Böck authored 10 years ago

152)                       file = open(mfile,encoding="iso-8859-15")
Bernd Wurst error handling when opening...

Bernd Wurst authored 12 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 14 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 16 years ago

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

Bernd Wurst authored 16 years ago

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

Hanno Böck authored 16 years ago

191)                         else:
192)                             if opts.DEBUG:
Hanno Böck replace print with function...

Hanno Böck authored 14 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 16 years ago

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

Hanno Böck authored 16 years ago

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

Bernd Wurst authored 16 years ago

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

Hanno Böck authored 16 years ago

207) 
208)                     else:
209)                         if opts.DEBUG:
Hanno Böck replace print with function...

Hanno Böck authored 14 years ago

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

Bernd Wurst authored 16 years ago

212) 
213) if opts.OUTPUT == 'xml':