160160from lib .core .settings import SAFE_VARIABLE_MARKER
161161from lib .core .settings import SENSITIVE_DATA_REGEX
162162from lib .core .settings import SENSITIVE_OPTIONS
163+ from lib .core .settings import STDIN_PIPE_DASH
163164from lib .core .settings import SUPPORTED_DBMS
164165from lib .core .settings import TEXT_TAG_REGEX
165166from lib .core .settings import TIME_STDEV_COEFF
@@ -1165,6 +1166,14 @@ def getHeader(headers, key):
11651166 break
11661167 return retVal
11671168
1169+ def checkPipedInput ():
1170+ """
1171+ Checks whether input to program has been provided via standard input (e.g. cat /tmp/req.txt | python sqlmap.py -r -)
1172+ # Reference: https://stackoverflow.com/a/33873570
1173+ """
1174+
1175+ return not os .isatty (sys .stdin .fileno ())
1176+
11681177def checkFile (filename , raiseOnError = True ):
11691178 """
11701179 Checks for file existence and readability
@@ -1178,19 +1187,22 @@ def checkFile(filename, raiseOnError=True):
11781187 if filename :
11791188 filename = filename .strip ('"\' ' )
11801189
1181- try :
1182- if filename is None or not os .path .isfile (filename ):
1183- valid = False
1184- except :
1185- valid = False
1186-
1187- if valid :
1190+ if filename == STDIN_PIPE_DASH :
1191+ return checkPipedInput ()
1192+ else :
11881193 try :
1189- with open ( filename , "rb" ):
1190- pass
1194+ if filename is None or not os . path . isfile ( filename ):
1195+ valid = False
11911196 except :
11921197 valid = False
11931198
1199+ if valid :
1200+ try :
1201+ with open (filename , "rb" ):
1202+ pass
1203+ except :
1204+ valid = False
1205+
11941206 if not valid and raiseOnError :
11951207 raise SqlmapSystemException ("unable to read file '%s'" % filename )
11961208
@@ -3305,13 +3317,19 @@ def openFile(filename, mode='r', encoding=UNICODE_ENCODING, errors="replace", bu
33053317 Returns file handle of a given filename
33063318 """
33073319
3308- try :
3309- return codecs .open (filename , mode , encoding , errors , buffering )
3310- except IOError :
3311- errMsg = "there has been a file opening error for filename '%s'. " % filename
3312- errMsg += "Please check %s permissions on a file " % ("write" if mode and ('w' in mode or 'a' in mode or '+' in mode ) else "read" )
3313- errMsg += "and that it's not locked by another process."
3314- raise SqlmapSystemException (errMsg )
3320+ if filename == STDIN_PIPE_DASH :
3321+ if filename not in kb .cache .content :
3322+ kb .cache .content [filename ] = sys .stdin .read ()
3323+
3324+ return contextlib .closing (StringIO (readCachedFileContent (filename )))
3325+ else :
3326+ try :
3327+ return codecs .open (filename , mode , encoding , errors , buffering )
3328+ except IOError :
3329+ errMsg = "there has been a file opening error for filename '%s'. " % filename
3330+ errMsg += "Please check %s permissions on a file " % ("write" if mode and ('w' in mode or 'a' in mode or '+' in mode ) else "read" )
3331+ errMsg += "and that it's not locked by another process."
3332+ raise SqlmapSystemException (errMsg )
33153333
33163334def decodeIntToUnicode (value ):
33173335 """
@@ -4797,14 +4815,7 @@ def _parseBurpLog(content):
47974815 if not (conf .scope and not re .search (conf .scope , url , re .I )):
47984816 yield (url , conf .method or method , data , cookie , tuple (headers ))
47994817
4800- checkFile (reqFile )
4801- try :
4802- with openFile (reqFile , "rb" ) as f :
4803- content = f .read ()
4804- except (IOError , OSError , MemoryError ) as ex :
4805- errMsg = "something went wrong while trying "
4806- errMsg += "to read the content of file '%s' ('%s')" % (reqFile , getSafeExString (ex ))
4807- raise SqlmapSystemException (errMsg )
4818+ content = readCachedFileContent (reqFile )
48084819
48094820 if conf .scope :
48104821 logger .info ("using regular expression '%s' for filtering targets" % conf .scope )
0 commit comments