-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from Honeybrain/reconfig
Reconfig
- Loading branch information
Showing
7 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { IsString } from "class-validator"; | ||
|
||
export class ReConfigDto { | ||
|
||
@IsString() | ||
config: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
syntax = "proto3"; | ||
|
||
package reconfigure; | ||
|
||
message ReconfigRequest { | ||
string config = 1; | ||
} | ||
|
||
message ReconfigReply { | ||
} | ||
|
||
service Reconfigure { | ||
rpc ReconfigHoneypot(ReconfigRequest) returns (ReconfigReply); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { Controller } from '@nestjs/common'; | ||
import { ReConfigDto } from './_utils/dto/request/reconfig-request.dto'; | ||
import { GrpcMethod } from '@nestjs/microservices'; | ||
import { ReconfigureService } from './reconfig.service'; | ||
|
||
@Controller('reconfigure') | ||
@ApiTags('Reconfigure') | ||
export class ReconfigureController { | ||
constructor(private reconfigureService: ReconfigureService) {} | ||
|
||
@GrpcMethod('Reconfigure', 'ReconfigHoneypot') | ||
reconfigHoneypot(ReconfigData: ReConfigDto) { | ||
return this.reconfigureService.reconfigHoneypot(ReconfigData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ReconfigureService } from './reconfig.service'; | ||
import { ReconfigureController } from './reconfig.controller'; | ||
|
||
@Module({ | ||
controllers: [ReconfigureController], | ||
providers: [ReconfigureService], | ||
}) | ||
export class ReconfigureModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Subject } from 'rxjs'; | ||
import { ReConfigDto } from './_utils/dto/request/reconfig-request.dto'; | ||
import * as Docker from 'dockerode'; | ||
import { RpcException } from '@nestjs/microservices'; | ||
|
||
interface dummies { | ||
num_services: number; | ||
ip_addresses: Array<string>; | ||
} | ||
|
||
interface ftps { | ||
ip_address: string; | ||
port: string; | ||
} | ||
|
||
interface NewConfig { | ||
dummy_pc: dummies; | ||
ftp: ftps; | ||
} | ||
|
||
function delay(ms: number) { | ||
return new Promise( resolve => setTimeout(resolve, ms) ); | ||
} | ||
|
||
@Injectable() | ||
export class ReconfigureService { | ||
|
||
private docker = new Docker(); | ||
|
||
/*private validate_config(configJson: NewConfig) { | ||
if (configJson.dummy_pc.num_services = 0) | ||
return | ||
}*/ | ||
|
||
public async reconfigHoneypot(Reconfig: ReConfigDto) { | ||
//check config | ||
if (!Reconfig.config) throw new RpcException('Config cannot be empty') | ||
|
||
//recup liste dockers | ||
const containers = await this.docker.listContainers({ all: true }); | ||
const buildContainers = containers.filter((container) => container.Names[0].startsWith('/honeypot_')); | ||
|
||
buildContainers.forEach(element => { | ||
this.docker.getContainer(element.Id).stop().catch((e) => console.log(e)); | ||
}); | ||
|
||
//creer docker depuis config | ||
let configJson: NewConfig = JSON.parse(Reconfig.config); | ||
|
||
//this.validate_config(configJson); | ||
await delay(1000) | ||
var newDocker = new Docker() | ||
//newDocker.createContainer({Image: 'ubuntu', Cmd: ['/bin/bash'], name: 'honeypot_dummy_pc_123'}, function(err, container){container?.start({})}) | ||
for (let i=0; i < configJson.dummy_pc.num_services; i++) { | ||
newDocker.run('ubuntu', ['bash', '-c', 'sleep infinity'], process.stdout, {Hostname: configJson.dummy_pc.ip_addresses[i], name:'honeypot_dummy_pc_'+i},function() { | ||
console.log("running new dummy pc on " + configJson.dummy_pc.ip_addresses[i]); | ||
}); | ||
} | ||
if (configJson.ftp.ip_address != "") { | ||
newDocker.run('ubuntu', ['bash', '-c', 'sleep infinity'], process.stdout, {Hostname: configJson.ftp.ip_address, name:'honeypot_dummy_ftp_1 '},function() { | ||
console.log("running new dummy ftp on " + configJson.ftp.ip_address + " on port " + configJson.ftp.port); | ||
}) | ||
} | ||
} | ||
} |