how to create wifi ssid finder using python and scapy

Namaste Friends,




Today, In This Tutorial I Am Going To Show You How To Create Wifi SSID Finder Using Python And Scapy Module.

Python Scapy Module Introduction


Python Scapy Module allow Us, To Capture, Encode&Decode Packets, Sniffing, Scanning And Many Types Of Other Network Related Tasks.

So,  let's Start With Some Basic Knowledge About Packets..

In Wireless Network, Packets Are Like Bunch Of Encoded Data, Used For Transferring And Exchanging Data Between Two Or More Clients And Stations. Well, There Are Various Types Of Packets Are Available For Different Purposes

But Today, We will Focus Our Target Only On Beacon Frames Because Every Available Access Point Continuously Transmit/Send Beacon Frames On Air To Introduce Their Access Point Network Availability.

That's Why Today, We will Only Try to Find And Filter Beacon Frames Available In Our Range.

But For This Process, First We Need To Set Our Wireless Card In Monitor Mode. If You Don't Know How To Do It, Then Check Here Another Tutorial.




Now, let me Explain You Exactly What We Are Going To Do With Our Python Codes.

In Our Codes, First With The Help of Scapy Module, We Will try To Capture All Available Packets On Air Then Again With The Help Of Scapy Module We Will Also Extract And Decode Beacon Frames To Find Available Access Point.  In Simple Words, We Will Try To Extract Beacon Frames Because As I Already Described Above These Frames are Responsible To Find New Available Access Point SSID And BSSID. here,  SSID means AP Name And BSSID means Access Point MAC Address. Now, Let Me Show You Quickly My Codes.

So, Here My Codes.


#!/usr/bin/env python

# import scapy module
import scapy.all as scapy




# Extracted Packet Format 
Pkt_Info = """
---------------[ Packet Captured ]-----------------------
 Subtype  : {}   
 Address 1  : {} | Address 2 : {} [BSSID] 
 Address 3  : {} | Address 4 : {} 
 AP   : {} [SSID]
"""


# GetAPStations Function
def GetAPStation(*args,  **kwargs):
 """
 Function For Filtering Beacon Frames And Extract Access 
 Point Information From Captured Packets.

 """
 ap=[]
 packets=[]
 def PacketFilter(pkt):
  if pkt.haslayer(scapy.Dot11Elt) and pkt.type == 0 and pkt.subtype == 8:
   if pkt.addr2 not in ap:
    ap.append(pkt.addr2)
    packets.append(pkt)
    print Pkt_Info.format(pkt.subtype,pkt.addr1, pkt.addr2, pkt.addr3, pkt.addr4, pkt.info)


 scapy.sniff(prn=PacketFilter, *args, **kwargs)
 return (ap, packets)

# Main Trigger
if __name__=="__main__":

 # Previous Function Trigger
 #
 # here, iface="mon0" for Interface with monitor mode enable
 #
 GetAPStation(iface="mon0", timeout=10)

Now Let Me Break These Codes In Small Parts, For Better Explanation's.


Codes Part 1.


#!/usr/bin/env python
# import scapy module
import scapy.all as scapy

Import Complete Scapy Module As Scapy

Code Part 2.

# Extracted Packet Format 
Pkt_Info = """
---------------[ Packet Captured ]-----------------------
 Subtype  : {}   
 Address 1  : {} | Address 2 : {} [BSSID] 
 Address 3  : {} | Address 4 : {} 
 AP   : {} [SSID]
"""


This Is Only A String Format To Print Packet Information On Terminal.

Code Part 3.


# GetAPStations Function
def GetAPStation(*args,  **kwargs):
 """
 Function For Filtering Beacon Frames And Extract Access 
 Point Information From Captured Packets.

 """
 ap=[]
 packets=[]
 def PacketFilter(pkt):
  if pkt.haslayer(scapy.Dot11Elt) and pkt.type == 0 and pkt.subtype == 8:
   if pkt.addr2 not in ap:
    ap.append(pkt.addr2)
    packets.append(pkt)
    print Pkt_Info.format(pkt.subtype,pkt.addr1, pkt.addr2, pkt.addr3, pkt.addr4, pkt.info)


 scapy.sniff(prn=PacketFilter, *args, **kwargs)
 return (ap, packets)

Here, GetAPStation() Will Works As Main Function,  In This Function, def PacketFilter(pkt) is Responsible For Extracting Useful Data From Captured Packets. In Simple Way, In This Functions,  if pkt.haslayer(scapy.Dot11Elt) and pkt.type == 0 and pkt.subtype == 8. This If Statement will Verify The Availability Of Beacon Frame In Captured Packets And After Verification, Our Function Will Also Check About SSID In Our List To Prevent From print Same Data Again and Again. At the End, print statement is For printing Information in our Terminal. Simple!

At Last Line Of This Function,  scapy.sniff(prn=PacketFilter, *args, **kwargs) Statement Is Very Important And Main Statement For Sniffing Packets Because With The Help Of This Statement Available With Scapy Module, We Can Do Many Types Of Sniffing Very Easily. So, Here With The Help Of Sniff Function We Will Sniff All Packets From Specified Interface And One More Good Feature With This Function Is, This Statement Also Provides Function Calling Facility To Manage Packets. So, Here We Will Pass PacketFilter(pkt) function For Data Extraction.


Code Part 4.

# Main Trigger
if __name__=="__main__":

 # Previous Function Trigger
 #
 # here, iface="mon0" for Interface with monitor mode enable
 #
 GetAPStation(iface="mon0", timeout=10)

At The End, Create A Trigger Function To Call Our Main Function. As You Can See In Above Codes  I Passed Two Arguments As Iface Is For Monitor Mode Interface And timeout is for timeout.


Now To Run Our Sniffing Code Just Type Below Commands

sudo python sniff_ssid.py


And Then, Wait For Few Moment To Find Available Access Points In Range of Your device.

Done!


Thanks For Reading,


For Any Queries Or Help,
Feel Free To Ask In Comment Box,

Written By
SSB

Share this

Related Posts

Previous
Next Post »