Author Topic: Custom public wifi connect script | DHCP issues  (Read 730 times)

0 Members and 1 Guest are viewing this topic.

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Custom public wifi connect script | DHCP issues
« on: December 02, 2014, 04:04:58 am »
This would also fit in the Scripting Language board as well, but I think it's more fitting here.

In the past, I've used wicd, NetworkManager, and things of that nature. However, I always had issues with both of them disconnecting constantly and hardware compatibility or whatever. This was a long time ago. Ever since I've simply been using wpa_supplicant + dhcpcd, or netctl on Arch. Simple is better, and I've never had connection issues doing it manually. The only trouble is that it's not always so convenient to do this  when you are on the move connecting to public wifi here and there. So I wrote a simple bash script to automate this. It takes the ssid and password as command-line arguments.
Quote
wifi_util Jimwifi password123
For some reason, even though it appears to do the same thing as when I connect to my home network with wpa_supplicant and dhcpcd, it fails with varied results. Here's the code first:
Code: (bash) [Select]
#!/bin/bash
# Quick public wifi connect tool

CONFIG=/home/user/dir/dir/wpa_supplicant.conf

# Clean up dhcp leases and process files
rm /var/lib/dhcpcd/* &>/dev/null
rm /var/run/dhcpcd-* &>/dev/null
rm /var/run/wpa_supplicant/*

# Easy creation of wpa_supplicant
echo "ctrl_interface=/var/run/wpa_supplicant" > $CONFIG
echo -e "ctrl_interface_group=root\n" >> $CONFIG
wpa_passphrase "$1" "$2" >> $CONFIG

# Connect
wpa_supplicant -B -Dwext -i wlan0 -c $CONFIG
dhcpcd wlan0

This is what I normally see:
Quote
dhcpcd[2065]: version 6.0.5 starting
dhcpcd[2065]: wlan0: waiting for carrier
dhcpcd[2065]: wlan0: carrier acquired
dhcpcd[2065]: wlan0: soliciting an IPv6 router
dhcpcd[2065]: wlan0: soliciting a DHCP lease
dhcpcd[2065]: wlan0: offered 172.20.1.7 from 172.20.1.1
dhcpcd[2065]: wlan0: leased 172.20.1.7 for 86400 seconds
dhcpcd[2065]: wlan0: adding host route to 172.20.1.7 via 127.0.0.1  < these seem related
dhcpcd[2065]: wlan0: ipv4_addroute: Network is unreachable           < and significant
dhcpcd[2065]: wlan0: adding route to 172.20.1.0/24
dhcpcd[2065]: wlan0: adding default route via 172.20.1.1
dhcpcd[2065]: forked to background, child pid 2107

The rest of the time it times out with this:
Quote
dhcpcd[1994]: version 6.0.5 starting
dhcpcd[1994]: wlan0: waiting for carrier
dhcpcd[1994]: timed out
dhcpcd[1994]: allowing 8 seconds for IPv4LL timeout
dhcpcd[1994]: wlan0: carrier acquired
dhcpcd[1994]: wlan0: soliciting an IPv6 router
dhcpcd[1994]: wlan0: soliciting a DHCP lease
dhcpcd[1994]: timed out
dhcpcd[1994]: exited

I'm not sure why this isn't working. Right before any error messages, I see that it successfully initializes wpa_supplicant, every time. So the problem seems to lie with DHCP somehow. Normally, I would connect simply with /etc/rc.d/rc.local:
Code: (text) [Select]
#!/bin/sh
#
# /etc/rc.d/rc.local:  Local system initialization script.
# Put scripts you want executed on shutdown in:
# /etc/rc.d/rc.local_shutdown

# Load alternative modules
modprobe rtl8188ee

# Connect to wireless internet
wpa_supplicant -B -Dwext -i wlan0 -c /etc/wpa_supplicant.conf < Same thing my script does
dhcpcd wlan0                                                              <
As you can see it's the same thing really. It just initializes wpa_supplicant and runs dhcpcd on startup. Both /etc/wpa_supplicant.conf and my custom wpa_supplicant.conf file are valid and get initialized successfully. I don't get it.
« Last Edit: December 02, 2014, 04:07:33 am by lucid »
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: Custom public wifi connect script | DHCP issues
« Reply #1 on: December 02, 2014, 05:59:37 am »
Can you stuff -d in wpa_supplicant and post results?
« Last Edit: December 02, 2014, 06:41:17 am by proxx »
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: Custom public wifi connect script | DHCP issues
« Reply #2 on: December 02, 2014, 07:13:17 am »
That did it! I guess it's my fault for not knowing how to debug wpa_supplicant. Thanks for the tip proxx. Here's the output:
Quote
wpa_supplicant v2.0
random: Trying to read entropy from /dev/random
Successfully initialized wpa_supplicant
Initializing interface 'wlan0' conf '/home/lucid/sec/wifi/wpa_supplicant.conf' driver 'wext'
ctrl_interface 'N/A' bridge 'N/A'
Configuration file '/home/lucid/sec/wifi/wpa_supplicant.conf' ->
'/home/lucid/sec/wifi/wpa_supplicant.conf'
Reading configuration file '/home/lucid/sec/wifi/wpa_supplicant.conf'
ctrl_interface='/var/run/wpa_supplicant'
ctrl_interface_group='root'
Priority group 0
   id=0 ssid='redacted'
WEXT: cfg80211-based driver detected
wext: interface wlan0 phy: phy0
rfkill: initial event: idx=1 type=1 op=0 soft=0 hard=0
rfkill: initial event: idx=2 type=1 op=0 soft=0 hard=0
SIOCGIWRANGE: WE(compiled)=22 WE(source)=21 enc_capa=0xf
  capabilities: key_mgmt 0xf enc 0x1f flags 0x0
netlink: Operstate: linkmode=1, operstate=5
wlan0: Own MAC address: redacted
wpa_driver_wext_set_key: alg=0 key_idx=0 set_tx=0 seq_len=0 key_len=0
wpa_driver_wext_set_key: alg=0 key_idx=1 set_tx=0 seq_len=0 key_len=0
wpa_driver_wext_set_key: alg=0 key_idx=2 set_tx=0 seq_len=0 key_len=0
wpa_driver_wext_set_key: alg=0 key_idx=3 set_tx=0 seq_len=0 key_len=0
wpa_driver_wext_set_countermeasures
wlan0: RSN: flushing PMKID list in the driver
wlan0: Setting scan request: 0 sec 100000 usec
WPS: Set UUID for interface wlan0
WPS: UUID based on MAC address - hexdump(len=16): 13 d9 02 08 31 51 5c a6 b7 ec 82 9b 35 1a b1 bd
EAPOL: SUPP_PAE entering state DISCONNECTED
EAPOL: Supplicant port status: Unauthorized
EAPOL: KEY_RX entering state NO_KEY_RECEIVE
EAPOL: SUPP_BE entering state INITIALIZE
EAP: EAP entering state DISABLED
EAPOL: Supplicant port status: Unauthorized
EAPOL: Supplicant port status: Unauthorized
Using existing control interface directory.
ctrl_interface_group=0 (from group name 'root')
wlan0: Added interface wlan0
wlan0: State: DISCONNECTED -> DISCONNECTED
wpa_driver_wext_set_operstate: operstate 0->0 (DORMANT)
netlink: Operstate: linkmode=-1, operstate=5
Daemonize..
dhcpcd[4462]: version 6.0.5 starting
dhcpcd[4462]: wlan0: waiting for carrier
dhcpcd[4462]: wlan0: carrier acquired
dhcpcd[4462]: wlan0: soliciting an IPv6 router
dhcpcd[4462]: wlan0: soliciting a DHCP lease
dhcpcd[4462]: wlan0: offered 172.20.1.7 from 172.20.1.1
dhcpcd[4462]: wlan0: leased 172.20.1.7 for 86400 seconds
dhcpcd[4462]: wlan0: adding host route to 172.20.1.7 via 127.0.0.1
dhcpcd[4462]: wlan0: ipv4_addroute: Network is unreachable
dhcpcd[4462]: wlan0: adding route to 172.20.1.0/24
dhcpcd[4462]: wlan0: adding default route via 172.20.1.1
dhcpcd[4462]: forked to background, child pid 4485

I ended up looking around more in /var/run, and realized that killing wpa_supplicant and dhcpcd was a much more efficient way to clean up then deleting files. It working now  :D

Also, might as well show you the code:
Code: (bash) [Select]
#!/bin/bash
# Quick public wifi connect tool

CONFIG=/home/lucid/sec/wifi/wpa_supplicant.conf
error=/home/lucid/sec/wifi/wpa_errors.log

# Clean up dhcp leases and process files
killall dhcpcd
killall wpa_supplicant

if [[ -e /var/lib/dhcpcd/dhcpcd-wlan0.lease ]]; then
  rm /var/lib/dhcpcd/dhcpcd-wlan0.lease
fi

# Easy creation of wpa_supplicant
echo "ctrl_interface=/var/run/wpa_supplicant" > $CONFIG
echo -e "ctrl_interface_group=root\n" >> $CONFIG
wpa_passphrase "$1" "$2" >> $CONFIG

# Connect
wpa_supplicant -B -Dwext -d -i wlan0 -c $CONFIG 2> $error
dhcpcd wlan0

*lucid gives proxx a cookie
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: Custom public wifi connect script | DHCP issues
« Reply #3 on: December 02, 2014, 08:12:29 am »
Welcome bro :)
Glad you solved it.

Btw I would add in some kind of ping or tcp handshake with a known host just so you know it actually works :)
« Last Edit: December 02, 2014, 08:16:53 am by proxx »
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: Custom public wifi connect script | DHCP issues
« Reply #4 on: December 02, 2014, 09:03:24 am »
Here's a more complete version for private public wifi
Code: (bash) [Select]
#!/bin/bash
# Quick public wifi connect tool
# Usage: sudo wifi <ssid> <psk>
# Don't forget sudo!

CONFIG=/home/lucid/sec/wifi/wpa_supplicant.conf
error=/home/lucid/sec/wifi/wpa_errors.log

# Spoof mac because fuck off
ifconfig wlan0 down
macchanger -r wlan0
ifconfig wlan0 up

# Clean up dhcp leases and process files
killall dhcpcd &>/dev/null
killall wpa_supplicant &>/dev/null

if [[ -e /var/lib/dhcpcd/dhcpcd-wlan0.lease ]]; then
  rm /var/lib/dhcpcd/dhcpcd-wlan0.lease
fi

# Easy creation of wpa_supplicant
echo "ctrl_interface=/var/run/wpa_supplicant" > $CONFIG
echo -e "ctrl_interface_group=root\n" >> $CONFIG
wpa_passphrase "$1" "$2" >> $CONFIG

# Connect
wpa_supplicant -B -Dwext -i wlan0 -c $CONFIG 2> $error
dhcpcd wlan0

# Test the connection
echo "Running a quick ping test.." && sleep 2
ping -c 2 8.8.8.8
echo -e "\nDone!"

EDIT: Everyone should note that dhcpcd transmits some identifying info when soliciting a lease, and can be easily spoofed. Perhaps I could add something like that in my script.
« Last Edit: December 02, 2014, 09:05:06 am by lucid »
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python