-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathethereum.py
308 lines (278 loc) · 13.1 KB
/
ethereum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
from collections import namedtuple
from json import dumps, loads
from os.path import isfile
from time import time
from uuid import uuid4
from db import DB
from solc import compile_source
import web3
from web3.contract import ConciseContract
namedtuple('userData', 'id, h_pw') # user data (id and hashe pw
database = DB()
class Transaction:
def __init__(self, dbdata, eth):
self.id = dbdata[0].decode("utf-8")
self.timestamp=int(dbdata[1])
eth_data = eth.get_event(self.id)
self.data = loads("{}" if eth_data == '' else eth_data )
def event_description(self):
kinds = {
"F": "Unrecognized user with IP address #IP tried to login",
"l": "User " + ("#userid logged in" if (self.data["status"] if "status" in self.data.keys() else '') else "failed to log in"),
"g": "Nonexisting admin tried to login from IP address #IP",
"n": "Existing admin #adminid " + ("logged in" if (self.data["status"] if "status" in self.data.keys() else '') else "failed to log in"),
"x": "User #userid accessed the dataset '#dataset'",
"q": "User #userid queried the dataset '#dataset'",
"p": "User #userid changed his password",
"m": "record edited", # TODO: Fix everything there
"D": "record added",
"t": "record deleted",
"h": "health",
"i": "import",
"r": "user registration"
}
kind = kinds[self.id[0]]
if self.id[0] == "n":
if "status" in self.data.keys():
admin = database.get_admin(self.data["user_id"])
kind = kind.replace("#adminid", admin.name + " " + admin.surname)
else:
kind = kind.replace("#adminid", "")
elif self.id[0] in ["l", 'x', 'q', 'p'] and "status" in self.data.keys():
user = database.get_user_from_id(self.data["user_id"])
kind = kind.replace("#userid", user.name + " " + user.surname + " from " + user.organization)
elif self.id[0] in ["l", 'x', 'q', 'p'] and "user" in self.data.keys():
user = database.get_user_from_id(self.data["user"])
kind = kind.replace("#userid", user.name + " " + user.surname + " from " + user.organization)
for k, val in self.data.items():
kind = kind.replace('#' + str(k), str(val))
return kind
def security_score(self):
dict = {
"F": "danger",
"l": ("warning" if not (self.data["status"] if "status" in self.data.keys() else '') else "success"),
"g": "danger",
"n": ("warning" if not (self.data["status"] if "status" in self.data.keys() else '') else "success"),
"x": "success",
"q": "success",
"p": "success",
"m": "success",
"D": "success",
"t": "warning",
"h": "danger",
"i": "success",
"r": "success"
}
return dict[self.id[0]]
class Ethereum:
"""
Ethereum class to deal with smart contracts
-METHODS:
1. get_user -> get namedtuple(id, and hashed pw) given id, as long there will ce no bc the string will be empty
-- Jakob please implement
-INITIALIZATION -> FIXIT
"""
def __init__(self, providerAddress = 'http://192.168.210.173:8545'):
# Inizialize Web3 object
self.w3 = web3.Web3(web3.Web3.HTTPProvider(providerAddress))
# Tell the blockchain which account to use
self.w3.eth.defaultAccount = self.w3.eth.accounts[0]
# Now dealing with User details smart contract
if not isfile("smart-contracts/user-details.json"):
with open("smart-contracts/user-details.sol") as f:
# Read Solidity source code
login_contract_source = f.read() # read login smart contract from sourcefile
# Compile Solidity
compiled_login_contract = compile_source(login_contract_source)
# Get interface for the contract
login_contract_iface = compiled_login_contract["<stdin>:UserDetails"]
# Start the registration contract onto the blockchain
UserDetails = self.w3.eth.contract(abi=login_contract_iface['abi'], bytecode=login_contract_iface['bin'])
# Initialize transaction
tx_hash = UserDetails.constructor().transact()
# Get transaction details
tx_receipt = self.w3.eth.waitForTransactionReceipt(tx_hash)
# Save ABI and contract address to file
sc_ud_abi=login_contract_iface["abi"]
sc_ud_address=tx_receipt.contractAddress
f = open("smart-contracts/user-details.json", 'w')
f.write(dumps({"address": sc_ud_address, "abi": sc_ud_abi}))
f.close()
else:
# Load smart contract data from file
with open("smart-contracts/user-details.json") as f:
dati_sc_ud = loads(f.read())
sc_ud_address = dati_sc_ud["address"]
sc_ud_abi = dati_sc_ud["abi"]
# Loading logging smart contract
if not isfile("smart-contracts/logging.json"):
with open("smart-contracts/logging.sol") as f:
# Read Solidity source code
logging_contract_source = f.read() # read login smart contract from sourcefile
# Compile Solidity
compiled_logging_contract = compile_source(logging_contract_source)
# Get interface for the contract
logging_contract_iface = compiled_logging_contract["<stdin>:Logging"]
# Start the registration contract onto the blockchain
Logging = self.w3.eth.contract(abi=logging_contract_iface['abi'], bytecode=logging_contract_iface['bin'])
# Initialize transaction
tx_hash = Logging.constructor().transact()
# Get transaction details
tx_receipt = self.w3.eth.waitForTransactionReceipt(tx_hash)
# Save ABI and contract address to file
sc_log_abi=logging_contract_iface["abi"]
sc_log_address=tx_receipt.contractAddress
f = open("smart-contracts/logging.json", 'w')
f.write(dumps({"address": sc_log_address, "abi": sc_log_abi}))
f.close()
else:
# Load smart contract data from file
with open("smart-contracts/logging.json") as f:
dati_sc_log = loads(f.read())
sc_log_address = dati_sc_log["address"]
sc_log_abi = dati_sc_log["abi"]
# Prepare the smart contract
self.user_details = self.w3.eth.contract(
address=sc_ud_address,
abi=sc_ud_abi,
)
self.logging = self.w3.eth.contract(
address=sc_log_address,
abi=sc_log_abi,
)
def get_user(self, id):
h_pw = self.user_details.functions.getPwdHash(id).call()
user = namedtuple('user_data', 'user_id user_pwd_hash')
return user(id, h_pw)
def set_user_hash(self, uid, p_hash):
tx_hash = self.user_details.functions.addUser(uid, p_hash).transact()
self.w3.eth.waitForTransactionReceipt(tx_hash)
def save_auth(self, user_id, outcome, admin=False):
# Save authentication attempt into the blockchain provided the user id, None if the user doesn't exist
attempt_id = ('n' if admin else 'l')+str(uuid4())
# attempt_id is the id of the event
# The blockchain doesn'th have the capabilities to get the list of transaction
# moreover, if you have a mismatch between the blockchain and the db it could
# mean that the DB has been compromised. Therefore, let's save basic transaction data there too
database.save_audit_transaction(attempt_id)
# Save on the blockchain the attempt_id and the user_id
tx_hash = self.logging.functions.addEvent(attempt_id, dumps({
"timestamp": int(time()),
"user_id": user_id,
"status": outcome
})).transact()
self.w3.eth.waitForTransactionReceipt(tx_hash)
def get_event(self, ev_id):
return self.logging.functions.getEvent(ev_id).call()
def report_login_failure(self, ip, admin=False):
# TODO: Fix IP
attempt_id = ('g' if admin else 'F')+str(uuid4())
# Save basic transaction data on the DB
database.save_audit_transaction(attempt_id)
tx_hash = self.logging.functions.addEvent(attempt_id, dumps({
"timestamp": int(time()),
"IP": ip
})).transact()
self.w3.eth.waitForTransactionReceipt(tx_hash)
def get_audit_len(self):
return self.logging.functions.getEventsLength().call()
def log_data_access(self, user, dataset):
transaction_id = "x" + str(uuid4())
database.save_audit_transaction(transaction_id)
tx_hash = self.logging.functions.addEvent(transaction_id, dumps({
"timestamp": int(time()),
"user": user,
"dataset": dataset
})).transact()
self.w3.eth.waitForTransactionReceipt(tx_hash)
def log_query(self, user, dataset, query):
transaction_id = "q" + str(uuid4())
database.save_audit_transaction(transaction_id)
tx_hash = self.logging.functions.addEvent(transaction_id, dumps({
"timestamp": int(time()),
"user": user,
"dataset": dataset,
"query_hash": None # TODO: put the hash and save query in DB
})).transact()
self.w3.eth.waitForTransactionReceipt(tx_hash)
def log_change_pwd(self, user):
transaction_id = "p" + str(uuid4())
database.save_audit_transaction(transaction_id)
tx_hash = self.logging.functions.addEvent(transaction_id, dumps({
"timestamp": int(time()),
"user": user
})).transact()
self.w3.eth.waitForTransactionReceipt(tx_hash)
def log_record_edit(self, record, dataset, user):
transaction_id = "m" + str(uuid4())
database.save_audit_transaction(transaction_id)
tx_hash = self.logging.functions.addEvent(transaction_id, dumps({
"timestamp": int(time()),
"user": user,
"record": record,
"dataset": dataset
})).transact()
self.w3.eth.waitForTransactionReceipt(tx_hash)
def log_record_add(self, record, dataset, user):
transaction_id = "D" + str(uuid4())
database.save_audit_transaction(transaction_id)
tx_hash = self.logging.functions.addEvent(transaction_id, dumps({
"timestamp": int(time()),
"user": user,
"record": record,
"dataset": dataset
})).transact()
self.w3.eth.waitForTransactionReceipt(tx_hash)
def log_record_delete(self, record, dataset, user):
transaction_id = "t" + str(uuid4())
database.save_audit_transaction(transaction_id)
tx_hash = self.logging.functions.addEvent(transaction_id, dumps({
"timestamp": int(time()),
"user": user,
"record": record,
"dataset": dataset
})).transact()
self.w3.eth.waitForTransactionReceipt(tx_hash)
def log_user_registration(self, admin, user):
transaction_id = "r" + str(uuid4())
database.save_audit_transaction(transaction_id)
tx_hash = self.logging.functions.addEvent(transaction_id, dumps({
"timestamp": int(time()),
"user": user,
"admin": admin
})).transact()
self.w3.eth.waitForTransactionReceipt(tx_hash)
def log_dataset_import(self, admin, dataset):
transaction_id = "i" + str(uuid4())
database.save_audit_transaction(transaction_id)
tx_hash = self.logging.functions.addEvent(transaction_id, dumps({
"timestamp": int(time()),
"dataset": dataset,
"admin": admin
})).transact()
self.w3.eth.waitForTransactionReceipt(tx_hash)
def get_audit_data(self):
database.cursor.execute("SELECT * FROM Audit ORDER BY Timestamp DESC")
return [Transaction(l, self) for l in database.cursor.fetchall()]
def healthy_log(self):
database.cursor.execute("SELECT * FROM Audit")
return len(database.cursor.fetchall()) == self.get_audit_len()
def healthcheck(self):
if not self.healthy_log():
transaction_id = "h" + str(uuid4())
database.save_audit_transaction(transaction_id)
tx_hash = self.logging.functions.addEvent(transaction_id, dumps({
"timestamp": int(time())
})).transact()
self.w3.eth.waitForTransactionReceipt(tx_hash)
def get_user_last_pwd_change(self, user_id):
database.cursor.execute("SELECT * FROM Audit WHERE Transaction LIKE \"p%\" ORDER BY Timestamp DESC;")
events = database.cursor.fetchall()
for event in events:
eth_data = self.get_event(event[0])
if eth_data == '':
continue
event_data = loads(eth_data)
if event_data["user"] == user_id:
return event_data["timestamp"]
return None