Advertisement
v1ral_ITS

Easy vpnbook Credentials Grabber [ command line ]

Mar 29th, 2018
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.50 KB | None | 0 0
  1. #!/bin/sh
  2. # ImPerial TeK. Solutions (ITS)
  3. # Copyright (c) 2018 v1ral <ImPerialTeKSolutions@outlook.com>
  4.  
  5. PROGRAM="vpnbook"
  6. # URL to the site containing user and password
  7. SITE="https://www.vpnbook.com/freevpn"
  8. # URL to the site containing OpenVPN configs
  9. OPENVPN_SITE="https://www.vpnbook.com"
  10. # File where VPNBook credentials get stored
  11. AUTH_FILE="./vpnbook.auth"
  12. # Path to temporary file
  13. AUTH_FILE_TMP="/tmp/vpnbook.$$"
  14. # Folder where OpenVPN configs are created
  15. CONFIG_FOLDER="./"
  16. # Path to temporary folder for config generation
  17. CONFIG_FOLDER_TEMP="/tmp/vpnbook-config.$$"
  18.  
  19. cleanup() {
  20.     rm -f "$AUTH_FILE_TMP"
  21.     rm -rf "$CONFIG_FOLDER_TEMP"
  22. }
  23.  
  24. trap cleanup HUP INT TERM
  25.  
  26. usage() {
  27.     cat << EOF
  28. Usage: $PROGRAM <command> [parameter]
  29. Commands are
  30.     config  Generate OpenVPN configs for non-interactive usage based on configs provided
  31.             on $OPENVPN_SITE
  32.     auth    Extract user and password from $OPENVPN_SITE and save to auth-user-pass file
  33.             (default: $AUTH_FILE)
  34. Parameters are
  35.     -a <file>    Path to the auth_user_pass file (default: $AUTH_FILE)
  36.     -c <folder>  Folder where OpenVPN configs are generated (default: $CONFIG_FOLDER)
  37.     -h           Show this help
  38. EOF
  39. }
  40.  
  41. download_site() {
  42.     local site="$1"
  43.     curl "$site" -s
  44. }
  45.  
  46. extract_credentials() {
  47.     awk -F '[<>]' '
  48.     BEGIN { exit_value = 1 }
  49.     /Username:/ && !user_found { print $5; user_found = 1; next }
  50.     /Password:/ && user_found { print $5; exit_value = 0; exit }
  51.     END { exit exit_value }
  52.     '
  53. }
  54.  
  55. extract_config_urls() {
  56.     local site="$1"
  57.     awk -F '[<>]' -v site=$site '
  58.     /free-openvpn-account/ { split($4, a, /"/); print site a[2] }
  59.     '
  60. }
  61.  
  62. generate_auth() {
  63.     local data="$1"
  64.     if (echo "$data" | extract_credentials) > "$AUTH_FILE_TMP"; then
  65.         mv "$AUTH_FILE_TMP" "$AUTH_FILE"
  66.         chmod 600 "$AUTH_FILE"
  67.     fi
  68. }
  69.  
  70. generate_vpn_config() {
  71.     awk -v auth_file="$AUTH_FILE" '
  72.     # Remove windows newline
  73.     { sub(/\r$/, "") }
  74.     # Add both ports in config file and let OpenVPN select them remote-random
  75.     /^remote/ {
  76.         host = $2; print
  77.         print $1, host, 25000
  78.         if (!remote_random_printed) {
  79.             print "remote-random"
  80.             remote_random_printed = 1
  81.         }
  82.         next
  83.     }
  84.     # Switch authentication from interactive to file based
  85.     /^auth-user-pass/ {
  86.         print $1, auth_file
  87.         print "auth-retry nointeract"
  88.         next
  89.     }
  90.     # Let OpenVPN check the remote certificate
  91.     /^<ca>/ {
  92.         print "ns-cert-type server"
  93.     }
  94.     # Output everything else unchanged
  95.     { print }
  96.     '
  97. }
  98.  
  99. generate_config() {
  100.     local data="$1"
  101.     mkdir "$CONFIG_FOLDER_TEMP" || return
  102.     for url in $(echo "$site_data" | extract_config_urls "$OPENVPN_SITE"); do
  103.         local file="$CONFIG_FOLDER_TEMP/${url##*/}"
  104.         local conf_file="vpnbook-${file##*-}"
  105.         conf_file=${conf_file%.zip}.ovpn
  106.         conf_file="$CONFIG_FOLDER/$(echo $conf_file | tr 'A-Z' 'a-z')"
  107.         curl "$url" -o "$file" -s
  108.         unzip -p "$file"  '*udp53*.ovpn' | generate_vpn_config > "$conf_file"
  109.     done
  110.  
  111.     generate_auth "$data"
  112. }
  113.  
  114. main() {
  115.     case $1 in
  116.         config | auth )
  117.             generator_func="generate_$1"
  118.             ;;
  119.         * )
  120.             usage
  121.             exit 1
  122.             ;;
  123.     esac
  124.     shift
  125.  
  126.     while getopts 'a:c:h' options; do
  127.         case $options in
  128.             a )
  129.                 auth_file=$OPTARG
  130.                 ;;
  131.             c )
  132.                 config_folder=$OPTARG
  133.                 ;;
  134.             h | \?)
  135.                 usage
  136.                 exit 1
  137.                 ;;
  138.         esac
  139.     done
  140.  
  141.     AUTH_FILE=${auth_file:-$AUTH_FILE}
  142.     CONFIG_FOLDER=${config_folder:-$CONFIG_FOLDER}
  143.  
  144.     local site_data="$(download_site "$SITE")"
  145.     $generator_func "$site_data"
  146.     retval=$?
  147.     cleanup
  148.     return $retval
  149. }
  150.  
  151. if [ "$PROGRAM" = ${0##*/} ]; then
  152.     main "$@"
  153.     exit $?
  154. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement