2012-06-21 48 views
1

我已經閱讀了所有關於python的seg錯誤的堆棧溢出和google,並且我的情況還沒有落入我迄今閱讀的任何內容。對我來說沒有任何意義的Python Seg錯誤

基本上我寫了一個接受來自外部客戶端的HTTP的API。客戶端將一個二進制文件POST到API,我將要運行一些進程並在之後進行存儲。

我正在導入一個模塊,它是用Python編碼的,以便進行實際的數據處理。

我使用API​​ SSH進入機器,並能夠通過命令行解釋器執行進程。但是,當我嘗試在API中執行該過程時,出現了分段錯誤。奇怪的部分對我來說是完全相同的部分代碼字面上複製和粘貼在命令行解釋器工作正常。 Seg錯誤發生在調用導入模塊的函數的地方。

---------------------這是終端解釋器-------------------- ---------------

Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> postdata = open('binfile').read() 
>>> postdata[0:10] 
'MZ\x90\x00\x03\x00\x00\x00\x04\x00' 
>>> import atklite 
>>> metadata = atklite.FileAnalysis(data=postdata).return_analysis() 
>>> metadata 
{'sha1': '1f0c33a18a417bb09f85add08b2a675c1deda5ca', 'analyzetime': 'Thu Jun 21 12:03:23 2012', 'ftype': 'PE32 executable (GUI) Intel 80386, for MS Windows', 'ttime': 1340294603.405788, 'sha256': '287744fc9d7b4f509fc271cce0af873c2d53fd1aa678d96dfc9afa8d6d1ad7a0', 'ssdeep': '768:HA/gyc4PWUSrxWoG+RUlyesIkJAkV+xQHz3ClxEEAdK88uDC4wOVdfxCO/ysRXA9:g/gyc4PWUSrxWoG+RUlyesIkJAkV+xQc', 'md5': '000038fe4b46c210c37bdde767835007', 'size': 28672} 

--------------這是它崩潰的API的一部分---- --------------------

def POST(self): 
       a = web.ctx.env.get("HTTP_X_AUTH_TOKEN", None) 
       creds = authenticator(a) 
       x = web.input() 
       b = x.values() 
       written = open('binfile', 'wb') 
       for char in b[0]: 
         written.write(char) 
       written.close() 
       postdata = open('binfile').read() 
       print postdata[0:10] 
       analysis = atklite.FileAnalysis(data=postdata) #SEG FAULTS HERE!!!! 
       metadata = analysis.return_analysis() 

----------------------- ---- Imported Module function ----------------------------------

class FileAnalysis: 
    results = {'analyzetime': time.ctime(),'ttime': time.time()} 
    hash_libs = ('md5','sha1','sha256') 
    if ERROR: 
    sys.exit('This function requires dependencies that aren\'t satisfied') 
    __ssd = ssdeep() 
    __ms = magic.open(magic.MAGIC_NONE) 
    __ms.load() 
    def __init__(self, data=None, filename=None): 
    if filename: 
     return(self.analyze_file(filename)) 
    if data: 
     return(self.analyze_data(data)) 
    def hash_data(self, data): 
    for lib in self.hash_libs: 
     hl = getattr(hashlib, lib) 
     self.results[lib] = hl(data).hexdigest() 
    def hash_file(self, filename): 
    hlibs = {} 
    size = 0 
    for lib in self.hash_libs: 
     hlibs[lib] = getattr(hashlib, lib)() 
    for chunk in read_chunks(open(filename, 'rb')): 
     # We do this here so we don't have to read twice 
     size += len(chunk) 
     for hl in hlibs: 
      hlibs[hl].update(chunk) 
    self.results['size'] = size 
    for hl in hlibs: 
     self.results[hl] = hlibs[hl].hexdigest() 
    def analyze_data(self, data): 
    if not data: 
     return False 
    self.hash_data(data) 
    self.results['size'] = len(data) 
    self.results['ftype'] = self.__ms.buffer(data) 
    self.results['ssdeep'] = self.__ssd.hash_bytes(data) 
    def analyze_file(self, filename): 
    if not os.path.isfile(filename): 
     raise IOError("File: %s doesn't exist" % filename) 
    # Size is done inside the hash bytes function so we don't have to read multiple times 
    self.hash_file(filename) 
    self.results['ftype'] = self.__ms.file(filename) 
    self.results['ssdeep'] = self.__ssd.hash_file(filename) 
    def return_analysis(self): 
    return (self.results) 
    def dump(self): 
    res = [] 
    for result in self.results: 
     res.append("%s: %s" % (result, self.results[result])) 
    return("\n".join(res)) 

使用鄧肯建議一個它追蹤到這個

> /home/spezzino/ProcessingCode/atklite.py(93)analyze_data() 
-> self.results['ftype'] = self.__ms.buffer(data) 
(Pdb) s 
--Call-- 
> /usr/lib/python2.7/dist-packages/magic.py(126)buffer() 
-> def buffer(self, buf): 
(Pdb) s 
> /usr/lib/python2.7/dist-packages/magic.py(132)buffer() 
-> return _buffer(self._magic_t, buf, len(buf)) 
(Pdb) s 
--Return-- 
> /usr/lib/python2.7/dist-packages/magic.py(132)buffer()->'PE32 executa...or MS Windows' 
-> return _buffer(self._magic_t, buf, len(buf)) 
(Pdb) s 
> /home/spezzino/ProcessingCode/atklite.py(94)analyze_data() 
-> self.results['ssdeep'] = self.__ssd.hash_bytes(data) 
(Pdb) s 
Segmentation fault 
+0

什麼版本的python? – geoffspear

+0

你可以發佈segfault的輸出嗎? –

+0

即時通訊使用Python 2.7.3 – SJP

回答

1

我沒有解釋,但它周圍搜索似乎有一個爲ssdeep替代包裝,稱爲pydeep可能更可靠(好像是包你經常在osx上使用segfaults - 也許這對linux也會更好)。

它提供了幾乎相同的接口(請參閱上面的鏈接),因此應該很容易交換。

+0

ssdeep lib不穩定。我結束了使用pydeep,它的工作。 – SJP

相關問題