Browse code

argparse instead of deprecated optparse

Hanno Böck authored on08/12/2019 17:54:06
Showing1 changed files
... ...
@@ -28,7 +28,7 @@ import configparser
28 28
 import os
29 29
 import glob
30 30
 import re
31
-import optparse
31
+import argparse
32 32
 import sys
33 33
 from xml.sax.saxutils import escape
34 34
 
... ...
@@ -45,12 +45,12 @@ def versioncompare(safe_version, find_version):
45 45
 
46 46
 
47 47
 def vulnprint(appname, version, safeversion, vuln, vfilename, subdir,
48
-              style=None):
48
+              xml):
49 49
     appdir = '/'.join(os.path.abspath(vfilename).split('/')[:-1 - subdir])
50
-    if not style:
50
+    if not xml:
51 51
         print("%(appname)s %(version)s (%(safeversion)s) %(vuln)s "
52 52
               "%(appdir)s" % vars())
53
-    elif style == 'xml':
53
+    else:
54 54
         state = 'vulnerable'
55 55
         if safeversion == 'ok':
56 56
             state = 'ok'
... ...
@@ -65,15 +65,16 @@ def vulnprint(appname, version, safeversion, vuln, vfilename, subdir,
65 65
 
66 66
 
67 67
 # Command-line options
68
-parser = optparse.OptionParser(usage="usage: %prog [options] <path>"
69
-                               "[<path2> ...]")
70
-parser.add_option("-a", "--all", action="store_true", dest="ALL",
71
-                  help="Show all webapps found, not just vulnerable")
72
-parser.add_option("-x", "--xml", action="store_const", dest="OUTPUT",
73
-                  const="xml", help="Output results as XML")
74
-parser.add_option("-3", "--thirdparty", action="store_true", dest="THIRDPARTY",
75
-                  help="Scan for third-party components like jquery")
76
-opts, args = parser.parse_args()
68
+parser = argparse.ArgumentParser()
69
+parser.add_argument("dirs", nargs="*",
70
+                    help="Directories to scan")
71
+parser.add_argument("-a", "--all", action="store_true",
72
+                    help="Show all webapps found, not just vulnerable")
73
+parser.add_argument("-x", "--xml", action="store_true",
74
+                    help="Output results as XML")
75
+parser.add_argument("-3", "--thirdparty", action="store_true",
76
+                    help="Scan for third-party components like jquery")
77
+opts = parser.parse_args()
77 78
 
78 79
 # Parse vulnerability database
79 80
 config = configparser.ConfigParser()
... ...
@@ -90,7 +91,7 @@ for sect in config.sections():
90 91
     item = {}
91 92
 
92 93
     if (config.getboolean(sect, 'thirdparty', fallback=False)
93
-       and not opts.THIRDPARTY):
94
+       and not opts.thirdparty):
94 95
         continue
95 96
 
96 97
     # base options
... ...
@@ -128,13 +129,13 @@ for sect in config.sections():
128 129
 
129 130
     vdb.append(item)
130 131
 
131
-if opts.OUTPUT == 'xml':
132
+if opts.xml:
132 133
     print('<?xml version="1.0" ?>')
133 134
     print('<freewvs>')
134 135
 
135 136
 # start the search
136 137
 
137
-for fdir in args:
138
+for fdir in opts.dirs:
138 139
     for root, NULL, files in os.walk(fdir):
139 140
         for filename in files:
140 141
             for item in vdb:
... ...
@@ -170,10 +171,10 @@ for fdir in args:
170 171
                     if not (versioncompare(item['safe'].split('.'),
171 172
                             findversion.split('.'))) or \
172 173
                             item['old_safe'].count(findversion) > 0:
173
-                        if opts.ALL:
174
+                        if opts.all:
174 175
                             vulnprint(item['name'], findversion,
175 176
                                       "ok", "", mfile, item['subdir'],
176
-                                      opts.OUTPUT)
177
+                                      opts.xml)
177 178
                     else:
178 179
                         safev = "9999"
179 180
                         for ver in item['old_safe']:
... ...
@@ -187,7 +188,7 @@ for fdir in args:
187 188
 
188 189
                         vulnprint(item['name'], findversion,
189 190
                                   safev, item['vuln'],
190
-                                  mfile, item['subdir'], opts.OUTPUT)
191
+                                  mfile, item['subdir'], opts.xml)
191 192
 
192
-if opts.OUTPUT == 'xml':
193
+if opts.xml:
193 194
     print('</freewvs>')