MITM_Proxy環境搭建
環境要求
系統環境要求:
- Ubuntu 14.04 x64,CentOS 7 x64以上版本系統(建議使用xubuntu 14.04 x64,穩定硬件要求低)
- Python 2.7以上運行環境(MITM用Python寫的)
- Pip 7.1版本以上,這個用於安裝MITM Proxy
官方安裝指南:http://mitmproxy.org/doc/install.html
下面以xubunut x64為例進行環境搭建
- 首先安裝一個純凈版的xubuntu 14.04 x64系統,手動安裝分區方法見下面鏈接: http://blog.chinaunix.net/uid-7547035-id-60111.html
- 設置好升級xubuntu升級服務器選擇,建議選擇aliyun節點或者香港中文大學節點,速度快,穩定
- 設置完畢后會提示授權以及是否立即進行更新,選擇是
然后打開終端模擬器(terminial),准備開始更新系統列表和開始配置網卡,運行命令如下:
sudo apt-get update #更新遠程軟件倉庫列表
sudo apt-get install vim git openssh-server openssh-client #安裝工具vim,git,openssh-server,openssh-client
git clone https://github.com/wuxinwei/MyConfig.git #從github上clone下來我的配置信息,里面有vimrc,tmux,zsh的配置文件,我已經全部配好了,可以覆蓋到/home/用戶名/就可以
cp .../MyConfig/_vimrc ~/.vimrc #使用vim配置文件
sudo /etc/init.d/ssh start #啟動ssh服務,ssh-server配置文件位於/ etc/ssh/sshd_config,在這里可以定義SSH的服務端口,默認端口是22,你可以自己定義成其他端口號,如222
sudo apt-get install bridge-utils #安裝橋工具bridge-utils
brctl addbr br0 #添加一個網橋
brctl addif br0 eth0 #將eth0加到網橋中去
brctl addif br0 eth1 #將eth1加到網橋中去
開啟IP轉發功能以及其他配置sudo vim /etc/sysctl.conf
:
# 將全部內容改為如下:
#
# /etc/sysctl.conf - Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables.
# See sysctl.conf (5) for information.
#
#kernel.domainname = example.com
# Uncomment the following to stop low-level messages on console
#kernel.printk = 3 4 1 3
##############################################################3
# Functions previously found in netbase
#
# Uncomment the next two lines to enable Spoof protection (reverse-path filter)
# Turn on Source Address Verification in all interfaces to
# prevent some spoofing attacks
#net.ipv4.conf.default.rp_filter=1
#net.ipv4.conf.all.rp_filter=1
# Uncomment the next line to enable TCP/IP SYN cookies
# See http://lwn.net/Articles/277146/
# Note: This may impact IPv6 TCP sessions too
#net.ipv4.tcp_syncookies=1
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1
# Uncomment the next line to enable packet forwarding for IPv6
# Enabling this option disables Stateless Address Autoconfiguration
# based on Router Advertisements for this host
net.ipv6.conf.all.forwarding=1
###################################################################
# Additional settings - these settings can improve the network
# security of the host and prevent against some network attacks
# including spoofing attacks and man in the middle attacks through
# redirection. Some network environments, however, require that these
# settings are disabled so review and enable them as needed.
#
# Do not accept ICMP redirects (prevent MITM attacks)
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
# _or_
# Accept ICMP redirects only for gateways listed in our default
# gateway list (enabled by default)
# net.ipv4.conf.all.secure_redirects = 1
net.ipv4.conf.all.secure_redirects = 0
#
# Do not send ICMP redirects (we are not a router)
#net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.send_redirects = 0
#
# Do not accept IP source route packets (we are not a router)
#net.ipv4.conf.all.accept_source_route = 0
#net.ipv6.conf.all.accept_source_route = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
#
# Log Martian Packets
#net.ipv4.conf.all.log_martians = 1
#
配置網橋sudo vim /etc/network/interfaces//以管理員權限打開網絡配置文件
:
# 把全部文件內容改為如下:
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto eth0 #網卡eth0
iface eth0 inet manual #設置eth0網卡為手動配置,這樣不會自動獲取ip配置等
auto eth1 #網卡eth1
iface eth1 inet manual #設置eth0網卡為手動配置,這樣不會自動獲取ip配置等
auto br0 #網橋0
iface br0 inet static #設置網橋br0為靜態,這樣不會變化ip
address 192.168.4.144 #這里設置網橋IP
netmask 255.255.255.0 #設置網橋子網掩碼
broadcast 192.168.4.255 #設置網橋廣播地址
gateway 192.168.4.2 #這是網橋網關
dns-nameservers 202.96.128.86 #設置網橋DNS
bridge_ports eth0 eth1 //將eth0 和eth1網卡添加如網橋中去
bridge_stp off
brideg_hello 2
bridge_fd 9
bridge_maxwait 0
設置防火牆(注意,這里的iptables
設置在重啟系統后會失效,所以如果重啟過機器這里需要重新設置):
echo 0 | sudo tee /proc/sys/net/ipv4/conf/*/send_redirects
*/
service iptables start #開啟iptables過濾服務
service iptables save
iptables -t nat -A PREROUTING -i br0 -p tcp --dport 80 -j REDIRECT --to-port 8080 #將80端口轉發給8080
iptables -t nat -A PREROUTING -i br0 -p tcp --dport 443 -j REDIRECT --to-port 8080 #將443端口數據轉發給8080
service iptables save
官方透明代理設置教程:
http://mitmproxy.org/doc/transparent/linux.html(物理機設置)
http://mitmproxy.org/doc/tutorials/transparent-dhcp.html(虛擬機設置)
經過以上配置,實現了建立網橋,打開路由轉發功能(用於MITM的透明代理)
安裝MITM Proxy運行環境以及MITM Proxy
sudo apt-get install python-pip python-dev libffi-dev libssl-dev libxml2-dev libxslt1-dev #安裝必要的運行環境
sudo pip install mitmproxy #安裝mitmproxy,安裝成功后會在生成兩個工具/usr/local/bin/mitmproxy與/usr/local/bin/mitmdump
到這里為mitmproxy的環境安裝完畢,接下來進行目標機方面的配置
- CA證書的安裝
要捕獲https證書,就得解決證書認證的問題,因此需要在通信發生的客戶端安裝證書,並且設置為受信任的根證書頒布機構。下面介紹6種客戶端的安裝方法。
當我們初次運行mitmproxy或mitmdump時,
會在當前目錄下生成 ~/.mitmproxy文件夾,其中該文件下包含4個文件,這就是我們要的證書了。
mitmproxy-ca.pem 私鑰
mitmproxy-ca-cert.pem 非windows平台使用
mitmproxy-ca-cert.p12 windows上使用
mitmproxy-ca-cert.cer 與mitmproxy-ca-cert.pem相同,android上使用
1. Firefox上安裝
preferences-Advanced-Encryption-View Certificates-Import (mitmproxy-ca-cert.pem)-trust this CA to identify web sites
2. chrome上安裝
設置-高級設置-HTTPS/SSL-管理證書-受信任的根證書頒發機構-導入mitmproxy-ca-cert.pem
2. osx上安裝
雙擊mitmproxy-ca-cert.pem - always trust
3.windows7上安裝
雙擊mitmproxy-ca-cert.p12-next-next-將所有的證書放入下列存儲-受信任的根證書發布機構
4.iOS上安裝
將mitmproxy-ca-cert.pem發送到iphone郵箱里,通過瀏覽器訪問/郵件附件
6.Android上安裝
將mitmproxy-ca-cert.cer 放到sdcard根目錄下
選擇設置-安全和隱私-從存儲設備安裝證書
一些額外的資料:
>* 官方教程: http://mitmproxy.org/doc/index.html
>* win7、linux安裝使用pip、mitmproxy
>* 推薦給開發人員的6個實用命令行工具
>* 使用mitmproxy進行Android的http抓包
>* mitmproxy 入門案例 -『抱抱』24小時銷毀的真相(iOS端)
>* mitmproxy實踐教程之調試 Android 上 HTTP流量