Skip to content

Instantly share code, notes, and snippets.

@kany
Last active Sep 20, 2019
Embed
What would you like to do?
Toggle Wireless Mode

Toggle Wireless Mode

This script will allow switching between managed and monitor mode for a wireless adapter.

Managed mode is the default wireless adapter mode.

Monitor mode allows monitoring all traffic received on a wireless channel and allows packets to be captured without having to associate with an access point.

Environment

  • VirtualBox 6.0
  • Kali Linux 2019.3
  • Ruby 2.5.5p157
  • Alfa AWUS036NHA Wireless B/G/N USB Adaptor

When wireless adapter is already running in managed mode

root@kali:~/scripts# ./toggle_wireless_mode wlan0
Attempting to toggle adapter wlan0...
Attempting to enable monitor mode...
Bringing wlan0 down...

Killing these processes:

  PID Name
 2822 wpa_supplicant

Enabling monitor mode...
Bringing wlan0 up in monitor mode...
Success! Adapter wlan0 is now running in monitor mode!

When wireless adapter is already running in monitor mode

root@kali:~/scripts# ./toggle_wireless_mode wlan0
Attempting to toggle adapter wlan0...
Attempting to enable managed mode...
Bringing wlan0 down...


Enabling managed mode...
Bringing wlan0 up in managed mode...
Restarting wireless service...
Success! Adapter wlan0 is now running in managed mode!
#!/usr/bin/env ruby

# Make executable
# chmod u+x toggle_wireless_mode

# Call it
# sudo ./toggle_wireless_mode wlan0

require 'open3'

class ToggleWirelessMode
  attr_accessor :adapter, :toggle_mode

  MISSING_ADAPTER_PARAMETER = "\nMissing adapter parameter!\nExample: ./toggle_wireless_mode wlan0\n"

  def initialize(adapter)
    if adapter.empty?
      raise MISSING_ADAPTER_PARAMETER
    else
      @adapter = adapter.first
    end
  end

  def execute!
    puts "Attempting to toggle adapter #{adapter}..."

    if ['managed', 'monitor'].include?(toggle_mode)
      enable_wireless_mode
    else
      raise "\nSomething went wrong. Error: #{toggle_mode}"
    end
  end

  private

  def current_adapter_status
    stdout, stderr, _ = Open3.capture3("iwconfig #{adapter}")

    if stdout.include?('Mode:Managed')
      'managed'
    elsif stdout.include?('Mode:Monitor')
      'monitor'
    else
      stderr
    end
  end

  def toggle_mode
    @toggle_mode ||= case current_adapter_status
    when 'monitor' then 'managed'
    when 'managed' then 'monitor'
    else current_adapter_status
    end
  end

  def mode_toggled?
    %w(monitor managed).include?(toggle_mode) && toggle_mode == current_adapter_status
  end

  def start_process
    puts "Attempting to enable #{toggle_mode} mode..."

    puts "Bringing #{adapter} down..."
    system("sudo ifconfig #{adapter} down")

    system('sudo airmon-ng check kill')

    puts "Enabling #{toggle_mode} mode..."
    system("sudo iwconfig #{adapter} mode #{toggle_mode}")

    puts "Bringing #{adapter} up in #{toggle_mode} mode..."
    system("sudo ifconfig #{adapter} up")
  end

  def enable_wireless_mode
    start_process

    if toggle_mode == 'managed'
      puts "Restarting wireless service..."
      system('sudo service network-manager restart')
    end

    if mode_toggled?
      puts "Success! Adapter #{adapter} is now running in #{toggle_mode} mode!"
    else
      puts "Failed! Adapter #{adapter} is NOT running in #{toggle_mode} mode!"
    end
  end
end

toggle_wireless_mode = ToggleWirelessMode.new(ARGV)
toggle_wireless_mode.execute!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment