Browse code

format syntax according to pep8

Hanno Böck authored on09/01/2017 18:08:20
Showing1 changed files
... ...
@@ -16,86 +16,96 @@
16 16
 # worldwide. This software is distributed without any warranty.
17 17
 #
18 18
 # You should have received a copy of the CC0 Public Domain Dedication along
19
-# with this software. If not, see 
19
+# with this software. If not, see
20 20
 # http://creativecommons.org/publicdomain/zero/1.0/
21 21
 # Nevertheless, in case you use a significant part of this code, we ask (but
22 22
 # not require, see the license) that you keep the authors' names in place and
23 23
 # return your changes to the public. We would be especially happy if you tell
24 24
 # us what you're going to do with this code.
25 25
 
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, glob, pprint, re, optparse, sys, gettext
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
35 41
 from xml.sax.saxutils import escape
36 42
 
37 43
 gettext.textdomain('freewvs')
38 44
 _ = gettext.gettext
39 45
 
46
+
40 47
 def versioncompare(safe_version, find_version):
41 48
     if safe_version == [""]:
42 49
         return True
43 50
     for i in range(min(len(find_version), len(safe_version))):
44
-        if int(find_version[i])<int(safe_version[i]):
51
+        if int(find_version[i]) < int(safe_version[i]):
45 52
             return True
46
-        if int(find_version[i])>int(safe_version[i]):
53
+        if int(find_version[i]) > int(safe_version[i]):
47 54
             return False
48
-    return (len(find_version)<len(safe_version))
55
+    return (len(find_version) < len(safe_version))
56
+
49 57
 
50
-def vulnprint(appname, version, safeversion, vuln, vfilename, subdir, style = None):
58
+def vulnprint(appname, version, safeversion, vuln, vfilename, subdir,
59
+              style=None):
51 60
     appdir = '/'.join(os.path.abspath(vfilename).split('/')[:-1-subdir])
52 61
     if not style:
53
-        print ("%(appname)s %(version)s (%(safeversion)s) %(vuln)s %(appdir)s" \
54
-              % vars())
55
-    elif style=='fancy':
56
-        print (_("Directory: %(appdir)s") % vars())
57
-        if safeversion!="ok":
58
-            if safeversion!="":
59
-                print (_("Vulnerable %(appname)s %(version)s found, please update to " \
60
-                        "%(safeversion)s or above.") % vars())
62
+        print("%(appname)s %(version)s (%(safeversion)s) %(vuln)s"
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())
61 70
             else:
62
-                print (_("Vulnerable %(appname)s %(version)s found, no fixed version available." \
63
-                        ) % vars())
71
+                print(_("Vulnerable %(appname)s %(version)s found, no fixed "
72
+                        "version available.") % vars())
64 73
             if vuln[:3] == "CVE":
65
-                print (_("http://cve.mitre.org/cgi-bin/cvename.cgi?name=%(vuln)s") \
66
-                        % vars())
74
+                print(_("http://cve.mitre.org/cgi-bin/cvename.cgi?name="
75
+                        "%(vuln)s") % vars())
67 76
             else:
68
-                print (vuln)
77
+                print(vuln)
69 78
         else:
70
-            print (_("%(appname)s %(version)s found." ) % vars())
71
-        print ("")
72
-    elif style=='xml':
79
+            print(_("%(appname)s %(version)s found.") % vars())
80
+        print("")
81
+    elif style == 'xml':
73 82
         state = 'vulnerable'
74 83
         if safeversion == 'ok':
75 84
             state = 'ok'
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))
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))
80 89
         if state == 'vulnerable':
81
-            print ('    <safeversion>%s</safeversion>' % escape(safeversion))
82
-            print ('    <vulninfo>%s</vulninfo>' % escape(vuln))
83
-        print ('  </app>')
90
+            print('    <safeversion>%s</safeversion>' % escape(safeversion))
91
+            print('    <vulninfo>%s</vulninfo>' % escape(vuln))
92
+        print('  </app>')
84 93
 
85 94
 
86 95
 pp = pprint.PrettyPrinter(indent=4)
87 96
 
88 97
 # Command-line options
89
-parser = optparse.OptionParser(usage="usage: %prog [options] <path> [<path2> ...]")
98
+parser = optparse.OptionParser(usage="usage: %prog [options] <path>"
99
+                               "[<path2> ...]")
90 100
 parser.add_option("-a", "--all", action="store_true", dest="ALL",
91 101
                   help="Show all webapps found, not just vulnerable")
92 102
 parser.add_option("-d", "--debug", action="store_true", dest="DEBUG",
93
-                  help="Show lots of debugging output, mainly useful"+ \
103
+                  help="Show lots of debugging output, mainly useful"
94 104
                   "for development")
95
-parser.add_option("-f", "--fancy", action="store_const", dest="OUTPUT", const="fancy",
96
-                  help="Show more fancy output")
97
-parser.add_option("-x", "--xml", action="store_const", dest="OUTPUT", const="xml",
98
-                  help="Output results as XML")
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")
99 109
 opts, args = parser.parse_args()
100 110
 
101 111
 # Parse vulnerability database
... ...
@@ -105,7 +115,7 @@ try:
105 115
     config.read(glob.glob('/usr/local/share/freewvs/*.freewvs'))
106 116
     config.read(glob.glob(os.path.dirname(sys.argv[0])+'/freewvsdb/*.freewvs'))
