📜 ⬆️ ⬇️

Honeypot- logger on nodejs and tcpdump

Hello. Recently Comrade R_Voland talked about his http hanipot. He inspired me to write this post. But in this case, we will catch all tcp and udp scans, not just http. We will catch requests by means of tcpdump.

For tcp, we only catch syn packages:

tcpdump -n "tcp[tcpflags] & (tcp-syn) != 0" 

For udp all incoming udp packets
')
  tcpdump -n inbound and udp 

In theory, the output from tcpdump can be redirected to a file and then parsed as necessary, but I'm still the pervert, so we will write a service to nodejs, which will listen to tcpdump and save the results in a mysql database.

Script header with everything you need:

 #!/usr/bin/nodejs 'use strict'; const net = require('net'); const spawn = require('child_process').spawn; const mysql = require('mysql2'); const config = require('./config'); const connection = mysql.createConnection(config.mysql); const tcpdump = spawn('tcpdump', ['-n', 'tcp[tcpflags] & (tcp-syn) != 0']); const excludePorts = [ 80 ]; const excludeAddrs = [ '127.0.0.1', ]; let lastTcpLine = ''; 

lastTcpLine - needed for temporary storage of the last line obtained from stdout. Because we receive data not line by line, but by blocks in which the last line may not be complete, and its 2nd half will arrive with the second data block.

we need to excludePorts and excludeAddrs to exclude any of our own connections. On port 80 we will have a separate extended logger, we will not listen to localhost either.

Hang the listener:

 tcpdump.stdout.on('data', (data) => { let lines = `${data}`.split('\n'); //      //               lastTcpLine ,        lastTcpLine let lastTcpLineNum = lines.length - 1; let toNum = lines.length - 1; lines[ 0 ] = lines[ 0 ] + lastTcpLine; if( lines[ lastTcpLineNum ].indexOf('\n') == -1 ) { lastTcpLine = lines[ lastTcpLineNum ]; toNum --; } else lastTcpLine = ''; for(let i=0; i<=toNum; i++) { saveLog( parseLine(lines[ i ], 'tcp') ); //       } }); 

IP come to us in the form of address.port, for example 192.168.1.1.443, we parse into the address and port:

 function parseLine(line, proto) { let parts = line.split(' '); //     let dstAddrParts = parseIP(parts[ 4 ]); //   ,    let srcAddrParts = parseIP(parts[ 2 ]); //   return { addr: srcAddrParts.addr, port: dstAddrParts.port, proto: proto, req_time: parseInt(new Date() / 1000), }; } function parseIP(ipStr) { let addrParts = ipStr.split('.'); //    ,   let port = addrParts[ addrParts.length - 1 ]; //   -   let ipOctets = []; //      for(let i=0; i<=(addrParts.length-2); i++) ipOctets.push(addrParts[ i ]); let addr = ipOctets.join('.'); if( !net.isIP(addr) ) // ,    ,   ,  -  ,  null   addr = null; return { addr: addr, port: parseInt(port) }; } 

Save the result to the database:

 function saveLog(info) { if( excludePorts.indexOf(info.port) > -1 ) //     -    return; if( excludeAddrs.indexOf(info.addr) > -1 ) //     -    return; for(let key in info) //  -     -    if( !info[ key ] ) { console.log('Bad info:', info); return; } let fields = []; //    for(let key in info) fields.push('`' + key + '`'); let values = []; //   for(let key in info) { if( typeof(info[ key ]) == 'number' ) values.push(info[ key ]); else values.push(`'` + info[ key ] + `'`); } let query = 'INSERT INTO access_logs (' + fields.join(',') + ') VALUES(' + values.join(',') + ')'; //       connection.query(query); } 

The code for the udp listener is 100% complete, I will not repeat, the source code can be viewed on the github: github.com/hololoev/honeypot_tcpdump_logger.git

Now we need a new virtual box on which we put nginx with the “Under construction” stub and our logger. We do not send any domain to it, in every way we depict the view of the newly created server, the average webmaster. In a couple of days (from May 5 to 9) we get the results:

Total adressestcp scansudp scanshttp Scans
432438558543101


Top 5 TCP ports:
tcp portScans
4452538
231515
221304
3306151
3389148


Top 5 udp ports:
udp portScans
506095
16141
190032
123thirty
13723


Top 10 most active ip addresses:
AddressTotal scansLocationhttptcpudp
40.115.124.12725822IE / Dublin0258220
77.72.82.101861RU /08610
77.72.82.22760RU /07600
145.239.134.1550GB /05500
101.128.72.140282ID / Jakarta02820
77.72.85.25244RU /02440
181.214.87.34208US / Las Vegas02080
5.188.11.91189RU / Saint Petersburg01890
128.199.141.239173SG / Singapore01730
5.188.11.79156RU / Saint Petersburg01560


Map with top 100 most active addresses:

Source: https://habr.com/ru/post/358214/


All Articles