3131from lib .core .common import parseTargetUrl
3232from lib .core .common import paths
3333from lib .core .common import randomRange
34+ from lib .core .common import readCachedFileContent
3435from lib .core .common import readInput
3536from lib .core .common import runningAsAdmin
3637from lib .core .common import sanitizeStr
@@ -835,7 +836,6 @@ def __setHTTPUserAgent():
835836 * A random value read from a list of User-Agent headers from a
836837 file choosed as user option
837838 """
838-
839839 if conf .agent :
840840 debugMsg = "setting the HTTP User-Agent header"
841841 logger .debug (debugMsg )
@@ -856,39 +856,28 @@ def __setHTTPUserAgent():
856856
857857 return
858858
859- debugMsg = "fetching random HTTP User-Agent header from "
860- debugMsg += "file '%s'" % conf .userAgentsFile
861- logger .debug (debugMsg )
862-
863- try :
864- fd = codecs .open (conf .userAgentsFile , "r" , conf .dataEncoding )
865- except IOError :
866- warnMsg = "unable to read HTTP User-Agent header "
867- warnMsg += "file '%s'" % conf .userAgentsFile
868- logger .warn (warnMsg )
869-
870- conf .httpHeaders .append (("User-Agent" , __defaultHTTPUserAgent ()))
871-
872- return
873-
874- __count = 0
875- __userAgents = []
859+ if not kb .userAgents :
860+ debugMsg = "loading random HTTP User-Agent header(s) from "
861+ debugMsg += "file '%s'" % conf .userAgentsFile
862+ logger .debug (debugMsg )
876863
877- while True :
878- line = fd .readline ()
864+ try :
865+ kb .userAgents = getFileItems (conf .userAgentsFile )
866+ except IOError :
867+ warnMsg = "unable to read HTTP User-Agent header "
868+ warnMsg += "file '%s'" % conf .userAgentsFile
869+ logger .warn (warnMsg )
879870
880- if not line :
881- break
871+ conf .httpHeaders .append (("User-Agent" , __defaultHTTPUserAgent ()))
882872
883- __userAgents .append (line )
884- __count += 1
873+ return
885874
886- fd . close ( )
875+ __count = len ( kb . userAgents )
887876
888877 if __count == 1 :
889- __userAgent = __userAgents [0 ]
878+ __userAgent = kb . userAgents [0 ]
890879 else :
891- __userAgent = __userAgents [randomRange (stop = __count )]
880+ __userAgent = kb . userAgents [randomRange (stop = __count )]
892881
893882 __userAgent = sanitizeStr (__userAgent )
894883 conf .httpHeaders .append (("User-Agent" , __userAgent ))
@@ -1114,6 +1103,7 @@ def __setKnowledgeBaseAttributes():
11141103 kb .unionPosition = None
11151104 kb .unionNegative = False
11161105 kb .unionFalseCond = False
1106+ kb .userAgents = None
11171107 kb .valueStack = []
11181108
11191109def __saveCmdline ():
0 commit comments