1. 程式人生 > >win7、linux安裝使用pip、mitmproxy

win7、linux安裝使用pip、mitmproxy

安裝pip https://pip.pypa.io/en/latest/installing.html

步驟:

下載 https://bootstrap.pypa.io/get-pip.py

python get-pip.py
D:\pip>python get-pip.py
Collecting pip
  Downloading pip-6.0.6-py2.py3-none-any.whl (1.3MB)
    100% |################################| 1.3MB 74kB/s ta 0:00:011
Collecting setuptools
  Downloading setuptools
-12.0.4-py2.py3-none-any.whl (502kB) 100% |################################| 503kB 117kB/s ta 0:00:01 Installing collected packages: setuptools, pip Successfully installed pip-6.0.6 setuptools-12.0.4
python -m pip
Usage:
  D:\Python27\python.exe -m pip <command> [options]

Commands:
  
install Install packages. uninstall Uninstall packages. freeze Output installed packages in requirements format. list List installed packages. show Show information about installed packages. search Search PyPI
for packages. wheel Build wheels from your requirements. zip DEPRECATED. Zip individual packages. unzip DEPRECATED. Unzip individual packages. help Show help for commands. General Options: -h, --help Show help. --isolated Run pip in an isolated mode, ignoring environment variables and user configuration. -v, --verbose Give more output. Option is additive, and can be used up to 3 times. -V, --version Show version and exit. -q, --quiet Give less output. --log <path> Path to a verbose appending log. --proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port. --retries <retries> Maximum number of retries each connection should attempt (default 5 times). --timeout <sec> Set the socket timeout (default 15 seconds). --exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup. --trusted-host <hostname> Mark this host as trusted, even though it does not have valid or any HTTPS. --cert <path> Path to alternate CA bundle. --client-cert <path> Path to SSL client certificate, a single file containing the private key and the certificate in PEM format. --cache-dir <dir> Store the cache data in <dir>. --no-cache-dir Disable the cache. --disable-pip-version-check Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index.

安裝依賴包

python -m pip install netlib pyopenssl pyasn1 urwid pil lxml flask

錯誤:

File "D:\Python27\lib\mimetypes.py", line 249, in enum_types
ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 1: ordinal not in range(128)

解決方案:

開啟D:\Python27\lib\mimetypes.py檔案,在256行,將

default_encoding = sys.getdefaultencoding()

改為

        if sys.getdefaultencoding() != 'gbk':  
            reload(sys)  
            sys.setdefaultencoding('gbk')  
        default_encoding = sys.getdefaultencoding()

 由於pil安裝出錯,所以先:

python -m pip install netlib pyopenssl pyasn1 urwid lxml flask

然後:

python -m pip install pyamf protobuf

又出錯:

error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat).
Get it from http://aka.ms/vcpython27

需要裝 Microsoft Visual C++ Compiler for Python 2.7(共 83.8 MB)

http://www.microsoft.com/en-us/download/confirmation.aspx?id=44266

再試:

python -m pip install pyamf protobuf

下一步:

python -m pip install pil --allow-external PIL --allow-unverified PIL

下一步:

python -m pip install nose pathod countershape

最後:

python -m pip install mitmproxy

參考資料:

https://pip.pypa.io/en/latest/installing.html
http://m.blog.csdn.net/blog/roler_/40398789
http://netsecurity.51cto.com/art/201311/416548.htm
http://www.ajucs.com/archives/565.html

——————————————————————————————————

Linux:

類似windows就是pip命令不用加前面的python -m ,如果不是管理員需要sudo。

在pip install netlib pyopenssl pyasn1 urwid lxml flask的時候遇到的錯誤:

src/lxml/lxml.etree.c:8:22: 錯誤:pyconfig.h:沒有那個檔案或目錄

yum install python-devel

c/_cffi_backend.c:13:17: 錯誤:ffi.h:沒有那個檔案或目錄

yum install libffi-devel

其他的還有:

