Advertisement
Guest User

Untitled

a guest
Feb 4th, 2016
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. const collector = require('./netflowv9.js');
  2. const netmask = require('netmask').Netmask;
  3. const mysql = require('mysql');
  4. const connection = mysql.createConnection({
  5. host:'localhost',
  6. user:'root',
  7. password:'',
  8. database:'nat'
  9. });
  10.  
  11. const blocks = [
  12. // list subnets that you wish to log translations..
  13. new netmask('100.64.200.0/24'),
  14. ];
  15.  
  16. function logTranslation(unixTime, lanSrcAddr, lanSrcPort,
  17. postNatSrcAddr, postNatSrcPort, dstAddr, dstPort) {
  18. var query = "INSERT INTO nat_translations VALUES (" + unixTime
  19. + ",INET_ATON('" + lanSrcAddr + "')," + lanSrcPort + ",INET_ATON('"
  20. + postNatSrcAddr + "')," + postNatSrcPort + ",INET_ATON('" + dstAddr
  21. + "')," + dstPort + ")";
  22. connection.query(query, function(err, rows, fields) {
  23. if (err) {
  24. console.error('error logging translation: ' + err.stack);
  25. }
  26. });
  27. }
  28.  
  29. collector(function(flowrecord) {
  30. var unixTime = flowrecord['header']['seconds'];
  31. var flows = flowrecord['flows'];
  32. for (var flow in flows) {
  33. var f = flows[flow];
  34. if (f['protocol'] != 6) {
  35. // only log TCP translations
  36. continue;
  37. }
  38. var src = f['ipv4_src_addr'];
  39. var isNat = false;
  40. for (var block in blocks) {
  41. if (blocks[block].contains(src)) {
  42. isNat = true;
  43. break;
  44. }
  45. }
  46. if (!isNat) {
  47. continue;
  48. }
  49. var natSrcAddr = f['postNATSourceIPv4Address'];
  50. if (src == natSrcAddr) {
  51. // when internal IP matches natted IP (anomaly I have not figured out)
  52. continue;
  53. }
  54. var dst = f['ipv4_dst_addr'];
  55. var dstPort = f['l4_dst_port'];
  56. var srcPort = f['l4_src_port'];
  57. var natSrcPort = f['postNAPTSourceTransportPort'];
  58. logTranslation(unixTime, src, srcPort, natSrcAddr, natSrcPort, dst, dstPort);
  59. //console.log(f)
  60. }
  61. }).listen(3241);
  62.  
  63. process.on('SIGINT', function() {
  64. connection.end();
  65. process.exit();
  66. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement