轉載
https://bg6cq.github.io/ITTS/security/bgp/exabgp/
一、BGP介紹
BGP是路由協議,通過BGP協議可以方便的控制路由器上的路由表,運維中最常用是用於引流或黑洞路由。
把某個IP的next-hop設置為特定的IP,稱為引流。用於黑洞路由時, 通常是把IP的next-hop設置為192.0.2.1,並通過ip route 192.0.2.1/32 null0
丟包。
二、ExaBGP簡介
ExaBGP 是一個python開發的BGP客戶端,被稱為BGP的瑞士軍刀, 可以與路由器建立BGP連接,發送和接收BGP更新消息。
其他程序可以方便的利用簡單的文本協議與ExaBGP通信,發送和接收BGP更新消息。
本文給出一個簡單例子,使用簡單文本發送BGP更新消息。
需要說明的是,exabgp 4.0版本的配置文件有較大變化,本文使用的是3.4版本。
三、ExaBGP安裝
不少系統發行版有ExaBGP,可以直接安裝。
也可以從github下載,方法是:
cd /usr/src/
git clone https://github.com/Exa-Networks/exabgp.git
cd exabgp
git checkout 3.4
./bin/healthcheck --help
healthcheck能運行說明程序安裝正常。對於CentOS系統,可能需要安裝以下軟件包:
yum install python-argparse python-ipaddr socat
四、ExaBGP配置文件
假定本機IP是210.45.230.89,ASN 65500;路由器是210.45.230.90,ASN 24362
vi /etc/exabgp.conf
neighbor 210.45.230.90 {
local-address 210.45.230.89;
peer-as 24362;
local-as 65500;
router-id 210.45.230.89;
process service-dynamic {
run /usr/bin/socat stdout pipe:/var/run/exabgp.cmd;
}
}
五、路由器配置片段
為了減少風險,僅僅接受ExaBGP發來的/32路由,並且最多接受10條路由。以下為華為/H3C配置:
ip ip-prefix only32 index 10 permit 0.0.0.0 0 greater-equal 32 less-equal 32
router bgp 24362
peer 210.45.230.89 as-number 65500
ipv4-family unicast
peer 210.45.230.89 enable
peer 210.45.230.89 ip-prefix only32 import
peer 210.45.230.89 route-limit 10
ip route-static 192.0.2.1 32 null0
六、ExaBGP啟動
/var/run/exabgp.cmd是其他程序與ExaBGP通信的管道文件。
如果是調試,可以使用以下命令啟動
env exabgp.daemon.user=root /usr/src/exabgp/sbin/exabgp /etc/exabgp.conf
啟動后,程序的輸出在屏幕上,從路由器上看到BGP連接建立說明正常。
調試完畢,可以使用如下命令啟動:
env exabgp.daemon.user=root exabgp.daemon.daemonize=true exabgp.daemon.pid=/var/run/exabgp.pid \
exabgp.log.destination=/var/log/exabgp.log /usr/src/exabgp/sbin/exabgp /etc/exabgp.conf
/var/log/exabgp.log記錄有日志輸出。
七、增加刪除路由
向/var/run/exabgp.cmd 管道寫文本就可以控制ExaBGP發送BGP消息,簡單的例子如下:
發送BGP路由:
echo announce route 202.38.95.255/32 next-hop 192.0.2.1 > /var/run/exabgp.cmd
echo announce route 202.38.95.0/32 next-hop 210.45.230.89 > /var/run/exabgp.cmd
撤回BGP路由:
echo withdraw route 202.38.95.255/32 > /var/run/exabgp.cmd
執行以上命令后,ExaBGP有信息輸出,登錄路由器執行
display bgp routing-table pee 210.45.230.89 received-routes
可以看到發來的路由信息。
八、一個簡單的WEB界面
請參考 https://github.com/bg6cq/blackip-exabgp
使用mysql數據庫存放信息,blackip-exabgp.php程序不停的輪詢數據庫,如果有新的路由需要發送或舊的路由需要撤回,會與 ExaBGP 通信。
九、使用ExaBGP發送路由處理DDoS攻擊
我們使用ExaBGP給上游路由器發送受DDoS攻擊的IP,把DDoS流量引流到Linux服務器,Linux服務器清洗流量后注入網絡。
請參考 http://blackholeah.ustc.edu.cn/admin/intro.php 。