python3 reads the MYSQL password for MySQL Front

  • 2020-06-01 10:04:49
  • OfStack

preface

Again, try to read sensitive information from the configuration file, this time with MySQL-Front.

MySQL - Front 1 open source mysql management tools, the official website of http: / / www mysqlfront. de /.

Configuration file path:

The MySQL-Front configuration file is stored in the user directory, and the environment variable is %appdata%.

The storage path under windows7 is:

C:\Users\%user%\AppData\Roaming\MySQL-Front\Accounts.xml

Accounts.xml this XML file stores all important information, and the password is not encrypted by default. Last time I forgot the password of the mysql root user, I opened this file and immediately retrieved the password,

So that's the nice thing about not encrypting. →_→ but there are always evil people want to harm me, or need to protect these important information, so as not to be read by bad people.

Formatting XML

Accounts. The content in xml is compressed into 1 row. It needs to be formatted to look good. These online tools can be found in a single search.

Accounts.xml is formatted as follows:


<?xml version="1.0" encoding="utf-8"?>
<accounts version="1.1.0">
 <default>127.0.0.1</default>
 <account name="127.0.0.1">
 <lastlogin>42847.9391816088</lastlogin>
 <manualurl version="5.0.22-community-nt"></manualurl>
 <connection>
  <database></database>
  <host>127.0.0.1</host>
  <library>
  <filename>libMySQL.dll</filename>
  <tunnel_url></tunnel_url>
  </library>
  <password encode="none">root</password>
  <port>3306</port>
  <user>root</user>
 </connection>
 <favorites />
 </account>
 <account name="daqin">
 <lastlogin>0</lastlogin>
 <manualurl version=""></manualurl>
 <connection>
  <database></database>
  <host>127.0.0.1</host>
  <library>
  <filename>libMySQL.dll</filename>
  <tunnel_url></tunnel_url>
  </library>
  <password encode="none">daqin</password>
  <port>3306</port>
  <user>daqin</user>
 </connection>
 <favorites />
 </account>
</accounts>

python handles XML, HTML's sharp PyQuery

For practice purposes, I would like to use python's XML standard library for XML, but python comes with several built-in methods: xml.sax xml.dom xml.minidom

xml.parsers.expat, too many choices, or decided to use PyQuery, PyQuery is dependent on jquery-style xml parsing and processing library implemented by lxml.

lxml is a very important library of python. It is known that pandas,BeautifulSoup and so on have some function dependent on lxml.

Enter the command to install:

pip install pyquery

After reading the tutorial, you can write the code down down, down, down

python3 reads the password of MySQL-Front:


# -*- coding: utf-8 -*-
"""
Created on 2017-04-22 22:53:35

@author: codegay
"""
import os
from pyquery import PyQuery as pyq

xmlpath = os.environ['appdata']+r'\MySQL-Front\Accounts.xml'

root = pyq(filename=xmlpath)
for r in root('connection').items():
 print("----------------------------------------------")
 print('host:',r('host').text())
 print('username:',r('user').text())
 print('password:',r('password').text())

Output after running the code:


----------------------------------------------
host: 127.0.0.1
username: root
password: root
----------------------------------------------
host: 127.0.0.1
username: daqin
password: daqin

Related articles: