python3读取sqlyog配置文件中的密码

Life is short, you need Python - Bruce Eckel
Package Index, Python 3.5.3 documentation
回复
头像
超时空偷鸡队
初来炸道
初来炸道
帖子: 11
注册时间: 2017年04月08日 23:09

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文件片段:

```

代码: 全选

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


举例:
```
>>> 2<<10 2048 >>> 2<<10&255 0
```

#####python3读取sqlyog.ini中的密码:
```python

代码: 全选

# -*- coding: utf-8 -*-
"""
Created on 2017-03-15 07:42:58

@author: codegay
"""

import os
import configparser
import base64


def decode(base64str):
    tmp = base64.b64decode(base64str)
    return bytearray([(b<<1&255)|(b>>7) for b in tmp]).decode("utf8")

sqlyogini = os.environ.get('APPDATA')+"\\SQLyog\\sqlyog.ini"
print("sqlyogini文件路径:",sqlyogini)
ini = configparser.ConfigParser()
ini.read(sqlyogini,encoding='utf8')

connections = [r for r in ini.sections() if 'name' in ini.options(r) and ini.get(r,'password')]

for c in connections:
    name = ini.get(c,'name')
    host = ini.get(c,'host')
    user = ini.get(c,'user')
    b64pass = ini.get(c,'password')
    password = decode(b64pass)
    print(name,host,user,sep='\n')
    print('密码',password)
    print('----------------------------------------------------------------------------------')
```

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


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

**以下是 sqlyog源码中密码加密解密的函数:**https://github.com/webyog/sqlyog-commun ... Helper.cpp
额外说一句,sqlyog的代码感觉是相当于好看的。

```

代码: 全选

DecodePassword(wyString &text)
{
	wyChar      pwd[512]={0}, pwdutf8[512] = {0};

	strcpy(pwdutf8, text.GetString());
	
	DecodeBase64(pwdutf8, pwd);
	RotateBitLeft((wyUChar*)pwd);
	strncpy(pwdutf8, pwd, 511);
	text.SetAs(pwdutf8);
	
	return wyTrue;
}


/*rotates bit right */
void
RotateBitRight(wyUChar *str)
{
	wyInt32     count;

	for(count = 0; str[count]; count++)
		str[count] = (((str[count])>>(1)) | ((str[count])<<(8 - (1))));

    return;
}

// We keep the name in encrypted form.
// so we do a bit rotation of 1 on the left before writing it into the registry.
void 
RotateBitLeft(wyUChar *str)
{
	wyInt32     count;

    for(count = 0; str[count]; count++)
		str[count] = (((str[count])<<(1)) | ((str[count])>>(8 - (1))));

	return;
}

wyBool
EncodePassword(wyString &text)
{
	wyChar *encode = NULL, pwdutf8[512] = {0};

	strcpy(pwdutf8, text.GetString());
	RotateBitRight((wyUChar*)pwdutf8); 
	EncodeBase64(pwdutf8, strlen(pwdutf8), &encode);
	strncpy(pwdutf8, encode, 511);

	text.SetAs(pwdutf8);
	
	if(encode)
		free(encode);

	return wyTrue;
}
```
>>>刮开有奖
:happy
头像
vicyang
版主
版主
帖子: 56
注册时间: 2016年07月21日 20:35
联系:

Re: python3读取sqlyog配置文件中的密码

帖子 vicyang »

虽然论坛目前没有支持到 markdown
但是可以写个脚本,replace 里面的标签到论坛格式标签。
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 1 访客