_                _   _                 ____            _     _ 	
   / \   _ __   ___ | |_| |__   ___ _ __  |  _ \ _ __ ___ (_) __| |	
  / _ \ | '_ \ / _ \| __| '_ \ / _ \ '__| | | | | '__/ _ \| |/ _\` |	
 / ___ \| | | | (_) | |_| | | |  __/ |    | |_| | | | (_) | | (_| |	
/_/   \_\_| |_|\___/ \__|_| |_|\___|_|    |____/|_|  \___/|_|\__,_|	
                                                                bbs
  XQTRs lair...
Home // Blog // NULL emag. // Files // Docs // Tutors // GitHub repo

                                                                                
                                                                                
  A simple function in Python3 to get some SAUCE data. It wont retrieve all     
  data as we pad the last 32 bytes and also it will ignore any COMMENTS         
  block. Your homework is to extend it... :p                                    
                                                                                
                                                                                
-- use python3                                                                  
#!/usr/bin/python3                                                              
                                                                                
-- import libraries that we need                                                
import os                                                                       
import sys                                                                      
                                                                                
-- the struct library is the important stuff here. with it we will get the      
   sauce structure and unpack it to values that we can use.                     
import struct                                                                   
                                                                                
-- the script needs a filename/path as its first parameter.                     
filename = sys.argv[1]                                                          
                                                                                
-- now let's get that sauce data...                                             
def getsauce(filename):                                                         
                                                                                
    -- open the given file for reading as binary file.                          
    f = open(filename, 'rb')                                                    
                                                                                
    -- go to the 128th byte before the end of the file, because that's where    
       the sauce data is being stored.                                          
    f.seek(-128,2)                                                              
                                                                                
    -- define the structure that we are going to read.                          
    saucestr = "<5s2s35s20s20s8si32x"                                           
                                                                                
    -- calculate the size of the structure                                      
    sf = struct.calcsize(saucestr)                                              
                                                                                
    -- and read it from the file                                                
    sauce = f.read(sf)                                                          
    s = struct.unpack(saucestr,sauce)                                           
                                                                                
    -- close the file                                                           
    f.close()                                                                   
                                                                                
    -- if our variable, contains a SAUCE string, this means the file has        
       sauce data, otherwise it doesn't.                                        
    if ''.join(str(s[0]).strip("b'")) == "SAUCE":                               
        -- return all the sauce data, as a string, as the result of the         
           function                                                             
        return s                                                                
    else:                                                                       
        return -1                                                               
                                                                                
-- if successful this will pring the sauce data of the file.                    
print(getsauce(filename))                                                       
                                                                                
                                                                                
  complete script below. copy/paste into a text file and make it executable.    
                                                                                
  -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-   
                                                                                
#!/usr/bin/python3                                                              
                                                                                
import os                                                                       
import struct                                                                   
import sys                                                                      
                                                                                
filename = sys.argv[1]                                                          
                                                                                
def getsauce(filename):                                                         
    f = open(filename, 'rb')                                                    
    f.seek(-128,2)                                                              
    saucestr = "<5s2s35s20s20s8si32x"                                           
    sf = struct.calcsize(saucestr)                                              
    sauce = f.read(sf)                                                          
    s = struct.unpack(saucestr,sauce)                                           
    f.close()                                                                   
    if ''.join(str(s[0]).strip("b'")) == "SAUCE":                               
        return s                                                                
    else:                                                                       
        return -1                                                               
                                                                                
print(getsauce(filename))                                                       
                                                                                
  -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-