yum install gcc
yum install libxml2
yum install libxslt-devel libxml2-devel

然後:

pip install pil --allow-external PIL --allow-unverified PIL
pip install pyamf protobuf
pip install nose pathod countershape
pip install mitmproxy

“pip install mitmproxy”遇到錯誤:

[[email protected] ~]# pip install mitmproxy
Collecting mitmproxy
Using cached mitmproxy-0.11.3.tar.gz
Traceback (most recent call last):
File "<string>", line 20, in <module
Complete output from command python setup.py egg_info:
Traceback (most recent call last):

File "<string>", line 20, in <module

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-QBFasU/mitmproxy

查網上說mitmproxy需要python2.7而CentOS6.4預設是python2.6,所以一下部分是CentOS6.4安裝python2.7與python2.6共存的辦法:

安裝必備的開發工具,據說都需要,以防萬一,我都安裝了:

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

下載和安裝Python,注意命令“make altinstall”據說是防止/usr/bin/下面有多個版本的python

cd ~
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar zxvf Python-2.7.9.tgz 
cd Python-2.7.9
./configure --prefix=/usr/local
make && make altinstall

如果需要更改系統python版本

mv /usr/bin/python /usr/bin/python2.6.6.old
ln -s /usr/local/bin/python27 /usr/bin/python
vi /usr/bin/yum

#!/usr/bin/python改為#!/usr/bin/python2.6,因為yum需要python2.6

安裝pip

wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py
python2.7 ez_setup.py
easy_install-2.7 pip

注意後面使用pip2.7而不是pip。

提示

-bash: /usr/bin/pip2.7: 沒有那個檔案或目錄

的話退出重新登入就好了。安裝mitmproxy

pip2.7 install netlib pyopenssl pyasn1 urwid lxml flask
pip2.7 install pil --allow-external PIL --allow-unverified PIL
pip2.7 install pyamf protobuf
pip2.7 install nose pathod countershape
pip2.7 install mitmproxy

參考資料:

http://web.49jie.com/?p=1644
http://segmentfault.com/blog/digwtx/1190000000654227
http://stackoverflow.com/questions/27144574/mitmproxy-installation-error

————————————————————————————

使用:

順便裝下圖形介面

yum groupinstall "Desktop"
yum groupinstall "X Window System"
yum groupinstall "Chinese Support"
startx

http代理

mitmproxy

快捷鍵

q - 返回上一層
enter - 檢視內容
Tab - 切換看request/response
pageup - 上翻頁
pagedown - 下翻頁
C - 清空(注意大寫)

透明代理

http://mitmproxy.org/doc/transparent/linux.html

sysctl -w net.ipv4.ip_forward=1
echo 0 | sudo tee /proc/sys/net/ipv4/conf/*/send_redirects
service iptables start
service iptables save
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8080
service iptables save
cat /etc/sysconfig/iptables
mitmproxy -T --host

大概的意思就是開啟轉發,設定路由規則,開啟mitmproxy的透明代理模式。

iptables如果是直接編輯檔案:

-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT 

-A PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
-A PREROUTING -i eth0 -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 8080

 使用中還有這麼個錯誤

File "/usr/local/lib/python2.7/threading.py", line 810, in __bootstrap_inner
File "/usr/local/lib/python2.7/site-packages/libmproxy/controller.py", line 77, in run
File "/usr/local/lib/python2.7/site-packages/netlib/tcp.py", line 477, in serve_forever
File "/usr/local/lib/python2.7/socket.py", line 202, in accept
error: [Errno 24] 開啟的檔案過多

ulimit -n 65535

不過是臨時修改

安裝證書 

mitmproxy-ca.pem PEM格式的私鑰和證書。
mitmproxy-CA-cert.pem PEM格式的證書。使用此分發大多數非Windows平臺。
mitmproxy-CA-cert.p12在PKCS12格式的證書。在Windows上使用。
mitmproxy-CA-cert.cer相同的檔案質子交換膜,但預計一些Android裝置的擴充套件。