[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创教程] python3读取sqlyog配置文件中的密码

###这个人有什么目的?:
我多多少少听过一些安全圈的大牛说到类似的思路,大意是可以通过扫描各种程序和服务的配置文件(比如SVN的文件,RSYNC的配置文件等),
从中发现敏感信息,从而找到入侵的突破口。沿着同样的思路扩展,管理员们用着各种各样的管理工具(SSH,FTP,mysql之类等等的管理工具),
这类工具通常都在本地存着密码,如果这些工具中配置信息被恶意扫描读取走,有可能会造成极其重大的损失。
所以我想尝试验证看看各种管理工具的敏感信息是不是容易读取并解码出来,二来这过程中有思考,也会有各种资料和动手实践,能很好地练习、复习编程技能。

基于总有刁民想害朕的想法,我比较希望操作系统或者安全软件能提供更加精细的权限控制,毕竟不管是百度还是360或者迅雷的全家桶,这些公司的软件底线比较低,
如果这些软件或者其它软件没有读取我们敏感数据的权限的话,我们会更放心一些。

**sqlyog是一款商业开源的非常不错的mysql管理工具。**

#####安装sqlyog后,会把配置文件存在用户目录下:
C:\Users\%user%\AppData\Roaming\SQLyog\sqlyog.ini
取环境变量%AppData% +上 SQLyog\sqlyog.ini 就可以。


###以下是 sqlyog.ini文件片段:

```
  1. [UserInterface]
  2. Language=zh-cn
  3. Version=2
  4. ThemeFile=963
  5. ThemeType=1
  6. [Themedetails]
  7. ThemeFile=964
  8. ThemeType=1
  9. [SQLYOG]
  10. Encoding=utf8
  11. Left=0
  12. Top=0
  13. Right=600
  14. Bottom=600
  15. Maximize=0
  16. Host=新连接
  17. ChildMaximized=1
  18. [Connection 1]
  19. Name=新连接
  20. Host=localhost
  21. User=root
  22. StorePassword=1
  23. Password=sLBzS1h0309zR9IxMQ==
  24. Port=3306
  25. Database=
  26. ......
复制代码
```
"=="号是base64编码最明显的特征,一般看到类似sLBzS1h0309zR9IxMQ==这样的字符,就知道是base64。
sqlyog对密码的编码过程是对密码的字节分别进行位运算后,再进行base64编码存在配置文件中。
**另一个需要注意的是python的bit左移是会进位的,所以需要与255。**


举例:
```
  1. >>> 2<<10
  2. 2048
  3. >>> 2<<10&255
  4. 0
复制代码
```

#####python3读取sqlyog.ini中的密码:
```python
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2017-03-15 07:42:58
  4. @author: codegay
  5. """
  6. import os
  7. import configparser
  8. import base64
  9. def decode(base64str):
  10.     tmp = base64.b64decode(base64str)
  11.     return bytearray([(b<<1&255)|(b>>7) for b in tmp]).decode("utf8")
  12. sqlyogini = os.environ.get('APPDATA')+"\\SQLyog\\sqlyog.ini"
  13. print("sqlyogini文件路径:",sqlyogini)
  14. ini = configparser.ConfigParser()
  15. ini.read(sqlyogini,encoding='utf8')
  16. connections = [r for r in ini.sections() if 'name' in ini.options(r) and ini.get(r,'password')]
  17. for c in connections:
  18.     name = ini.get(c,'name')
  19.     host = ini.get(c,'host')
  20.     user = ini.get(c,'user')
  21.     b64pass = ini.get(c,'password')
  22.     password = decode(b64pass)
  23.     print(name,host,user,sep='\n')
  24.     print('密码',password)
  25.     print('----------------------------------------------------------------------------------')
复制代码
```

#####运行程序后输出:
```
  1. sqlyogini文件路径: C:\Users\root\AppData\Roaming\SQLyog\sqlyog.ini
  2. 新连接
  3. localhost
  4. root
  5. 密码 aa新连接bb
复制代码
```


2017-4-8 21:55:53 codegay
###参考资料:
**Retrieve passwords stored in SQLyog** https://dd9e.blogspot.jp/2014/05 ... ored-in-sqlyog.html
**sqlyog-decode-pwd** https://github.com/gkralik/sqlyo ... ob/master/decode.py

**以下是 sqlyog源码中密码加密解密的函数:**https://github.com/webyog/sqlyog ... rc/CommonHelper.cpp
额外说一句,sqlyog的代码感觉是相当于好看的。
  1. DecodePassword(wyString &text)
  2. {
  3. wyChar      pwd[512]={0}, pwdutf8[512] = {0};
  4. strcpy(pwdutf8, text.GetString());
  5. DecodeBase64(pwdutf8, pwd);
  6. RotateBitLeft((wyUChar*)pwd);
  7. strncpy(pwdutf8, pwd, 511);
  8. text.SetAs(pwdutf8);
  9. return wyTrue;
  10. }
  11. /*rotates bit right */
  12. void
  13. RotateBitRight(wyUChar *str)
  14. {
  15. wyInt32     count;
  16. for(count = 0; str[count]; count++)
  17. str[count] = (((str[count])>>(1)) | ((str[count])<<(8 - (1))));
  18.     return;
  19. }
  20. // We keep the name in encrypted form.
  21. // so we do a bit rotation of 1 on the left before writing it into the registry.
  22. void
  23. RotateBitLeft(wyUChar *str)
  24. {
  25. wyInt32     count;
  26.     for(count = 0; str[count]; count++)
  27. str[count] = (((str[count])<<(1)) | ((str[count])>>(8 - (1))));
  28. return;
  29. }
  30. wyBool
  31. EncodePassword(wyString &text)
  32. {
  33. wyChar *encode = NULL, pwdutf8[512] = {0};
  34. strcpy(pwdutf8, text.GetString());
  35. RotateBitRight((wyUChar*)pwdutf8);
  36. EncodeBase64(pwdutf8, strlen(pwdutf8), &encode);
  37. strncpy(pwdutf8, encode, 511);
  38. text.SetAs(pwdutf8);
  39. if(encode)
  40. free(encode);
  41. return wyTrue;
  42. }
复制代码
3

评分人数

去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

论坛不支持MD,重新排版太费劲,原文:
http://www.cnblogs.com/gayhub/p/python3-sqlyog-password.html
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

干货点赞,python3 在学ing

TOP

本帖最后由 codegay 于 2017-4-9 01:45 编辑

回复 3# 523066680


    你学这东西干嘛啊?
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

本帖最后由 523066680 于 2017-4-9 09:43 编辑

回复 4# codegay


    数据可视化特方便, 顺带也想试试PyQt。

TOP

返回列表