Skip to content

Commit

Permalink
update logs
Browse files Browse the repository at this point in the history
  • Loading branch information
netcode committed Mar 18, 2024
1 parent 40d1fef commit 6607fff
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 41 deletions.
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
FROM node:16
FROM node:18

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies

# RUN npm install -g nodemon

# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
Expand All @@ -17,4 +20,5 @@ COPY . .

RUN chmod +x ./bin/hash-honeypot

ENTRYPOINT [ "./bin/hash-honeypot" ]
#ENTRYPOINT [ "./bin/hash-honeypot", "help" ]
CMD ["sleep", "infinity"]
3 changes: 3 additions & 0 deletions bin/hash-honeypot-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

nodemon ../index.js
4 changes: 3 additions & 1 deletion cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const run = (appFolder, options) => {
app.config = config;
app.config.options = options; //add the cli options to config

app.logger = newLogger(app.config);
const log = newLogger(app.config);
app.logger = log.logger
app.tracer = log.tracer

app.logger.info('App -> Starting HASH ');

Expand Down
9 changes: 6 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ services:
- DD_APM_NON_LOCAL_TRAFFIC=true
- DD_API_KEY=${DD_API_KEY}
- DD_SITE=${DD_SITE:-datadoghq.com}
- DD_ENV=prod
default:
- DD_ENV=dev
hash-honeypot:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/usr/src/app
environment:
- DD_AGENT_HOST=agent
- DD_TRACE_AGENT_PORT=8126
- DD_TRACER_ENABLED=true
- DD_APPSEC_ENABLED=true
- DD_API_KEY=${DD_API_KEY}
- DD_SERVICE=${DD_SERVICE:-sec-research}
- DD_TRACE_DEBUG=false
- DD_ENV=prod
- DD_ENV=dev
tty: true
depends_on:
agent:
Expand Down
60 changes: 36 additions & 24 deletions libs/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ module.exports = (app) => {
const exp = express();

//generate an app key
//const randomAppKey = crypto.createHash('md5').update(text).digest('hex')

const randomAppKey = crypto.randomBytes(32).toString('hex');

app.logger.info(
Expand Down Expand Up @@ -54,32 +52,46 @@ module.exports = (app) => {

app.logger.info('Init -> Configure datadog logger');
const middlewareLogger = function (req, res, next) {

exp.logger = (id, title, info) => {
app.logger.warn(
'HASH: ' + req.method + ' ' + req.originalUrl + ': ' + title,
{
type: 'malicious',
templateId: id,
info,
http: {
client_ip: req.ip,
host: req.headers.host,
method: req.method,
path: req.path,
},
request: {
query: req.query || {},
params: req.params || {},
body: req.body || {},
headers: {
...req.headers,
...{
cookie_parsed: req.cookies,
},
const payload = {
type: 'malicious',
templateId: id,
info,
http: {
client_ip: req.ip,
host: req.headers.host,
method: req.method,
path: req.path,
},
request: {
query: req.query || {},
params: req.params || {},
body: req.body || {},
headers: {
...req.headers,
...{
cookie_parsed: req.cookies,
},
},
}
},
}
app.logger.warn(
'HASH: ' + req.method + ' ' + req.originalUrl + ': ' + title,
payload
);


// const span = app.tracer.scope().active()
// span.setTag("http.body", payload.request.body)
// span.setTag("http.info", payload.http)
// span.setTag("http.query", payload.query)

app.tracer.appsec.trackCustomEvent('malicious.trap', {
type: 'malicious',
templateId: id
})

};
next();
};
Expand Down
16 changes: 14 additions & 2 deletions libs/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const MAX_FILE_SIZE = 1000000;
const MAX_FILES = 100;

module.exports.newLogger = (config) => {
let tracer = null; //init status in case of datadog is not enabled
const availableTransports = {
console: () => {
return new winston.transports.Console({
Expand Down Expand Up @@ -42,12 +43,23 @@ module.exports.newLogger = (config) => {
return false;
}

require('dd-trace').init({
tracer = require('dd-trace').init({
appsec: true,
logInjection: true,
service: datadogServiceName,
});

tracer.use('express', {
// hook will be executed right before the request span is finished
hooks: {
request: (span, req, res) => {
span.setTag("http.body", req.body)
span.setTag("http.query", req.query)
span.setTag("http.full_headers", req.headers)
}
}
})

const params = new URLSearchParams({
"dd-api-key": datadogApiKey,
"ddsource": "nodejs",
Expand Down Expand Up @@ -89,5 +101,5 @@ module.exports.newLogger = (config) => {
);
}
}
return logger;
return { logger, tracer };
};
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"clui": "^0.3.6",
"commander": "^11.0.0",
"cookie-parser": "^1.4.6",
"dd-trace": "^3.9.3",
"dd-trace": "^5.6.0",
"debug": "^4.3.4",
"dotenv": "^16.0.3",
"express": "^4.18.1",
Expand Down

0 comments on commit 6607fff

Please sign in to comment.