107 117
 except configparser.MissingSectionHeaderError as err:
108
-    print("Error parsing config files: %s" % err);
118
+    print("Error parsing config files: %s" % err)
109 119
 
110 120
 vdb = []
111 121
 for sect in config.sections():
... ...
@@ -120,21 +130,21 @@ for sect in config.sections():
120 130
 
121 131
     # match magic
122 132
     item['variable'] = []
123
-    for var in config.get(sect,'variable').split(","):
124
-        item['variable'].append(re.compile(re.escape(var)+
133
+    for var in config.get(sect, 'variable').split(","):
134
+        item['variable'].append(re.compile(re.escape(var) +
125 135
                                 r"[^0-9.\n\r]*[.]*([0-9.]*[0-9])[^0-9.]"))
126 136
 
127 137
     # optional options
128
-    if config.has_option(sect,'extra_match'):
129
-        item['extra_match'] = config.get(sect,'extra_match')
138
+    if config.has_option(sect, 'extra_match'):
139
+        item['extra_match'] = config.get(sect, 'extra_match')
130 140
     else:
131 141
         item['extra_match'] = False
132
-    if config.has_option(sect,'add_minor'):
133
-        item['add_minor'] = config.get(sect,'add_minor')
142
+    if config.has_option(sect, 'add_minor'):
143
+        item['add_minor'] = config.get(sect, 'add_minor')
134 144
     else:
135 145
         item['add_minor'] = False
136
-    if config.has_option(sect,'old_safe'):
137
-        item['old_safe'] = config.get(sect,'old_safe').split(",")
146
+    if config.has_option(sect, 'old_safe'):
147
+        item['old_safe'] = config.get(sect, 'old_safe').split(",")
138 148
     else:
139 149
         item['old_safe'] = []
140 150
 
... ...
@@ -143,8 +153,8 @@ if opts.DEBUG:
143 153
     pp.pprint(vdb)
144 154
 
145 155
 if opts.OUTPUT == 'xml':
146
-  print ('<?xml version="1.0" ?>')
147
-  print ('<freewvs>')
156
+    print('<?xml version="1.0" ?>')
157
+    print('<freewvs>')
148 158
 
149 159
 # start the search
150 160
 
... ...
@@ -155,9 +165,9 @@ for fdir in args:
155 165
                 if filename == item['file']:
156 166
                     mfile = os.path.join(root, filename)
157 167
                     try:
158
-                      file = open(mfile)
168
+                        file = open(mfile)
159 169
                     except:
160
-                      continue
170
+                        continue
161 171
                     filestr = file.read()
162 172
                     file.close()
163 173
 
... ...
@@ -181,40 +191,40 @@ for fdir in args:
181 191
                         # Very ugly phpbb workaround
182 192
                         if item['add_minor']:
183 193
                             findversion = findversion.split('.')
184
-                            findversion[-1] = str(int(findversion[-1])+
185
-                                            int(item['add_minor']))
194
+                            findversion[-1] = str(int(findversion[-1]) +
195
+                                                  int(item['add_minor']))
186 196
                             findversion = '.'.join(findversion)
187 197
 
188
-                        if not (versioncompare(item['safe'].split('.'), \
198
+                        if not (versioncompare(item['safe'].split('.'),
189 199
                                 findversion.split('.'))) or \
190
-                                item['old_safe'].count(findversion)>0:
200
+                                item['old_safe'].count(findversion) > 0:
191 201
                             if opts.ALL:
192 202
                                 if opts.DEBUG:
193
-                                    print ("File "+mfile)
194
-                                vulnprint(item['name'], findversion, \
195
-                                          "ok", "", mfile, item['subdir'], \
203
+                                    print("File "+mfile)
204
+                                vulnprint(item['name'], findversion,
205
+                                          "ok", "", mfile, item['subdir'],
196 206
                                           opts.OUTPUT)
197 207
                         else:
198 208
                             if opts.DEBUG:
199
-                                print ("File "+mfile)
200
-                            safev="9999"
209
+                                print("File " + mfile)
210
+                            safev = "9999"
201 211
                             for ver in item['old_safe']:
202
-                                if (versioncompare(ver.split('.'), \
203
-                                    findversion.split('.') ) and \
204
-                                    not versioncompare(ver.split('.'), \
205
-                                    safev.split('.')) ):
206
-                                    safev=ver
207
-                            if safev=="9999":
208
-                                safev=item['safe']
209
-
210
-                            vulnprint (item['name'], findversion, \
211
-                                       safev, item['vuln'], \
212
-                                       mfile, item['subdir'], opts.OUTPUT)
212
+                                if(versioncompare(ver.split('.'),
213
+                                   findversion.split('.')) and
214
+                                   not versioncompare(ver.split('.'),
215
+                                   safev.split('.'))):
216
+                                    safev = ver
217
+                            if safev == "9999":
218
+                                safev = item['safe']
219
+
220
+                            vulnprint(item['name'], findversion,
221
+                                      safev, item['vuln'],
222
+                                      mfile, item['subdir'], opts.OUTPUT)
213 223
 
214 224
                     else:
215 225
                         if opts.DEBUG:
216
-                            print ("regexp failed for " + \
226
+                            print("regexp failed for " +
217 227
                                   item['name'] + " on " + mfile)
218 228
 
219 229
 if opts.OUTPUT == 'xml':
220
-  print ('</freewvs>')
230
+    print('</freewvs>')