Python Forum
error in scapy attribute 'haslayer'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error in scapy attribute 'haslayer'
#1
hello all ...
im trying to write a port scanner using scapy module
my code :
from scapy.all import * target = "192.168.1.81" print("Scanning .... " + target + " For Open Ports!\n") for x in range(20 , 100): packet = IP(dst=target)/TCP(dport=x,flags='S') res = sr1(packet ,timeout=0.5) if res.haslayer(TCP) and res.getlayer(TCP).flags==0x12: print("port" + str(x) + "is open!") sr(IP(dst=target)/TCP(dport=res.sport,flags='R'),timeout=0.5) print("scan is complete")
Output:
Scanning .... 192.168.1.81 For Open Ports! Begin emission: Finished sending 1 packets. ..... Received 5 packets, got 0 answers, remaining 1 packets Traceback (most recent call last): File "/home/evilcode1/Downloads/Course/ping.py", line 11, in <module> if res.haslayer(TCP) and res.getlayer(TCP).flags==0x12: AttributeError: 'NoneType' object has no attribute 'haslayer'
any help please
Reply
#2
res doesn't have a .haslayer method, because res is None. So check to make sure it has a value before trying to use it:

 res = sr1(packet ,timeout=0.5) if res and res.haslayer(TCP) and res.getlayer(TCP).flags==0x12:
Reply
#3
(Jan-27-2021, 05:19 PM)nilamo Wrote: res doesn't have a .haslayer method, because res is None. So check to make sure it has a value before trying to use it:

 res = sr1(packet ,timeout=0.5) if res and res.haslayer(TCP) and res.getlayer(TCP).flags==0x12:

thank u its work bro :D
but how i can hide this lines from scapy outputs ?

Output:
Received 1 packets, got 1 answers, remaining 0 packets Port : 21 is open! Begin emission: Finished sending 1 packets. ...... Received 6 packets, got 0 answers, remaining 1 packets Begin emission: Finished sending 1 packets. * Received 1 packets, got 1 answers, remaining 0 packets Port : 22 is open! Begin emission: Finished sending 1 packets.
i need it just to show :
Output:
Port : 22 is open! Port : 21 is open!
Reply
#4
https://scapy.readthedocs.io/en/latest/a...ndrecv.sr1

Use the verbose option.
res = sr1(packet ,timeout=0.5, verbose=False)
Reply
#5
our generic except masks any errors that your code might encounter. Change pass to raise and remove any specific errors. E.g. the first I encountered with your code was:

socket.error: [Errno 1] Operation not permitted
and after running as root I got:

A
ttributeError: 'list' object has no attribute 'haslayer'
and that made me change the code to something that worked (running as root):

from scapy.all import * while 1: pktl = sniff ( iface="eth0" , count = 1 ) pktl.summary() for pkt in pktl: try: if pkt.haslayer(IP): ipsrc =pkt.getlayer(IP).src print ipsrc except: raise
So you probably better of removing the try - except altogether
Reply
#6
(Jan-27-2021, 05:54 PM)nilamo Wrote: https://scapy.readthedocs.io/en/latest/a...ndrecv.sr1

Use the verbose option.
res = sr1(packet ,timeout=0.5, verbose=False)

thank u solved my problem <3
nilamo likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending Packages with Scapy yoda2247 3 3,554 Dec-25-2024, 11:14 PM
Last Post: yoda2247
  Listening on receiving Interface (using scapy) CodyTheCodeNoob 1 3,262 Dec-22-2024, 10:51 PM
Last Post: PolandoFaker
  Error: audioio has no attribute 'AudioOut' netwrok 3 2,515 Oct-22-2023, 05:53 PM
Last Post: netwrok
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 6,799 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Permission issue when using scapy jao 3 21,611 Feb-05-2022, 06:14 PM
Last Post: snippsat
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 9,494 Aug-17-2021, 05:24 PM
Last Post: Franky77
  Attribute Error received not understood (Please Help) crocolicious 5 4,771 Jun-19-2021, 08:45 PM
Last Post: crocolicious
  attribute error instead of correct output MaartenRo 2 3,580 Aug-28-2020, 10:22 AM
Last Post: Larz60+
  attribute error stumped on how to fix it. hank4eva 7 11,609 Aug-11-2020, 04:47 AM
Last Post: hank4eva
  Attribute Error - trying to create a pixel array out of PNG files The_Sarco 1 3,092 Apr-29-2020, 07:10 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.