黑帽联盟

 找回密码
 会员注册
查看: 1574|回复: 0
打印 上一主题 下一主题

[脚本语言] linux开设无线热点的shell脚本

[复制链接]

293

主题

18

听众

955

积分

管理员

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    奋斗
    2023-10-26 13:13
  • 签到天数: 358 天

    [LV.8]以坛为家I

    #!/bin/bash

    #####################################
    #Author: 定位                  #
    #email: cnblackhat@qq.com       #
    #QQ: 1074189538                     #
    #Version: 1.0                       #
    #Note: If you have some GOOD ideas  #
    #      or advice, please mail me^^  #
    #Modified: cnblackhat@ 2016-10-20 #
    #          (cnblackhat@qq.com)    #
    #####################################

    #####################################
    ShareNet=eth0
    Wmode=ad-hoc
    Wchannel=auto
    Wessid=WiFi-Network
    WInterface=$1
    # the length of encrypted key is determined by device,
    # running `sudo iwlist interface keys` to check
    # the supported length
    WPasskey=
    Wkey=off
    Igateway=192.168.2.254
    Inetmask=255.255.255.0
    Inetwork=192.168.2.0
    DhcpRangeMin=192.168.2.10
    DhcpRangeMax=192.168.2.105
    #####################################

    adhocFolder=/etc/adhoc
    dnsmasqFile=$adhocFolder/dnsmasq.conf
    resolvFile=/etc/resolv.conf
    dnsmasqPid=/var/run/dnsmasq.pid
    dnsmasqLeases=/var/run/dnsmasq.leases

    # run command and print error message
    function run_command
    {
      local command="$1"
      local msg
      msg=$(${command} 2>&1)
      sleep 0.5
      if [ $? -ne 0 ]; then
        echo -e "    [ \033[31mFAILED\033[0m ]"
        echo    "    ${msg}"
        exit 1
      else
        echo -e "    [ GOOD ]"
      fi
    }

    # start Ad-hoc
    function adhoc_start
    {
      echo "Starting Ad-hoc..."
      check_dnsmasq
      sleep 0.5

      echo -n "Setting $WInterface gateway[$Igateway] and netmask[$Inetmask]..."
      run_command "ifconfig $WInterface $Igateway netmask $Inetmask"

      echo -n "Setting down $WInterface..."
      run_command "ifconfig $WInterface down"
      # only is wlan0 shut down, you can set mode
      echo -n "Setting $WInterface mode[$Wmode]..."
      run_command "iwconfig $WInterface mode $Wmode"

      echo -n "Setting $WInterface essid[$Wessid]..."
      run_command "iwconfig $WInterface essid $Wessid"

      echo -n "Setting $WInterface channel[$Wchannel]..."
      run_command "iwconfig $WInterface channel $Wchannel"

      if [ -n "$WPasskey" ]; then
        Wkey="restricted"
        echo -n "Setting $WInterface password[$WPasskey]..."
        run_command "iwconfig $WInterface key sWPasskey"
      else
        Wkey="off"
      fi

      echo -n "Setting $WInterface key type[$Wkey]..."
      run_command "iwconfig $WInterface key $Wkey"
      if [ "$Wkey" = "off" ]; then
        echo -e "    [ \033[31mWARNING\033[0m, non-encrypted network ]"
      fi

      echo -n "Setting up $WInterface..."
      run_command "ifconfig $WInterface up"

      echo "Setting iptable:"
      #remove the old rules
      echo -n "    remove old rules..."
      iptables -N wireless-adhoc
      iptables -F wireless-adhoc
      iptables -t nat -F PREROUTING
      iptables -t nat -F POSTROUTING
      iptables -t nat -F
      echo "    [ GOOD ]"
      #bring up the NAT rules
      echo -n "    bring up NAT rules..."
      iptables -A wireless-adhoc -m state --state ESTABLISHED,RELATED -j ACCEPT
      iptables -A wireless-adhoc -s $Inetwork/24 -j ACCEPT
      iptables -A wireless-adhoc -p 47 -j ACCEPT
      iptables -A wireless-adhoc -j DROP
      iptables -A FORWARD -m state --state INVALID -j DROP
      iptables -A FORWARD -j wireless-adhoc
      iptables -t nat -I POSTROUTING -s $Inetwork/24 -j MASQUERADE
      echo "    [ GOOD ]"

      sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

      echo -n "Starting DNS and DHCP server..."
      run_command "dnsmasq -i $WInterface \
                            --resolv-file=$resolvFile \
                            --conf-file=$dnsmasqFile"
      echo "Completed!^^"
    }

    function wait_a_moment
    {
      sleep 0.5
      echo -n "."
    }

    # stop Ad-hoc
    function adhoc_stop
    {
      echo "Stopping adhoc ..."
      sh -c "echo 0 > /proc/sys/net/ipv4/ip_forward"
      echo -n "."
      # only is wlan0 shut down, you can set mode
      ifconfig $WInterface down
      wait_a_moment

      # stop adhoc mode
      iwconfig $WInterface mode managed
      wait_a_moment

      iwconfig $WInterface key off
      wait_a_moment

      iwconfig $WInterface essid any
      wait_a_moment

      # remove iptabled rules
      iptables -D FORWARD -j wireless-adhoc
      iptables -D FORWARD -m state --state INVALID -j DROP
      iptables -F wireless-adhoc
      iptables -X wireless-adhoc
      iptables -t nat -F PREROUTING
      iptables -t nat -F POSTROUTING
      iptables -t nat -F
      wait_a_moment

      if [ -f $dnsmasqPid ]; then
        dnsmasqID=`cat $dnsmasqPid`
        kill $dnsmasqID
      fi
      if [ -f $dnsmasqLeases ]; then
        rm $dnsmasqLeases
      fi
      echo -e "\nWifi ad-hoc now stopped"
    }

    function adhoc_restart
    {
      echo "Now, resart Ad-hoc ..."
      adhoc_stop
      sleep 1
      adhoc_start
    }
    # check dnsmasq.conf
    function check_dnsmasq
    {

      if [ -f $dnsmasqPid ]; then
        echo "DHCP server is running!"
        echo "Now, restart Ad-hoc"
        adhoc_stop
      fi
      if [ ! -d $adhocFolder ]; then
        mkdir $adhocFolder
      fi

      if [ ! -f $dnsmasqFile ]; then
        echo "$dnsmasqFile is not exist, now building."

        echo "dhcp-authoritative" > $dnsmasqFile
        echo "dhcp-range=$DhcpRangeMin,$DhcpRangeMax,12h" >> $dnsmasqFile
        echo "dhcp-leasefile=$dnsmasqLeases" >> $dnsmasqFile
        echo "pid-file=$dnsmasqPid" >> $dnsmasqFile
        echo "user=root" >> $dnsmasqFile
        echo "no-negcache" >> $dnsmasqFile
      fi
    }

    # is super user?
    function super_user
    {
      if [ "$UID" = "0" ]; then
        return 0
      else
        return 1
      fi
    }

    # check whether encrypted key has supported length or not
    function check_key_size
    {
      local key=$1
      if [[ -n $key ]]; then
        local key_bit_sizes=$(iwlist ${WInterface} keys \
                            | grep "sizes" \
                            | sed "s/.*: *\(.*\)bits/\1/; s/,/ /")
        local key_byte_sizes
        local wrong_key=1
        local size
        for size in ${key_bit_sizes}; do
          key_byte_sizes="${key_byte_sizes},$((size/8))"
          if ((${#key} == ${size}/8)); then
            wrong_key=0
          fi
        done
        if ((${wrong_key})); then
          echo "Encrypted key must have ${key_byte_sizes#,} characters."
          exit 1
        fi
      fi
    }

    # check whether the specified wifi interface is valid or not
    function check_wifi_interface
    {
      local wifi="$1"
      local interfaces=$(iwconfig 2> /dev/null \
                              | grep "ESSID" \
                              | sed "s/^\([^ ]\{1,\}\).*/\1/")
      if [ -z "${interfaces}" ]; then
        echo "It seems that you haven't any WiFi device!"
        exit 1
      fi
      local inf
      for inf in ${interfaces}; do
        if [ "${wifi}" = "${inf}" ]; then
          return 0
        fi
      done
      echo "Maybe \"${wifi}\" is not a WiFi device which you have."
      echo "Detected WiFi devices following:"
      for inf in ${interfaces}; do
        echo -n "    ${inf}"
      done
      echo
      exit 1
    }

    function usage
    {
      local program=$(echo $0 | sed "s/.*\/\([^\/]*\)$/\1/")
      echo "             Wifi Ad-hoc Control"
      echo
      echo "Usage:"
      echo "    ${program} interface [essid X] [key K] (start|stop|restart)"
      echo
      echo "Description:"
      echo "    essid    The name of your network, default \"$Wessid\"."
      echo "    key      The encryption key of your network,"
      echo "             you can use any ASCII string,"
      echo "             if not set, your network will be non-encrypted."
      echo "    start    Start creating Wifi Ad-hoc Network."
      echo "    stop     Stop Wifi Ad-hoc Network."
      echo "    restart  Restart creating Wifi Ad-hoc Network."
    }

    if [ $# -lt 1 ]; then
      usage
      exit 1
    fi
    if ! super_user ; then
      echo "Need super user permission!"
      exit 1
    fi

    check_wifi_interface "$WInterface"

    shift
    while [[ -n $1 ]]; do
      case $1 in
        essid)  shift
                Wessid=$1
                ;;
        key)    shift
                WPasskey=$1
                check_key_size "${WPasskey}"
                ;;
        stop)   Action="stop"
                ;;
        restart) Action="restart"
                ;;
        start) Action="start"
                ;;
        *)      usage
                exit 1
                ;;
      esac
      shift
    done
    case $Action in
      stop)     adhoc_stop
                ;;
      restart)  adhoc_restart
                ;;
      start)    adhoc_start
                ;;
      *)        usage
    esac


    帖子永久地址: 

    黑帽联盟 - 论坛版权1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与黑帽联盟享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和黑帽联盟的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、黑帽联盟管理员和版主有权不事先通知发贴者而删除本文

    您需要登录后才可以回帖 登录 | 会员注册

    发布主题 !fastreply! 收藏帖子 返回列表 搜索
    回顶部