使用Python查询纯真IP(最新修改,无乱码)

纯真IP挺全,IP数据库也是中文的,最近由于把一个服务器放到公网上,老是有一堆人用ssh暴力破解,用 DenyHosts 禁止暴力破解之余,还想看看到底这些 IP 是 哪里的(不过基本上都是代理 ^_^),就找了一个纯真IP的查询脚本。

纯真IP的格式详解

在网上找到一个 Python 脚本,修改后在linux下的 UTF-8 环境正常使用了,添加一些注释,使用的时候下载最新的纯真IP库,解压后把 QQWry.Dat 文件放到下面脚本同一个目录下。使用”./脚本名字.py IP地址"就可以查询了:

感谢 Xu Xiaodong 的帮助,脚本放在这里。如果有什么我能帮到的问题,可以email我。

Python 查询纯真IP库

我在 DenyHosts 禁止的IP查询示例:

for i in `sed -e '/^ALL/!d' /etc/hosts.deny |cut -d' ' -f2`; do /root/查询纯真IP.py $i;done
122.224.7.146 浙江省绍兴市/电信
89.163.144.165 俄罗斯/
211.174.187.161 韩国/首尔
59.166.120.184 日本/ATHOME网络
147.231.70.91 捷克/
85.28.26.66 英国/伦敦大学皇家医学院皇家法医实验鉴定中心
218.239.223.77 韩国/
216.146.47.37 美国/加拿大/未知
122.155.0.62 泰国/ CZ88.NET
69.31.5.120 美国/
75.109.170.56 美国/
69.43.142.150 美国/
59.109.6.83 北京市/方正宽带
125.46.248.158 河南省郑州市/网通
201.17.35.128 巴西/圣保罗
212.175.84.152 土耳其/
219.134.242.67 广东省深圳市/电信ADSL
213.228.142.42 葡萄牙/
85.17.182.198 荷兰/阿姆斯特丹Leaseweb IDC
218.77.129.6 海南省/
61.151.248.152 上海市/电信
210.210.18.218 印度/ CZ88.NET
194.171.247.21 荷兰/
59.57.251.57 福建省厦门市/电信
218.56.61.114 山东省济南市/网通
220.130.208.19 台湾省台北市/中华电信数据通信分公司
201.63.218.70 美国/中部/南部(IANA)
118.102.25.161 北京市/光环恒通数字技术

Read More:

