简体   繁体   中英

How to protect mysqldb connection in python?

I'm creating a pygtk app that needs a mysql connection from a remote db.

db = MySQLdb.connect("remotehost","username","password","databse", charset='utf8')

App is almost completed and going to be published. But the problem is, if anyone decompile this script they can easily read the above credentials and then there is a security issue. So how do I can protect this code or is there any way I can strongly compile this file?

Database connections are generally made from trusted computers inside a trusted network, for a variety of reasons:

  • As you've seen, the client needs to store access credentials to the DB.
  • Most of the time, such connections are made with no transport security (unencrypted), so any eavesdropper can observe and mangle requests/responses.
  • Latency in the path to the DB is usually a issue, so you want to minimize it, thus placing the client near to the DB

Violating this common practice means you'll have to deal with these problems.

It's very common to have a intermediary service using some other protocol (for example, HTTP/REST) to exposes an API that indirectly modifies the database. You keep the service on a host in your trusted computing base , and only that one host accesses the DB. In this architecture, you can (and should) perform authentication and mandatory access control in the intermediary service. In turn, having different credentials for each client that accesses that service will help keep things secure.


If you can't rewrite your application at this point, you should follow patriciasz's suggestion on keeping the least priviledge possible . You may also be interested in techniques to make it harder (but not impossible) to obtain the credentials

There is no way to protect your code (compiled or not) from the owner of the machine it runs on.

In this case he will effectively have the same access restrictions your application's SQL user has.

There is no good way to protect your code, but you can use read_default_file options while using connect . The connection arguments will then be read form the file, specified with read_default_file . NOTE: This in no way is securing your username, password since anyone having access to the cnf file can get the information.

Build an interface between the database and the application. Only the interface will get true database access.

Give the app credentials to access the interface, and only the interface, then let the interface interact with the data base. This adds a second layer to boost security and helps to protect database integrity.

In the future develop with separate logic from the start. The app does not need to accesses the data base. Instead, it needs data from the database.

Also as a rule of database security avoid putting credentials on the client side. If you have n apps then n apps can access your data base, and controlling access points is a big part of database logic.

分开程序逻辑是真正的交易,凭据不必像芯片上所说的那样驻留在客户端计算机上

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM