summaryrefslogtreecommitdiffstats
path: root/usr/bin/autowifi
blob: 62ae28381b5a3bbcc6515a067c7117563afb0895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/sh -x
wifi=wlan0
iface=@wifi-iface[0]
radio=$(uci get wireless.${iface}.device)
connect_wifi(){
    # channel ssid encryption key
    uci set wireless.${iface}.mode=sta

    ifconfig $wifi up
    uci set wireless.${radio}.channel=$1
    uci set "wireless.${iface}.ssid=$2"
    if [ $3 == "none" ] ; then
        uci set wireless.${iface}.encryption=none
        uci -q delete wireless.${iface}.key
    else
        uci set "wireless.${iface}.key=$4"
        uci set wireless.${iface}.encryption=$3
    fi 
    uci commit wireless
    wifi up
}

print_iwlist_env(){
    # takes environment:
    #   count
    #   MAC
    #   CHANNEL
    #   QUALITY
    #   ENCRYPTION
    #   ESSID
    #   WPA
    #   WPA2
    for i in ESSID MAC CHANNEL QUALITY ENCRYPTION WPA WPA2;do
        eval echo ${i}_${count}=\$${i}
    done
}

iwlist_scan(){
    ifconfig $wifi up

    count=0
    
    iwlist scan ${1:-} 2>/dev/null | ( while read line;
    do
        case "$line" in
            *"Cell "*)
                [ $count -eq  0 ] || print_iwlist_env
                WPA=0
                WPA2=0
                : $((count+=1))
                MAC="${line#*Address: }"
                ;;
            *Channel:*)
                CHANNEL="${line#*:}"
                ;;
            *Quality=*)
                QUALITY="`printf '%s' ${line#*Quality=} | cut -d/  -f 1`"
                ;;
            *"Encryption key:"*)
                ENCRYPTION="${line#*key:}"
                ;;
            *ESSID:*)
                ESSID="${line#*ESSID:}"
                ;;
            *"IE: IEEE 802.11i/WPA2"*)
                WPA2=1
                ;;
            *"IE: WPA Version 1"*)
                WPA=1
                ;;
            *);;
        esac
    done; print_iwlist_env ;echo WIFI_COUNT=$count)
}
crack_wifi(){
    #SSID MAC CHANNEL ENCRYPTION WPA WPA2
    all_led timer
    if [ "$4" == off ];then
        encr=open
    elif [ "$6" -eq 1 ]; then
        encr=psk2
    elif [ "$5" -eq 1 ]; then
        encr=psk
    elif [ "$4" == on ]; then
        encr=wep
    fi
    for hack in $(find /usr/lib/autowifi/ -type f); do
        key=$($hack "$@");
        ret=$?
        if [ $ret -eq 0 ];then
            connect_wifi "$3" "$1" $encr "$key"
            sleep 5
            if check_gateway; then
                (cat /etc/autowifi/wifi_keys | grep -v "$1|$2|" ; echo "$1|$2|$key" ) | sort | uniq  > /etc/autowifi/wifi_keys2
                mv /etc/autowifi/wifi_keys2 /etc/autowifi/wifi_keys
                echo "yay gateway"
                check_internet && all_led none && return 0
            fi
        fi
    done
    return 1
}
check_gateway(){
    ping -c 1 -w 5 $(ip route | awk '/default/{print $3}')
}
check_internet(){
    secret=$(wget -O- http://euer.krebsco.de/secret)
    if [ "$secret" == "1337" ]; then
        return 0
    else
        return 1
    fi
}
loop_over_networks(){
    . /tmp/${wifi}.scan
    for i in `seq 1 $WIFI_COUNT`; do
        eval grep -q \${MAC_${i}} /etc/autowifi/blacklist && continue
        eval crack_wifi "\${ESSID_${i}}" \${MAC_${i}} \${CHANNEL_${i}} \${ENCRYPTION_${i}} \${WPA_${i}} \${WPA2_${i}} && break
    done
}

iwlist_scan > /tmp/${wifi}.scan
loop_over_networks

while sleep 60; do
    if ! check_internet; then
        all_led default-on
        iwlist_scan > /tmp/${wifi}.scan
        loop_over_networks
    fi
done