Skip to content

Commit

Permalink
Merge pull request #43 from Honeybrain/reconfig
Browse files Browse the repository at this point in the history
Reconfig
  • Loading branch information
valentinbreiz authored Mar 20, 2024
2 parents 45d3a9a + 1f3ba47 commit 642533f
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { EnvironmentVariables, validateEnv } from './_utils/config/config';
import { HelloworldModule } from './helloworld/helloworld.module';
import { LogsModule } from './logs/logs.module';
import { DashboardModule } from './dashboard/dashboard.module';
import { ContainersModule } from './containers/containers.module';
Expand All @@ -13,6 +12,7 @@ import { MailsModule } from './mails/mails.module';
import { RulesModule } from './rules/rules.module';
import { MobileModule } from './mobile/mobile.module';
import { HistoryModule } from './history/history.module';
import { ReconfigureModule } from './reconfig/reconfig.module';

@Module({
imports: [
Expand All @@ -35,6 +35,7 @@ import { HistoryModule } from './history/history.module';
RulesModule,
MobileModule,
HistoryModule,
ReconfigureModule,
],
})
export class AppModule {}
3 changes: 2 additions & 1 deletion src/grpc-client.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const grpcClientOptions: ClientOptions = {
transport: Transport.GRPC,
options: {
url: process.env.GRPC_URL,
package: ['logs', 'dashboard', 'containers', 'blacklist', 'user', 'rules', 'mobile'], // proto package name
package: ['logs', 'dashboard', 'containers', 'blacklist', 'user', 'rules', 'mobile', 'reconfigure'], // proto package name
protoPath: [
'./logs/_utils/logs.proto',
'./dashboard/_utils/dashboard.proto',
Expand All @@ -14,6 +14,7 @@ export const grpcClientOptions: ClientOptions = {
'./user/_utils/user.proto',
'./rules/_utils/rules.proto',
'./mobile/_utils/mobile.proto',
'./reconfig/_utils/reconfig.proto',
].map((x) => join(__dirname, x)), // proto file path
},
};
7 changes: 7 additions & 0 deletions src/reconfig/_utils/dto/request/reconfig-request.dto.ts
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;
}
14 changes: 14 additions & 0 deletions src/reconfig/_utils/reconfig.proto
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);
}
16 changes: 16 additions & 0 deletions src/reconfig/reconfig.controller.ts
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);
}
}
9 changes: 9 additions & 0 deletions src/reconfig/reconfig.module.ts
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 {}
67 changes: 67 additions & 0 deletions src/reconfig/reconfig.service.ts
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);
})
}
}
}

0 comments on commit 642533f

Please sign in to comment.