2014-12-02 2 views
0

나는 scapy와 python을 처음 사용하며, Violent Python : Hacker 's Cookbook의 코드 예제에서 아래 코드를 모델링했습니다. 내 wlan0 인터페이스에서 프로그램을 실행 한 후 AttributeError 오류가 발생합니다. readlinespython의 readlines() 오류

readlines()는 캡처를 목록으로 변환 한 다음 findall()이 문자열 행에서 작동 할 수 있다는 것을 알고 있습니다.

아무도 도와 줄 수 있습니까? 건배. 다음으로 추적

#!/usr/bin/python 

import optparse 
import logging 
import re  # provides support for regular expressions 
logging.getLogger("scapy.runtime").setLevel(logging.ERROR) # suppresses messages which have a lower level of seriousness than error messages 

from scapy.all import * 

def findguest(capture): 
     for line in capture.splitlines(): 
       IPaddr = re.findall("(?:\d{1,3}\.){3}\d{1,3}",line) # findall returns a LIST 
               # must use ,raw 

       if IPaddr: 
         print 'found IP addr '+IPaddr[0] 

def main(): 
     parser = optparse.OptionParser() # create parser object 

     parser.add_option('-i', dest='interface', type='string', help='specify interface to listen on') 
     (opts, args) = parser.parse_args() 

# the below if-else clause is just some error handling.. if no interface is given after -i it will print the usage menu again 
# conf.iface is the standard interface which will be used by the program, so we set it to whatever interface the user gives us 

     if opts.interface == None: 
       print parser.usage 
       exit(0) 
     else: 
       conf.iface = opts.interface 

     try: 
       sniff(filter='tcp', prn=findguest, store=0) 

     except KeyboardInterrupt: 
       exit(0) 

if __name__ == "__main__": 
     main() 

: splitlines를 사용하지 않고는

[email protected] ~/python/wirelessmayhem $ sudo ./hotelguest2.py -i wlan0 
['__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__div__', '__doc__', '__eq__', '__format__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__len__', '__lt__', '__metaclass__', '__module__', '__mul__', '__ne__', '__new__', '__nonzero__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_do_summary', 'add_payload', 'add_underlayer', 'aliastypes', 'answers', 'build', 'build_done', 'build_padding', 'build_ps', 'canvas_dump', 'clone_with', 'command', 'copy', 'decode_payload_as', 'default_fields', 'default_payload_class', 'delfieldval', 'dispatch_hook', 'display', 'dissect', 'dissection_done', 'do_build', 'do_build_payload', 'do_build_ps', 'do_dissect', 'do_dissect_payload', 'do_init_fields', 'explicit', 'extract_padding', 'fields', 'fields_desc', 'fieldtype', 'firstlayer', 'fragment', 'from_hexcap', 'get_field', 'getfield_and_val', 'getfieldval', 'getlayer', 'guess_payload_class', 'hashret', 'haslayer', 'hide_defaults', 'init_fields', 'initialized', 'lastlayer', 'libnet', 'lower_bonds', 'mysummary', 'name', 'overload_fields', 'overloaded_fields', 'packetfields', 'payload', 'payload_guess', 'pdfdump', 'post_build', 'post_dissect', 'post_dissection', 'post_transforms', 'pre_dissect', 'psdump', 'remove_payload', 'remove_underlayer', 'route', 'self_build', 'sent_time', 'setfieldval', 'show', 'show2', 'show_indent', 'sprintf', 'summary', 'time', 'underlayer', 'upper_bonds'] 
Traceback (most recent call last): 
    File "./hotelguest2.py", line 41, in <module> 
    main() 
    File "./hotelguest2.py", line 35, in main 
    sniff(filter='tcp', prn=findguest, store=0) 
    File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 586, in sniff 
    r = prn(p) 
    File "./hotelguest2.py", line 12, in findguest 
    for line in capture.splitlines(): 
    File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 176, in __getattr__ 
    fld,v = self.getfield_and_val(attr) 
    File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 172, in getfield_and_val 
    return self.payload.getfield_and_val(attr) 
    File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 172, in getfield_and_val 
    return self.payload.getfield_and_val(attr) 
    File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 172, in getfield_and_val 
    return self.payload.getfield_and_val(attr) 
    File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 1057, in getfield_and_val 
    raise AttributeError(attr) 
AttributeError: splitlines 

추적()

Traceback (most recent call last): 
    File "./hotelguest2.py", line 38, in <module> 
    main() 
    File "./hotelguest2.py", line 32, in main 
    sniff(filter='tcp', prn=findguest, store=0) 
    File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 586, in sniff 
    r = prn(p) 
    File "./hotelguest2.py", line 11, in findguest 
    IPaddr = re.findall("(?:\d{1,3}\.){3}\d{1,3}",capture) # findall returns a LIST 
    File "/usr/lib/python2.7/re.py", line 177, in findall 
    return _compile(pattern, flags).findall(string) 
TypeError: expected string or buffer 
+0

죄송 캡처에서 작동

가이 기능 findguest에 전달 단지 변수입니다 .. 그냥 내 코드에서 변수()입니다. 나는 그것이 문자열이라고 생각한다. – ams

+0

'findguest'라는 코드와 주변 코드를 보여줘야합니다. 그래서'capture'가 무엇인지 알 수 있습니다. – Evert

+0

'line'은'string'이 아니며 그렇지 않으면'findall'이 불평하지 않습니다. 파일 기술자를 캡처합니까? for 루프 앞에'print capture'를 시도하십시오. –

답변

0

readlines() does not convert capture into a list where capture is a string. 그것은 파일 객체 rather.Use capture.split("\n") 대신 또는 capture.splitlines()

+0

글쎄 처음에 이걸 가지고 있었다 : def findguest (capture) : IPaddr = re.findall ("(? : \ d {1,3} \.) {3} \ d {1,3}", capture) # findall은 LIST'를 반환합니다. 'TypeError : expected string or buffer' 오류가 발생했습니다. 문자열을 캡처하지 못합니까? – ams

+3

'print type (capture)'을 사용하여 타입을 알 수 있습니다 – vks

+0

흠 .. 문자열입니다 .. 왜 위의 것이 실패했는지 확실하지 않습니다. – ams