GKD認證指令碼

CYHMS發表於2024-04-06

更新日誌:

v0.1:登入方式原始碼指令碼開始編改

執行指令碼:./giwifi.sh 賬號 密碼

v0.2:新增sh儲存賬號密碼功能

建立執行檔案 :vi giwifi.sh

複製貼上下方程式碼

按鍵盤ESC 鍵盤輸入 :x 儲存

給予檔案全部許可權執行 chmod +x giwifi.sh

然後就可以 ./giwifi.sh開始登入啦!

[root@root AM ~]  #vi 1.sh
[root@root AM ~]  #chmod +x 1.sh
[root@root AM ~]  #./1.sh

最後更新時間:2024年4月6日

GitHub連線:等待更新

wget下載連結:等待更新

v0.2精簡版(只有登入功能):理論上這個就夠了

#!/usr/bin/env bash
cd $(
    cd "$(dirname "$0")"
    pwd
)
UA="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.5.1.4 Safari/537.36"
baseUrl="http://192.168.99.2"
first_page=$baseUrl'/gportal/web/login'
post_url=$baseUrl'/gportal/web/authLogin?round='${RANDOM::3}
state_url=$baseUrl'/gportal/web/queryAuthState'
logout_url=$baseUrl'/gportal/web/authLogout'
test_url="http://nettest.gwifi.com.cn"
mywget() {
    resp_file=$(mktemp)
    if wget "$@" -qO $resp_file -U "$UA" --header "Origin:"$baseUrl --timeout 2 --tries 2; then
        cat $resp_file
    fi
    rm $resp_file
}
urlencode() {
    local string="$1"
    local strlen=${#string}
    local encoded=""
    local pos c o
    for ((pos = 0; pos < strlen; pos++)); do
        c=${string:$pos:1}
        case "$c" in
        [-_.~a-zA-Z0-9]) o="${c}" ;;
        *)
            printf -v o '%%%02x' "'$c"
            o=$(echo $o | tr 'a-z' 'A-Z')
            ;;
        esac
        encoded+="${o}"
    done
    echo "${encoded}"
}
urldecode() {
    printf '%b' "$(echo $1 | sed 's/+/ /g; s/%\(..\)/\\x\1/g;')"
}
aes_128_cbc() {
    str2hex() {
        echo -n $1 | hexdump -v -e '/1 "%02x"'
    }
    hex2str() {
        printf "%b" "$(echo -e $1 | sed 's/.\{2\}/\\x&/g')"
    }
    zeropadding() {
        hex=$1
        blocksize=16
        padlen=$((($blocksize - ${#hex} / 2 % $blocksize) % $blocksize))
        for ((i = 1; i <= padlen; i++)); do
            hex+='00'
        done
        echo $hex
    }
    hex_key=$(str2hex $2)
    hex_iv=$(str2hex $3)
    hex_data=$(str2hex $1)
    hex_data_padding=$(zeropadding $hex_data)
    hex2str $hex_data_padding | openssl enc -aes-128-cbc -e -K $hex_key -iv $hex_iv -nopad -base64 -A
}
get() {
    mywget $first_page
}
get_form_input_from_page() {
    line_urlencode() {
        params_urlencode() {
            echo $(urlencode $1)"="$(urlencode $2)
        }
        while read line; do
            new_line=$(echo $line |
                awk -F '=' '{
                print $1 " " $2
                }')
            echo $(params_urlencode $new_line)
        done
    }
    echo $1 | grep -o '<form id=\"'$2'\"[^>]*>.*</form>' | sed 's/<\/form>.*//' |
        grep -o '<input[^>]*>' | grep -o 'name=\".*\" \([\w]*\(=\".*\"\)\? \)*value\(=\"[^\"]*\"\)\?' |
        sed 's/ .* / /g' | sed 's/name=\"\([^"]*\)\" value\(=\"\([^\"]*\)\"\)\?/\1=\3/g' |
        line_urlencode |
        awk '{printf("%s&", $0)}' | sed 's/&$//' |
        sed 's/\(\&name=\)\(\&password=\)//g'
}
json_get() {
    echo $1 | sed -n 's/.*\"'$2'\":[ ]*\([0-9]*\)[^},]*[,}].*/\1/p'
}
post() {
    iv=$(echo $1 | grep -o 'iv=[^\&]*' | sed 's/iv=//g')
    data=$(aes_128_cbc $1 "1234567887654321" $iv)
    msg="data="$(urlencode $data)"&iv=$iv"
    result=$(mywget $post_url --post-data $msg --header "Content-Type:application/x-www-form-urlencoded; charset=UTF-8" --header "Referer:$first_page")
    json_get "$result" "status"
}
queryAuthState() {
    sign=$(echo $1 | grep -o 'sign=[^\&]*' | sed 's/sign=//g')
    result=$(mywget $state_url --post-data "sign=$sign" --header "Content-Type:application/x-www-form-urlencoded; charset=UTF-8")
    json_get "$result" "status"
}
checkAccessInternet() {
    result=$(mywget $test_url)
    if [ -n "$internetCheck" ]; then
        echo 1
    else
        echo 0
    fi
}
logout() {
    data=$(get_form_input_from_page "$1" "frmLogout")
    result=$(mywget $logout_url --post-data "$data" --header "Content-Type:application/x-www-form-urlencoded; charset=UTF-8")
    json_get "$result" "status"
}
login() {
    html=$(get)
    if [ -z "$html" ]; then
        exit 101
    fi
    myparam=$(get_form_input_from_page "$html" "frmLogin")
    if [ -z "myparam" ]; then
        exit 102
    fi
    myparam=$myparam"&name=""$(urlencode $1)""&password=""$(urlencode $2)"
    authState=$(queryAuthState)
    if [ $authState -eq 1 ]; then
        echo "已連線[already online]"
        logoutBack=$(logout "$html")
        if [ $logoutBack -eq 1 ]; then
            echo "登出成功[logout success]"
        fi
    fi
    echo $(post $myparam)
}
username=""
password=""
login $username $password

相關文章