I have my MySQL database on a Raspberry Pi. I set it up like this:
sudo mariadb -u root -p
CREATE DATABASE Login;
CREATE USER RaspberryPi@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON Login.* TO RaspberryPi@'%';
SELECT Login;
CREATE TABLE Users;
I have this python code:
import mysql.connector
mydb = mysql.connector.connect(host="addr_of_pi", user="RaspberryPi", passwd="password", database="Login")
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM Users")
print(mycursor)
The problem is, that I want to give this script to a person as an .exe file but I don't want to give him the account credentials to my database. I tried using PyProtect
but the .exe script can still be read in some way. I also tried using os.gentenv(...)
but then you have to create a .env file with the credentials that also can be read. So is there an other way?
CodePudding user response:
Try stored procedures.
Exe files can easily be decompiled. Often you can just open the file in a text editor and see the strings, so your approach is not safe.
You should instead:
- Create a stored procedure which does the select:
CREATE DEFINER=root@'%' PROCEDURE SelectUsers()
READS SQL DATA
BEGIN
SELECT
*
FROM Users;
END
- Only grant permissions to execute the procedure to the user. Since the procedure is defined with
DEFINER=root@'%'
the RaspberryPi user will get the data on behalf of the root user, therefore there is no need to grant access to the table itself:
GRANT EXECUTE ON PROCEDURE Login.SelectUsers TO 'RaspberryPi'@'%';
- Hand out your script like this:
import mysql.connector
mydb = mysql.connector.connect(host="addr_of_pi", user="RaspberryPi", passwd="password", database="Login")
mycursor = mydb.cursor()
mycursor.execute("CALL SelectUsers()")
print(mycursor)
With this approach your RaspberryPi user will have no access to any table in your database, it can just CALL the stored procedure you granted execute permission. So, no need to "exe" your Python script.
CodePudding user response:
Long story short, anything you publish is "potentially" breakable.
Adding an auth server in the middle will solve the core issue. He connects to your server, your server connects to ... whatever.
Shitty thing to say, but basically anything that you're sending a user; just assume it will be cracked. You can make it harder to crack blah blah, but at the end of the day user side security (ie just having the db creds in an exe) is essentially useless. You should literally -never- expose credentials in your app that way.
CodePudding user response:
I don't think there's any easily safe way to do this. No matter what you do as long as your credentials are saved into the code or the way to get them (such as a request to a webpage) is, people would be able to extract them. Look into running your own server and learning how to create an API.
Hide your database credentials behind a webserver, using PHP or Django/python. The connection to the actual database should happen on there, your .exe application should connect to an API on that server that handles getting the data from the database for you. I don't know how experienced your friend would be or if the application would be used by others further down the line, but I highly recommend investing time into learning setting up servers and creating APIs.
In your case, look into running a Django server on your Raspberry Pi. The Python application would make some kind of request to an API on there that'd handle database functionality.
This way, your database credentials and data itself would be safe, since your API would only allow a specific set of actions and won't give anyone full access to your database.
Disclaimer: It'd take quite the time to get into servers, APIs, protection against SQL injections, etc. It's requires effort to protect your database.
CodePudding user response:
you should consider give the user name and password as arguments.
user = input()
password = input()