• Ubuntu系统下创建虚拟AP和STA扫描/关联/认证/ping


    首先 加载mac80211.ko, 参考: https://blog.csdn.net/fantasy_wxe/article/details/128057949

    然后执行如下操作,创建 AP&STA 并进行扫描、关联、认证、ping包操作

    编译hostapd

    wget https://w1.fi/releases/hostapd-2.10.tar.gz
    tar zxvf hostapd-2.10.tar.gz
    cd hostapd-2.10/hostapd/
    cp defconfig .config
    make
    
    • 1
    • 2
    • 3
    • 4
    • 5

    编译wpa_supplicant

    wget https://w1.fi/releases/wpa_supplicant-2.10.tar.gz
    tar zxvf wpa_supplicant-2.10.tar.gz
    cd wpa_supplicant-2.10/wpa_supplicant
    cp defconfig .config
    make
    
    • 1
    • 2
    • 3
    • 4
    • 5

    编译dhcp

    wget https://downloads.isc.org/isc/dhcp/4.4.3/dhcp-4.4.3.tar.gz
    cd dhcp-4.4.3
    ./configure
    make
    
    • 1
    • 2
    • 3
    • 4

    启动AP

    创建hostapd.conf配置文件,文件内容如下

    # hostapd.conf
    
    interface=wlan0
    driver=nl80211
    country_code=CN
    ssid=wifi_simu_test
    channel=6
    hw_mode=b
    wpa=3
    wpa_key_mgmt=WPA-PSK
    wpa_pairwise=TKIP CCMP
    wpa_passphrase=helloworld
    auth_algs=3
    beacon_int=100
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    创建dhcpd.conf配置文件,文件内容如下

    #dhcpd.conf
    subnet 192.168.100.0 netmask 255.255.255.0 {
      option routers 192.168.100.1 ;
      option domain-name-servers 192.168.100.1 ;
      range 192.168.100.80 192.168.100.150;
      range 192.168.100.180 192.168.100.220;
      default-lease-time 600 ;
      max-lease-time 7200 ;
      # we want this server to be the only DHCP server in this network
      authoritative ;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在AP侧Terminal中执行如下命令

    w512@w512-pc:~/wlan_test$ su root
    root@w512-pc:/home/w512/wlan_test# ip netns add wifi_ap
    root@w512-pc:/home/w512/wlan_test# ip netns list
    wifi_ap
    root@w512-pc:/home/w512/wlan_test# ip netns exec wifi_ap bash
    root@w512-pc:/home/w512/wlan_test# echo $BASHPID
    2280
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    注意: 2280在STA侧会用到

    新建一个Terminal, 作为STA侧

    su root
    
    iw phy phy0 set netns 2280
    
    • 1
    • 2
    • 3

    在AP侧Terminal中输入如下命令

    ip addr add 192.168.100.1/24 dev wlan0
    
    hostapd-2.10/hostapd/hostapd hostapd.conf &
    
    mkdir -p /var/db
    touch /var/db/dhcpd.leases
    
    dhcp-4.4.3/server/dhcpd -4 -f -d -cf dhcpd.conf -lf /var/db/dhcpd.leases wlan0&
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    通过ip a查看wlan0信息

    root@w512-pc:/home/w512/wlan_test# ip a
    1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 02:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
        inet 192.168.100.1/24 scope global wlan0
           valid_lft forever preferred_lft forever
        inet6 fe80::ff:fe00:0/64 scope link 
           valid_lft forever preferred_lft forever
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    STA侧启动

    创建wpa_supplicant.conf文件,文件内容如下:

    #wpa_supplicant.conf
    network={
      ssid="wifi_simu_test"
      key_mgmt=WPA-PSK
      psk="helloworld"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在STA侧Terminal中输入如下命令

    wpa_supplicant-2.10/wpa_supplicant/wpa_supplicant -B -i wlan1 -c wpa_supplicant.conf
    
    dhcp-4.4.3/client/dhclient -i wlan1
    
    • 1
    • 2
    • 3

    通过ip a查看wlan1信息

    root@w512-pc:/home/w512/wlan_test# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 08:00:27:9d:d4:e9 brd ff:ff:ff:ff:ff:ff
        inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic noprefixroute enp0s3
           valid_lft 84831sec preferred_lft 84831sec
        inet6 fe80::76da:36d3:6f9f:1ed7/64 scope link noprefixroute 
           valid_lft forever preferred_lft forever
    4: wlan1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 02:00:00:00:01:00 brd ff:ff:ff:ff:ff:ff
        inet 192.168.100.80/24 brd 192.168.100.255 scope global dynamic wlan1
           valid_lft 484sec preferred_lft 484sec
    5: hwsim0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
        link/ieee802.11/radiotap 12:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    可见, STA获取到了IP地址为192.168.100.80

    从AP侧pingSTA侧

    root@w512-pc:/home/w512/wlan_test# ping 192.168.100.80
    PING 192.168.100.80 (192.168.100.80) 56(84) bytes of data.
    64 bytes from 192.168.100.80: icmp_seq=1 ttl=64 time=0.052 ms
    64 bytes from 192.168.100.80: icmp_seq=2 ttl=64 time=0.076 ms
    64 bytes from 192.168.100.80: icmp_seq=3 ttl=64 time=0.084 ms
    64 bytes from 192.168.100.80: icmp_seq=4 ttl=64 time=0.086 ms
    64 bytes from 192.168.100.80: icmp_seq=5 ttl=64 time=0.067 ms
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    【UE5】 ListView使用DataTable数据的蓝图方法
    传播问卷调查数据不够?自己生成假数据!
    JVM面试总结
    与面试相关的redis
    zookeeper节点的状态和权限控制
    C++系列-关系运算符
    无病休学什么理由好
    开发者也能“搞艺术”?聚合数据联合科大讯飞助力开发者创新不断
    windows系统 flutter 开发环境配置
    软件性能测试学习笔记(LoadRunner):从零开始
  • 原文地址:https://blog.csdn.net/wuxiaoer717/article/details/128062077