33 Comments

  1. 1 bones7456 Commented @ 2009-05-27 10:54Reply to this comment

    我这有个球猫写的python版...

  2. 2 Terry Wang Commented @ 2009-05-27 11:10Reply to this comment

    2004年还在学校的时候,参考了Luma大神的Lumaqq代码,用Swing写了一个类似纯真showip的东西。到现在还能跑,不过没什么用;-)

    这玩意儿,还是用web的方便。 http://www.cz88.net/ http://ip.cn

  3. 3 jianlee Commented @ 2009-05-27 11:23Reply to this comment

    其实主要的作用在于,分析一下QQWry.Dat的结构,方便以后在其他应用中集成,要知道现在日志分析的工作少不了IP地址查询,了解结构就可以用其他任何自己擅长的语言编程。再者练习Python.

  4. 4 jianlee Commented @ 2009-05-27 11:25Reply to this comment

    @Terry Wang:

    应用中很少在web里面查询

  5. 5 Terry Wang Commented @ 2009-05-27 11:47Reply to this comment

    @jianlee: 我说的是如果自己要查,并非说应用。

    Wordpress插件ip2nation,类似的功能。用的是一个.sql的数据存储结构,估计来源和纯真的不同吧?

  6. 6 GunsNRose Commented @ 2009-05-27 11:55Reply to this comment

    很棒,收藏起来 二楼推荐的网址也很方便

  7. 7 proguru Commented @ 2009-05-27 12:12Reply to this comment

    ssh改个端口号,用2048位的RSA自动登录,让他们爆破去吧!

  8. 8 netawater Commented @ 2009-05-27 12:55Reply to this comment

    可以禁止ssh密码登录吧?

  9. 9 proguru Commented @ 2009-05-27 13:53Reply to this comment

    禁止密码验证,禁止root远程登录

  10. 10 netsnail Commented @ 2009-05-27 14:39Reply to this comment

    代码有bug if byte != 1 and byte != 2: # 最简单的IP记录形式,[IP][国家信息][地区信息] # 重定向模式1有种情况就是偏移量指向包含国家和地区信息两个字符串 # 即偏移量指向的第一个字节不是1或2,就使用这里的分支 # 简单地说:取连续取两个字符串! cArea = self.getString(o) o += len(cArea) + 1 aArea = self.getString(o) return (cArea, aArea)

    这里,不能用len(cArea),原来是用GB2312编码的,转换成utf8后会变长,这样后面的offset会偏后. 另外,字符串0结尾貌似网页里贴不出来.

  11. 11 tlze Commented @ 2009-05-27 15:01Reply to this comment

    def getLong3(self, offset = 0): '''QQWay.dat中的偏移记录都是3字节,本函数取得3字节的偏移量的常规表示 QQWay.dat使用“字符串“存储这些值''' s = self.img[offset: offset + 3] s += '' # unpack用一个'I'作为format,后面的字符串必须是4字节 return unpack('I', s)[0]

    其中 s+=''这里应该有问题吧!不知道是笔误还网页过虑了,作者还是提供一下文件下载吧!

  12. 12 CyrusYzGTt Commented @ 2009-05-27 15:37Reply to this comment

    可不可以,用在linuxqq

  13. 13 jianlee Commented @ 2009-05-27 16:46Reply to this comment

    @netsnail:

    呵呵,我使用结果很好,有些没有“信息“的部分也返回”未知“。这个脚本每次运行只查询一个ip。不会出现你说的问题。

    
            #return self.img[offset:o2]
            # 有可能只有国家信息没有地区信息,
            gb2312_str = self.img[offset:o2]
            try:
                utf8_str = unicode(gb2312_str,'gb2312').encode('utf-8')
            except:
                return '未知'
            return utf8_str
    
  14. 14 jianlee Commented @ 2009-05-27 16:59Reply to this comment

    @tlze:

    佩服!确是是网页过滤了,原本是 s += '' 我不知道怎么传文件,已把附件email给Xu Xiaodong了。今天这个主题还是他帮忙发的!

    已经传好,地址在: http://linuxtoy.org/files/pyip.py

  15. 15 jianlee Commented @ 2009-05-27 17:01Reply to this comment

    @CyrusYzGTt:

    不知道是哪个qq,如果是pidgin,我想应该没有问题,如果是qq的qq,我也不知道。不过有了思路,怎么应用也就方便了。

  16. 16 jianlee Commented @ 2009-05-27 17:03Reply to this comment

    @proguru:

    开着 selinux ,开着 iptables ,仅仅 两个很难猜到名字的用户能远程登录。root用户只要一次失败就禁止连接,无效用户2次失败禁止连接,有效用户3次失败禁止连接。反正现在主要是给内部人员外部访问,也不怕封错了IP。让别人折腾去吧。还能分析分析日志学习学习。

  17. 17 eglic Commented @ 2009-05-27 22:27Reply to this comment

    厄。。。新手借问一下。。。怎么看SSH登录的日志。。。

  18. 18 Mond Commented @ 2009-05-27 22:44Reply to this comment

    问一下 怎么看被人攻击阿?

  19. 19 NetSnail Commented @ 2009-05-27 23:27Reply to this comment

    @jianlee:

    确实是有错误的,随手查了几个ip: 58.101.86.1 正确结果是浙江省嘉兴市/广电宽带,这个程序是 浙江省嘉兴市/带

    218.25.125.212 正确:辽宁省沈阳市/沈阳建筑大学,错误:辽宁省沈阳市/筑大学

  20. 20 wlx Commented @ 2009-05-27 23:52Reply to this comment

    @jianlee 请教:怎么实现的“无效用户2次失败禁止连接,有效用户3次失败禁止连接"?

  21. 21 Cooky Commented @ 2009-05-28 10:27Reply to this comment

    英国/伦敦大学皇家医学院皇家法医实验鉴定中心....

  22. 22 AutumnCat Commented @ 2009-05-28 14:17Reply to this comment

    发评论要审核吗?

    http://forum.ubuntu.org.cn/viewtopic.php?t=118366

  23. 23 AutumnCat Commented @ 2009-05-28 14:20Reply to this comment

    我贴的地址当广告了?

  24. 24 jianlee Commented @ 2009-05-29 13:18Reply to this comment

    @NetSnail:

    谢谢!不好意思,刚开始都未细看就否认了。确实有这个问题! 修改

    
    o += len(cArea) + 1
    
    为(注意!下面find的括号里面的大写O是零!应为直接用零,web系统会过滤掉!)
    
    o = self.img.find('\O',o) + 1
    
    就可以了

    ./查询纯真IP.py 58.101.86.1 ./查询纯真IP.py 218.25.125.212

  25. 25 jianlee Commented @ 2009-05-29 13:25Reply to this comment

    @Mond:

    通常的系统(rhel)都在 /var/log/secure 记录 ssh 登录信息

  26. 26 jianlee Commented @ 2009-05-29 13:29Reply to this comment

    @wlx:

    用一个python工具:DenyHosts。 其实就是定时扫描ssh日志,把达到禁止条件的用户或IP放到 /etc/hosts.deny 里面。

  27. 27 CyrusYzGTt Commented @ 2009-05-30 16:32Reply to this comment

    用在腾讯出的linuxqq

  28. 28 CyrusYzGTt Commented @ 2009-05-30 16:34Reply to this comment

    用在腾讯出的linuxqq就好

  29. 29 proguru Commented @ 2009-05-30 22:01Reply to this comment

    /var/log/auth.log

  30. 30 chen_kevin Commented @ 2009-06-23 16:28Reply to this comment

    我用了纯真20090620的版本,结果报错: struct.error: unpack requires a string argument of length 4

  31. 31 lilo Commented @ 2009-07-03 8:38Reply to this comment

    同楼上的情况...

  32. 32 lilo Commented @ 2009-07-03 8:38Reply to this comment

    跟楼上的情况一样....

  33. 33 lilo Commented @ 2009-07-03 9:43Reply to this comment

    自己解决了 不是ip库版本的问题 上面的程序是linux下的 所以打开文件的时候用的是 f = file(dbname, 'r') 在linux下没问题但是在windows下面打开二进制文件的话一定要用'rb'模式 所以程序里第49行改成 f = file(dbname, 'rb') 就可以了