58
58
from lib .core .convert import getUnicode
59
59
from lib .core .convert import htmlUnescape
60
60
from lib .core .convert import stdoutEncode
61
- from lib .core .data import cmdLineOptions
61
+ from lib .core .data import cmdLineOptions , paths
62
62
from lib .core .data import conf
63
63
from lib .core .data import kb
64
64
from lib .core .data import logger
@@ -2554,7 +2554,7 @@ def initCommonOutputs():
2554
2554
if line not in kb .commonOutputs [key ]:
2555
2555
kb .commonOutputs [key ].add (line )
2556
2556
2557
- def getFileItems (filename , commentPrefix = '#' , unicoded = True , lowercase = False , unique = False ):
2557
+ def getFileItems (filename , commentPrefix = '#' , unicoded = True , lowercase = False , unique = False , raiseOnError = True ):
2558
2558
"""
2559
2559
Returns newline delimited items contained inside file
2560
2560
@@ -2567,7 +2567,7 @@ def getFileItems(filename, commentPrefix='#', unicoded=True, lowercase=False, un
2567
2567
if filename :
2568
2568
filename = filename .strip ('"\' ' )
2569
2569
2570
- checkFile (filename )
2570
+ checkFile (filename , raiseOnError = raiseOnError )
2571
2571
2572
2572
try :
2573
2573
with openFile (filename , 'r' , errors = "ignore" ) if unicoded else open (filename , 'r' ) as f :
@@ -5599,18 +5599,36 @@ def checkSums():
5599
5599
5600
5600
retVal = True
5601
5601
5602
- if paths .get ("DIGEST_FILE" ):
5603
- for entry in getFileItems (paths .DIGEST_FILE ):
5604
- match = re .search (r"([0-9a-f]+)\s+([^\s]+)" , entry )
5605
- if match :
5606
- expected , filename = match .groups ()
5607
- filepath = os .path .join (paths .SQLMAP_ROOT_PATH , filename ).replace ('/' , os .path .sep )
5608
- if not checkFile (filepath , False ):
5609
- continue
5610
- with open (filepath , "rb" ) as f :
5611
- content = f .read ()
5612
- if not hashlib .sha256 (content ).hexdigest () == expected :
5613
- retVal &= False
5614
- break
5602
+ for entry in getFileItems (paths .DIGEST_FILE , raiseOnError = False ):
5603
+ file_data = entry .split ()
5604
+ if len (file_data ) == 2 and len (file_data [0 ]) == 64 :
5605
+ filepath = os .path .join (paths .SQLMAP_ROOT_PATH , file_data [1 ]).replace ('/' , os .path .sep )
5606
+ content = openFile (filepath , 'rb' , None ).read ()
5607
+ if isinstance (content , str ):
5608
+ content = content .encode ('utf-8' )
5609
+ if not hashlib .sha256 (content ).hexdigest () == file_data [0 ]:
5610
+ retVal &= False
5611
+ break
5615
5612
5616
5613
return retVal
5614
+
5615
+
5616
+ def updateSums ():
5617
+ # Read existing entries to maintain file order
5618
+ entries = []
5619
+ for entry in getFileItems (paths .DIGEST_FILE ):
5620
+ file_data = entry .split ()
5621
+ if len (file_data ) == 2 and len (file_data [0 ]) == 64 :
5622
+ filepath = os .path .join (paths .SQLMAP_ROOT_PATH , file_data [1 ]).replace ('/' , os .path .sep )
5623
+ content = openFile (filepath , 'rb' , None ).read ()
5624
+ newline = "%s %s\n " % (
5625
+ hashlib .sha256 (content ).hexdigest (),
5626
+ file_data [1 ].encode ('utf-8' ).decode ('utf-8' ),
5627
+ )
5628
+ entries .append (newline )
5629
+
5630
+
5631
+ # Write updated hashes back to file
5632
+ if entries :
5633
+ with open (paths .DIGEST_FILE , "w" ) as f :
5634
+ f .write ("" .join (entries ))
0 commit comments