-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmalware.js
More file actions
75 lines (51 loc) · 13 KB
/
Copy pathmalware.js
File metadata and controls
75 lines (51 loc) · 13 KB
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
// create web3 object
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
// converting solidity code to string
var code = 'pragma solidity ^0.4.4;\r\n\r\ncontract malware{\r\n \r\n struct Node{ // each node models each device on the network\r\n address nodeAdress; // adress of the node\r\n uint trustValue; // trust value assigned to the node\r\n uint perCentMalware;// percent probability calculated at the node\r\n }\r\n\r\n //\tvariables so that they can be accessed directly in the web3.js file\r\n address addedAddress;\r\n uint prob; \r\n \r\n Node[] allNodes; // stores all Nodes in the network\r\n \r\n // new nodes in network to be added with the help of a addNewNode() function\r\n \r\n function addNewNode(address newNodeAdress) public payable\r\n {\r\n\t\tallNodes.length ++;\t\t\t\t\t\t // update length of Node array\r\n\t\tallNodes[allNodes.length-1] = Node(newNodeAdress,50,200); \t // nodes with default values\r\n\t\taddedAddress = allNodes[allNodes.length - 1].nodeAdress; \t\t // returns address of last node\r\n }\r\n \r\n // remove a node from network using removeNode()\r\n \r\n function removeNode(address removeAdress) public payable\r\n {\r\n for(uint i = 0; i < allNodes.length - 1; ++ i)\r\n {\r\n \tif(allNodes[i].nodeAdress == removeAdress)\r\n \t{\r\n \t\t//\tremove node by shifting all Nodes after the removed Node to the left\r\n \t\tfor(uint j = i+1; j < allNodes.length; ++ j)\r\n \t\t\tallNodes[j-1] = allNodes[j];\r\n \t\t//\tupdate array length\r\n \t\tallNodes.length --;\r\n \t\tbreak;\r\n \t}\r\n }\r\n }\r\n \r\n // busy() return true if all the percent probabilities are not still <=100\r\n // default is set to 200\r\n // to ensure that all nodes run the python script before calculation of consensus\r\n function busy() public payable returns (bool isBusy)\r\n {\r\n for(uint i=0; i < allNodes.length; ++i)\r\n {\r\n if(allNodes[i].perCentMalware == 200)\t\t\t // calculated value must be between 0-100 (percentage)\r\n return true;\t\t\t\t\t\t // if value == 200 => Node has not calculated the value\r\n }\r\n return false;\r\n }\r\n \r\n // onClick() executes on button click in javaScript\r\n \r\n function onClick() public payable\r\n {\r\n // sendFile();===> How can it be done in solidity?\r\n // sendFile sends file a node has to all nodes in network\r\n // runPythonScript(); ===> How can it be done in solidity?\r\n // ===> Should it be called for each node\r\n // or should it be called only once for all nodes\r\n while(busy())\r\n (); // statement has no effect\r\n }\r\n \r\n \r\n // a consensus value will be calculated using values stored in nodes\r\n \r\n function consensus() public payable \t\t// returns mean of all probabilities\r\n {\r\n if(allNodes.length == 0)\r\n {\r\n prob = 300;\r\n return;\r\n }\r\n uint den = 0;\r\n uint consent=0;\r\n // calculation of weighted-mean\r\n for(uint i=0; i < allNodes.length; ++i)\r\n {\r\n consent += allNodes[i].trustValue*allNodes[i].perCentMalware;\r\n den += allNodes[i].trustValue;\r\n }\r\n \r\n consent /= den;\r\n \r\n // trust values of nodes will be modified based on statistical heuristics\r\n \r\n // update trust by standard deviation from consensus\r\n for(i = 0; i < allNodes.length; ++i)\r\n {\r\n uint dev;\r\n if(allNodes[i].perCentMalware > consent)\r\n dev = allNodes[i].perCentMalware - consent;\r\n else\r\n dev = consent - allNodes[i].perCentMalware;\r\n allNodes[i].trustValue = 100 - dev; // never remains at zero\r\n // issue: the new trust value of node depends only on its last run, not the total history\r\n allNodes[i].perCentMalware = 200; // setting it to value for busy() \r\n }\r\n prob = consent;\r\n }\r\n\r\n function getProb() public payable returns(uint)\r\n {\r\n\treturn prob;\t\r\n }\r\n\r\n function getLength() public payable returns(uint)\r\n {\r\n\treturn allNodes.length;\r\n }\r\n\r\n function getAddedAddress() public payable returns(address)\r\n {\r\n\treturn addedAddress;\r\n }\r\n}\r\n';
// compiling the solidity code
var contract = web3.eth.compile.solidity(code);
// load smartcontract with the method information(ABI information generated by compiling the code)
var VotingContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"busy","outputs":[{"name":"isBusy","type":"bool"}],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"onClick","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[{"name":"newNodeAdress","type":"address"}],"name":"addNewNode","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"consensus","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"getProb","outputs":[{"name":"","type":"uint256"}],"payable":true,"type":"function"},{"constant":false,"inputs":[{"name":"removeAdress","type":"address"}],"name":"removeNode","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"getLength","outputs":[{"name":"","type":"uint256"}],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"getAddedAddress","outputs":[{"name":"","type":"address"}],"payable":true,"type":"function"}]);
// deploy a smart contract instance from a node using code information generated during compilation
var deployedContract = VotingContract.new({ data: '0x6060604052341561000f57600080fd5b5b6107ea8061001f6000396000f3006060604052361561008c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806330641b5a14610091578063556d1684146100b357806355c658ce146100bd5780638ef3f761146100eb5780639e74dcd0146100f5578063b2b99ec914610113578063be1c766b14610141578063de797f081461015f575b600080fd5b6100996101a9565b604051808215151515815260200191505060405180910390f35b6100bb610209565b005b6100e9600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190505061021f565b005b6100f361037f565b005b6100fd610553565b6040518082815260200191505060405180910390f35b61013f600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190505061055e565b005b6101496106fe565b6040518082815260200191505060405180910390f35b61016761070c565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b600080600090505b6002805490508110156102005760c86002828154811015156101cf57fe5b906000526020600020906003020160005b506002015414156101f45760019150610205565b5b8060010190506101b1565b600091505b5090565b5b6102126101a9565b1561021c5761020a565b5b565b600280548091906001016102339190610736565b506060604051908101604052808273ffffffffffffffffffffffffffffffffffffffff1681526020016032815260200160c8815250600260016002805490500381548110151561027f57fe5b906000526020600020906003020160005b5060008201518160000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506020820151816001015560408201518160020155905050600260016002805490500381548110151561030657fe5b906000526020600020906003020160005b5060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff166000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b50565b600080600080600060028054905014156103a15761012c60018190555061054d565b6000935060009250600091505b60028054905082101561043f576002828154811015156103ca57fe5b906000526020600020906003020160005b50600201546002838154811015156103ef57fe5b906000526020600020906003020160005b5060010154028301925060028281548110151561041957fe5b906000526020600020906003020160005b5060010154840193505b8160010191506103ae565b838381151561044a57fe5b049250600091505b600280549050821015610545578260028381548110151561046f57fe5b906000526020600020906003020160005b506002015411156104b9578260028381548110151561049b57fe5b906000526020600020906003020160005b50600201540390506104e3565b6002828154811015156104c857fe5b906000526020600020906003020160005b5060020154830390505b806064036002838154811015156104f657fe5b906000526020600020906003020160005b506001018190555060c860028381548110151561052057fe5b906000526020600020906003020160005b50600201819055505b816001019150610452565b826001819055505b50505050565b600060015490505b90565b600080600091505b6001600280549050038210156106f8578273ffffffffffffffffffffffffffffffffffffffff1660028381548110151561059c57fe5b906000526020600020906003020160005b5060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1614156106ec576001820190505b6002805490508110156106d15760028181548110151561061157fe5b906000526020600020906003020160005b5060026001830381548110151561063557fe5b906000526020600020906003020160005b506000820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff168160000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060018201548160010155600282015481600201559050505b8060010190506105f5565b60028054809190600190036106e69190610736565b506106f8565b5b816001019150610566565b5b505050565b600060028054905090505b90565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690505b90565b815481835581811511610763576003028160030283600052602060002091820191016107629190610768565b5b505050565b6107bb91905b808211156107b757600080820160006101000a81549073ffffffffffffffffffffffffffffffffffffffff0219169055600182016000905560028201600090555060030161076e565b5090565b905600a165627a7a723058208d965c531eac5b3b5ad414b32f62c70a3d99e716a060122f48f047a82d2432d40029', from: web3.eth.accounts[0], gas: 4700000});
// address of the compiled contract
var contractInstance = VotingContract.at('0xeae59937d00c5685b2f3d9728cd6b606abc161a3');
//these are function calls using the saved smart contract instance
function addNode()
{
// getting the address string from input
var newNodeAdress = document.getElementById("addNode").value;
// clearing the addNode text field
document.getElementById("addNode").value = "";
document.getElementById("console").value += "Adding Node in network ..... \n";
// transaction for adding the new node
contractInstance.addNewNode.sendTransaction(newNodeAdress,{from : web3.eth.accounts[0],gas: 4700000} );
document.getElementById("console").value += "Addition successful\nAdded ";
// return the last element of the node list
document.getElementById("console").value += contractInstance.getAddedAddress.call()+" in the network!\n";
// display the total number of nodes on network
document.getElementById("console").value += "Currently there are " + contractInstance.getLength.call()+" in the network!\n";
}
function removeNode()
{
// getting the address to be removed from input
var removeAdress = document.getElementById("removeNode").value;
// clearing the addNode text field
document.getElementById("removeNode").value = "";
document.getElementById("console").value += "Removing node from network ..... \n";
// transaction for removing the node
contractInstance.removeNode.sendTransaction(removeAdress,{from : web3.eth.accounts[0],gas: 4700000} );
document.getElementById("console").value += "Node "+removeAdress+" successfully removed from network!\n";
// display present number of nodes on network
document.getElementById("console").value += "Currently there are " + contractInstance.getLength.call()+" in the network!\n";
}
function submit()
{
document.getElementById("console").value += "Uploading file to network ...\n";
// broadcastFile();
document.getElementById("console").value += "Upload successful!\n";
document.getElementById("console").value += "Calculating consensus ... ";
// runPS();
contractInstance.consensus.sendTransaction({from : web3.eth.accounts[0],gas: 4700000} );
document.getElementById("console").value += "The probability that the file is malicious: "+contractInstance.getProb.call()+"\n";
}