def main2(): # Get the key from the database database = 'flask/ml_fingerprint_database.db' conn = sqlite3.connect(database) conn.row_factory = sqlite3.Row c = conn.cursor() db_key = c.execute('select * from key order by id desc limit 1').fetchone() public_key = RSA.import_key(db_key['publickey']) private_key = RSA.import_key(db_key['privatekey']) # Decorate BaseEstimator ml_fingerprint.decorate_base_estimator() modelname = 'example_regression' model = example_models.vanderplas_regression() model.sign(private_key) api_key = "Insert API Key here" rem = remote.RemoteServer(url, api_key, unsafe_https=True) rem.insert_model(model, modelname, True, "regression", {"MSE": 0.987}, "1.2.78", {"parameter_X": "TEST"}, datetime.datetime.now(), "Example from VanderPlas book") #update_model(model, "example_regression", True, "regression", {"MSE":0.987}, "1.2.78", {"parameter_X":"TEST"}) server_model = rem.get_model(modelname, public_key) print(server_model.coef_) rem.delete_model(modelname)
def main3(): database = 'flask/ml_fingerprint_database.db' conn = sqlite3.connect(database) conn.row_factory = sqlite3.Row c = conn.cursor() db_key = c.execute('select * from key order by id desc limit 1').fetchone() public_key = RSA.import_key(db_key['publickey']) private_key = RSA.import_key(db_key['privatekey']) # Decorate BaseEstimator ml_fingerprint.decorate_base_estimator() modelname = 'pokemon_clustering' model, groups = example_models.pokemon_clustering() model.sign(private_key) api_key = "plhtvpafgH3Al6KMa8IbtQ" rem = remote.RemoteServer(url, api_key, unsafe_https=True) rem.insert_model( model, modelname, True, "clustering", {}, "1.0.0", {}, datetime.datetime.now(), "Modelo de clustering que agrupa Pokémon en 4 grupos distintos.") list_models = rem.get_list_models(doprint=True) rem.delete_model(modelname)
def unsigned_test(model, key): # This test gets a model, adds the fingerprint methods, DOESN'T sign the model and then tries to verify it. print("Starting unsigned test...") # Adding the verification methods to the BaseEstimator class ml_fingerprint.decorate_base_estimator() # Verifying the model model.verify(key.publickey()) print()
def unaltered_test(model, key): # This test gets a model, adds the fingerprint methods, signs the model and then tries to verify it. print("Starting unaltered test...") # Adding the verification methods to the BaseEstimator class ml_fingerprint.decorate_base_estimator() # Signing the model after it has been trained model.sign(key) # Verifying the model model.verify(key.publickey()) print()
def setUp(self): self.model = example_models.vanderplas_regression() self.private_key = RSA.import_key('''-----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEAnB5U96c2cR7C/2BaBd7tSgNMQAODRay0n4AVJRw2WyuP828T w18lK3yapnWfwxTMMv7machk7eyWRvrmp84fzdZqeebetHEOdWhQGJhghs15fnFA a1NHDcpFbbH6JoLhZyL0BPNZvQdIXcrGdL/eGCGKvMWr8OjV8Q1W1v1Oc7rggMwm WggM9XCgQGACXQROJfs4+cowVvqxJeSJ9ucp/UmaQoaBRP8ncf6uUAfoHreWEH1n qQqoQ5TYqw4/QqnK6owCWwaHOWFsB+e8AkqpVLfcsrXeCOdYKNFUtmhmfWzHFmKs MWqrMOkewSnWYix48EnewXXpEDi2Ip9437cDTwIDAQABAoIBAAHwsjMW0fEeUqGX zxQuoUKBrDpwKfM/LgxV5mG89cd+WL1nCaKRWYnRfCKMItWszUhEuOYNmOlWAMdj mHLcGpPfCeLgIt48UAIc7Ve5JsYTsAVXDcRi2TwfyoJQQLki5Peyn8/NwdRrZdeC JB9S1z1oFJUkYAI+xHrf9krPiFEJ7dJwSWBk1G/0HMS2+aUXiyNqB+oKvhNK2uTL ZSRJJl0rrhruNvST9RDsqj6aEAyHpOsxNE919Bvj2XqNpetFIOlPOGSqd6M8Xpgq Jv6sTtIIGchCB2LmCHHKfFlu2WdLSy8YtuTT5GE3crTqu3J+6REkt6051xwDhixK fXdGBBECgYEAvYfZ9nygXcuXRPjYElq4TnPv1Ue6lis+/UAphc+ElGJMhNhvK3g9 Zqc+JZCXF2wBVvx+JYtw1QOSnHSN9sNDT3k/p3WE96jgO/goHIlrLhTKHt3lAnOp 0JyTCnsgBgVTVDvylkwW1wyMr0gmwAkKlNx12s0fROBaSj+lJ+/1kU0CgYEA0t64 LwDY+WsDtEiBxFPVjkMiqz1oZBlEm1bKgQteGBrZ14qtw15PNXT2qoErBkmLGio1 K8WHyaQufnjgLhLkPns4MwspiPi4PFUoN/xqi9R4IlXp1BwoHYTiPTCBVlP97Mcd rrLd/iibIYErglnqT1g+bqp3OV4mPBf0TAA+WQsCgYBNr9Swyw60w5HCrLlsdJEd XzpMUvVxPlK4XcdnWNNAOzagMVMIH2YToLPBlA3KhPPbou1WGFzsg2ViCghywEDj 35Au5OXW2frnueU/JTiwj8b1dgC7y4ssvjErV6KdtSsd2OCrqI6oITW9VzbC+jHD KMaKJBAQHGIGEyZwQ1SF9QKBgCCQpq8TmW6DWVIZNHkmXT+YHJ2HYTy8Lj0zHRjq sYBVfTjtQMKAKQlfY8yXIBbberDyvb1hxwOb6FY9wpk676a6jsZLPgHaL1GZ8Rkc UTQA/wCGdhL8ujE82VeLyx2S4Q8U/P+CvgfHK5X0Bc0ep7HTNekPSFRCtvLm3CJ0 kyxbAoGBAKf7ngLFzGWqjszoFsU2cLOOToWXrGJKGx5HSYp9lcOF8ADFCpWkMSgn BZkzaa3IKIKoHOhyf4aOHif1uqBbF1tAMJ+Q6LJ4XDoeIeylWlZW2rNLw9ETfPow f2sFc2oi0MN86k7DSB22XGo5ifJTZAp9KLZIOjh1AW+5WUJHnNhy -----END RSA PRIVATE KEY-----''') self.public_key = RSA.import_key('''-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnB5U96c2cR7C/2BaBd7t SgNMQAODRay0n4AVJRw2WyuP828Tw18lK3yapnWfwxTMMv7machk7eyWRvrmp84f zdZqeebetHEOdWhQGJhghs15fnFAa1NHDcpFbbH6JoLhZyL0BPNZvQdIXcrGdL/e GCGKvMWr8OjV8Q1W1v1Oc7rggMwmWggM9XCgQGACXQROJfs4+cowVvqxJeSJ9ucp /UmaQoaBRP8ncf6uUAfoHreWEH1nqQqoQ5TYqw4/QqnK6owCWwaHOWFsB+e8Akqp VLfcsrXeCOdYKNFUtmhmfWzHFmKsMWqrMOkewSnWYix48EnewXXpEDi2Ip9437cD TwIDAQAB -----END PUBLIC KEY-----''') ml_fingerprint.decorate_base_estimator()
def main_function(): # Get the key from the database database = 'ml_fingerprint_database.db' conn = sqlite3.connect(database) conn.row_factory = sqlite3.Row c = conn.cursor() db_key = c.execute('select * from key order by id desc limit 1').fetchone() public_key = RSA.import_key(db_key['publickey']) private_key = RSA.import_key(db_key['privatekey']) # Decorate BaseEstimator ml_fingerprint.decorate_base_estimator() modelname = 'example_regression' model = example_models.vanderplas_regression() model.sign(private_key) insert_model(model, modelname, True, "regression", {"MSE": 0.987}, "1.2.78", {"parameter_X": "TEST"}) #update_model(model, "example_regression", True, "regression", {"MSE":0.987}, "1.2.78", {"parameter_X":"TEST"}) server_model = get_model(modelname, public_key) print(server_model.coef_) delete_model(modelname)
def altered_test(model, key): # This test gets a model, adds the fingerprint methods, signs the model, MODIFIES IT and then tries to verify it. print("Starting altered test...") # Adding the verification methods to the BaseEstimator class ml_fingerprint.decorate_base_estimator() # Signing the model BEFORE it has been trained model.sign(key) # Modifying the model if type(model).__name__ == 'SVC': model.__dict__['dual_coef_'][0][0] = -4.0 elif type(model).__name__ == 'LinearRegression': model.__dict__['coef_'][0] = -4.0 elif type(model).__name__ == 'LogisticRegression': model.__dict__['coef_'][0] = -4.0 else: print(model.__dict__) print("altered_test() failed. The given model isn't compatible with this test.") return # Verifying the model (that has been signed before training, and therefore, should not verify) model.verify(key.publickey()) print()
#from sklearn.linear_model import LinearRegression from Crypto.PublicKey import RSA import sqlite3 import pickle import requests as req import joblib #Get the key from the database database = 'ml_fingerprint_database.db' conn = sqlite3.connect(database) conn.row_factory = sqlite3.Row c = conn.cursor() db_key = c.execute('select * from key order by id desc limit 1').fetchone() public_key = RSA.import_key(db_key['publickey']) ml_fingerprint.decorate_base_estimator() modelname = 'example_unaltered' res = req.get('http://localhost:5000/getmodel?modelname=' + modelname) if res.status_code != 200: print(res.text) else: model = pickle.loads(res.content) if ml_fingerprint.isInyected(model): model.verify(public_key) else: print(model.coef_) model = joblib.load('model.joblib') if ml_fingerprint.isInyected(model): model.verify(public_key)