OpenWrt on VirtualBox
環境配置
+在windows上配置
- 下載
wget.exe 並將其放到git 的bin 目錄下; - 下載
dd 工具包。
腳本執行
環境:windows10 + git bash + virtualBox
腳本分析
注意:在配置好上述環境的情況下,可在windows環境中一鍵安裝openwrt
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 | #!/usr/bin/env bash VER="19.07.5" # openwrt version VDI_BASE="openwrt-x86-64-combined-squashfs.vdi" shasum -c img.sha256.sum #kate: # -c 從檔案中讀取SHA1 的校驗值並予以檢查 # -q don't print OK for each successfully verified file (kali里有該引數,ubuntu16.4里無改引數) # 在ubuntu里,如果加上-q,則每次該命令會執行出錯,從而無論本地是否已經下載過下面的對映,都會重新下載。 # 標準輸出重新導向到 /dev/null , 2表示標準錯誤輸出,重新導向到 &1 ,1表示標準輸出。即:標準錯誤輸出也重新導向到 /dev/null if [[ $? -ne 0 ]];then # kate: # $? 最後執行的命令執行程式碼的回傳值。即shasum的回傳值。 # -ne : 不等於 # shasum執行成功回傳0 # 下載韌體 wget https://downloads.openwrt.org/releases/$VER/targets/x86/64/openwrt-$VER-x86-64-combined-squashfs.img.gz -O openwrt-x86-64-combined-squashfs.img.gz # kate: # wget : 非互動式的網路檔案下載工具 # -O : 將檔案寫入 FILE/重命名 # 若官方網站連結發生變化,可以在google搜尋檔名。 # 解壓縮 gzip -d openwrt-x86-64-combined-squashfs.img.gz # kate: # gzip : Compress or uncompress FILEs fi shasum -c vdi.sha256.sum if [[ $? -ne 0 ]];then # img 格式轉換為 Virtualbox 虛擬硬碟格式 vdi VBoxManage convertfromraw --format VDI openwrt-x86-64-combined-squashfs.img "$VDI_BASE" # 新建虛擬機選擇「型別」 Linux / 「版本」Linux 2.6 / 3.x / 4.x (64-bit),填寫有意義的虛擬機「名稱」 # 記憶體設定為 256 MB # 使用已有的虛擬硬碟檔案 - 「註冊」新虛擬硬碟檔案選擇剛才轉換生成的 .vdi 檔案 if [[ $? -eq 1 ]];then # 上述程式碼執行失敗,則執行下述程式碼:將源img對映拷貝一份,並給其一個新的名字。 # ref: https://openwrt.org/docs/guide-user/virtualization/virtualbox-vm#convert_openwrtimg_to_vbox_drive dd if=openwrt-x86-64-combined-squashfs.img of=openwrt-x86-64-combined-squashfs-padded.img bs=128000 conv=sync # dd :用指定大小的塊拷貝一個檔案,並在拷貝的同時進行指定的轉換 # if=檔名:輸入檔名 # of=檔名:輸出檔名 # bs=bytes:同時設定讀入/輸出的塊大小為bytes個位元組。 # conv=conversion:用指定的引數轉換檔案。 # sync:將每個輸入塊填充到ibs個位元組,不足部分用空(NUL)字元補齊。 VBoxManage convertfromraw --format VDI openwrt-x86-64-combined-squashfs-padded.img "$VDI_BASE" # This command converts a raw disk image to an Oracle VM VirtualBox Disk Image (VDI) file. fi fi # 建立虛擬機 VM="openwrt-demo" # VBoxManage list ostypes if [[ $(VBoxManage list vms | cut -d ' ' -f 1 | grep -w ""$VM"" -c) -eq 0 ]];then # kate : # VBoxManage list vms : 列出所有的虛擬機 # cut : Print selected parts of lines from each FILE to standard output. # -d : 指定分隔符 # -f : 選取分割後的第一個 # grep : 在每個 FILE 或是標準輸入中搜尋 PATTERN。預設的 PATTERN 是一個基本正規表示式(縮寫為 BRE) # -w : 強制匹配 # -c : 只列印匹配的行數 # 本行含義:如果版本為「openwrt-19.07.5」的虛擬機的個數為0,則繼續往下執行: echo "vm $VM not exsits, create it ..." VBoxManage createvm --name $VM --ostype "Linux26_64" --register # VBoxManage createvm --name $VM --ostype "Linux26_64" --register --groups "/IoT" # VBoxManage createvm : 建立並註冊一個虛擬機。 # --name : 指定虛擬機的名稱為:openwrt-19.07.5 # --ostype : 指定虛擬機的系統型別為:Linux26_64 # 建立一個 SATA 控制器 VBoxManage storagectl "$VM" --name "SATA" --add sata --controller IntelAHCI # 向該控制器安裝一個「硬碟」 ## --medium 指定本地的一個「多重載入」虛擬硬碟檔案 VBoxManage storageattach "$VM" --storagectl "SATA" --port 0 --device 0 --type hdd --medium "$VDI_BASE" VBoxManage storagectl "$VM" --name "SATA" --remove # 將目標 vdi 修改為「多重載入」 VBoxManage modifymedium disk --type multiattach "$VDI_BASE" # 虛擬磁碟擴容 VBoxManage modifymedium disk --resize 10240 "$VDI_BASE" VBoxManage storagectl "$VM" --name "SATA" --add sata --controller IntelAHCI VBoxManage storageattach "$VM" --storagectl "SATA" --port 0 --device 0 --type hdd --medium "$VDI_BASE" # 啟用 USB 3.0 介面 VBoxManage modifyvm "$VM" --usbxhci on # 修改虛擬機配置 ## --memory 記憶體設定為 256MB ## --vram 顯存設定為 16MB VBoxManage modifyvm "$VM" --memory 256 --vram 16 # ref: https://docs.oracle.com/en/virtualization/virtualbox/6.1/user/settings-display.html # VMSVGA: Use this graphics controller to emulate a VMware SVGA graphics device. This is the default graphics controller for Linux guests. VBoxManage modifyvm "$VM" --graphicscontroller vmsvga # CAUTION: 虛擬機的 WAN 網卡對應的虛擬網路型別必須設定為 NAT 而不能使用 NatNetwork ,無線客戶端連入無線網路後才可以正常上網 ## 檢查 NATNetwork 網路是否存在 # natnetwork_name="NatNetwork" # natnetwork_count=$(VBoxManage natnetwork list | grep -c "$natnetwork_name") # if [[ $natnetwork_count -eq 0 ]];then # VBoxManage natnetwork add --netname "$natnetwork_name" --network "10.0.2.0/24" --enable --dhcp on # fi ## 新增 Host-Only 網卡為第 1 塊虛擬網卡 ## --hostonlyadapter1 第 1 塊網卡的介面名稱為 vboxnet0 ## --nictype1 第 1 塊網卡的控制晶片為 Intel PRO/1000 MT 桌面 (82540EM) ## --nic2 nat 第 2 塊網卡配置為 NAT 模式 VBoxManage modifyvm "$VM" --nic1 "hostonly" --nictype1 "82540EM" --hostonlyadapter1 "vboxnet0" VBoxManage modifyvm "$VM" --nic2 nat fi |
也可以手動安裝,用到部分上述命令,可在上面貼上。
手動安裝openwrt
-
在
https://downloads.openwrt.org/releases/19.07.5/targets/x86/64/ 中下載openwrt-19.07.5-x86-64-combined-squashfs.img.gz 。
-
使用
gzip -d 在git bash 中解壓縮剛下載的檔案。
- 使用下述命令將
img 檔案轉換成vdi 檔案。
- 磁碟擴容
-
在
virtualBox 中新建虛擬機,並選擇[使用已有虛擬硬碟],並註冊剛才生成的vdi 。得到新的虛擬機openwrt-demo 。?
-
進行一些配置,如:設定多重載入、NAT+Host only雙網卡、修改記憶體大小、顯存大小、USB裝置配置等。
無線網卡配置(補充)
寫入USB無線網卡,檢測下述命令:
usb 連接埠設定:
注意:重裝virtualbox 需要安裝 extension pack,才能更改上面的USB控制器。
- 可以採用下面的三條指令檢測虛擬機是否可以正常的識別網卡。
- 可以使用
sudo airodump-ng wlan0 -c 11 -w demo-20210324 --beacons : 抓包。
OpenWrt 配置
-
網路配置:透過
vi 直接編輯/etc/config/network 設定檔來設定好遠端管理專用網卡的 IP 地址。修改OpenWrt 區域網路地址為當前 Host-only 網段內可用地址,只需要修改option ipaddr 的值即可。1
2
3
4
5
6
7config interface 'lan'
option type 'bridge'
option ifname 'eth0'
option proto 'static'
option ipaddr '192.168.56.11'
option netmask '255.255.255.0'
option ip6assign '60'然後可以透過重啟系統使之重新載入配置。
-
安裝
LuCi 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# 更新 opkg 本地快取
opkg update
# 搜尋指定軟體包
opkg find luci
# luci - git-19.223.33685-f929298-1
# 檢視 luci 依賴的軟體包有哪些
opkg depends luci
# luci depends on:
# libc
# uhttpd
# uhttpd-mod-ubus
# luci-mod-admin-full
# luci-theme-bootstrap
# luci-app-firewall
# luci-proto-ppp
# libiwinfo-lua
# luci-proto-ipv6
# 檢視系統中已安裝軟體包
opkg list-installed
# 安裝 luci
opkg install luci
# 檢視 luci-mod-admin-full 在系統上釋放的檔案有哪些
opkg files luci-mod-admin-full
# Package luci-mod-admin-full (git-16.018.33482-3201903-1) is installed on root and has the following files:
# /usr/lib/lua/luci/view/admin_network/wifi_status.htm
# /usr/lib/lua/luci/view/admin_system/packages.htm
# /usr/lib/lua/luci/model/cbi/admin_status/processes.lua
# /www/luci-static/resources/wireless.svg
# /usr/lib/lua/luci/model/cbi/admin_system/system.
# ...
# /usr/lib/lua/luci/view/admin_network/iface_status.htm
# /usr/lib/lua/luci/view/admin_uci/revert.htm
# /usr/lib/lua/luci/model/cbi/admin_network/proto_ahcp.lua
# /usr/lib/lua/luci/view/admin_uci/changelog.htm -
安裝完
LuCi 後瀏覽器訪問的結果
-
檢測網卡驅動
1
2
3
4
5
6
7
8root@OpenWrt:~# lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 0bda:8178 Realtek Semiconductor Corp. RTL8192CU 802.11n WLAN Adapter
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
root@OpenWrt:~# lsusb -t
/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/6p, 5000M
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/8p, 480M
|__ Port 1: Dev 2, If 0, Class=Vendor Specific Class, Driver=, 480M發現
RTL8192CU 無線網卡無驅動。 -
安裝驅動
1
2
3
4
5
6
7
8
9
10
11#快速搜尋可能包含指定晶片名稱的驅動程式包
root@OpenWrt:~# opkg find kmod-* | grep rtl8192cu
kmod-rtl8192cu - 4.14.209+4.19.137-1-2 - Realtek RTL8192CU/RTL8188CU support
#安裝上述搜尋出的驅動
opkg install kmod-rtl8192cu
#安裝完成後檢查:
root@OpenWrt:~# lsusb -t
/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/6p, 5000M
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/8p, 480M
|__ Port 1: Dev 2, If 0, Class=Vendor Specific Class, Driver=rtl8192cu, 480M
root@OpenWrt:~# -
安裝
wpa-supplicant 和hostapd 。1
2#wpa-supplicant 和 hostapd 。其中 wpa-supplicant 提供 WPA 客戶端認證,hostapd 提供 AP 或 ad-hoc 模式的 WPA 認證。
opkg install hostapd wpa-supplicant -
重啟系統,使得上述安裝的配置生效。以便能夠在
LuCi 的網頁版中管理無線網卡設定。能在網頁版的Network 下拉式選單中看見Wireless 為上述操作成功的標識。 -
進行下述的配置
記得點選Enabble 啟動、儲存配置。就可以用行動電話連接該熱點啦~
參考資料
黃大課本