fullhostscan.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /*
  3. * networks fast scan with nmap
  4. */
  5. if (ubRouting::get('action') == 'fullhostscan') {
  6. $fullScanResult = '';
  7. if (!ubRouting::checkGet('nn')) {
  8. $nmapPath = $alterconf['NMAP_PATH'];
  9. $allMultinetNetworks_q = "select * from `networks`";
  10. $allMultinetNetworks = simple_queryall($allMultinetNetworks_q);
  11. if (!empty($allMultinetNetworks)) {
  12. foreach ($allMultinetNetworks as $ig => $eachsubnet) {
  13. $nmapCommand = $nmapPath . ' -sP -n ' . $eachsubnet['desc'];
  14. $fullScanResult .= shell_exec($nmapCommand);
  15. print($eachsubnet['desc'] . ' :' . date("Y-m-d H:i:s") . ':SCANNED' . "\n");
  16. }
  17. }
  18. }
  19. //additional parameters
  20. if (ubRouting::checkGet('param')) {
  21. if (ubRouting::get('param') == 'traffdiff') {
  22. $fullScanResult .= '== Traffic analysis diff here ==' . "\n";
  23. $traff_q = "SELECT `login`,`IP`, (`U0`+`U1`+`U2`+`U3`+`U4`+`U5`+`U6`+`U7`+`U8`+`U9`) as `traff` from `users`";
  24. $curTraff = simple_queryall($traff_q);
  25. $prevTraff = array();
  26. $diffCurr = array();
  27. $diffPrev = array();
  28. //mixing ishimura aggregated traffic
  29. $ishimuraOption = MultiGen::OPTION_ISHIMURA;
  30. if ($alterconf[$ishimuraOption]) {
  31. $ishimuraTable = MultiGen::NAS_ISHIMURA;
  32. $additionalTraffic = array();
  33. $query_hideki = "SELECT `login`,`D0`,`U0` from `" . $ishimuraTable . "` WHERE `month`='" . date("n") . "' AND `year`='" . curyear() . "'";
  34. $dataHideki = simple_queryall($query_hideki);
  35. if (!empty($dataHideki)) {
  36. foreach ($dataHideki as $io => $each) {
  37. $additionalTraffic[$each['login']] = $each['D0'] + $each['U0'];
  38. }
  39. }
  40. if (!empty($curTraff) AND !empty($additionalTraffic)) {
  41. foreach ($curTraff as $io => $each) {
  42. if (isset($additionalTraffic[$each['login']])) {
  43. $curTraff[$io]['traff'] += $additionalTraffic[$each['login']];
  44. }
  45. }
  46. }
  47. }
  48. //mixing ophanim traffic data
  49. if ($alterconf[OphanimFlow::OPTION_ENABLED]) {
  50. $ophTraff = new OphanimFlow();
  51. $additionalTraffic = $ophTraff->getAllUsersAggrTraff();
  52. if (!empty($curTraff) AND !empty($additionalTraffic)) {
  53. foreach ($curTraff as $io => $each) {
  54. if (isset($additionalTraffic[$each['login']])) {
  55. $curTraff[$io]['traff'] += $additionalTraffic[$each['login']];
  56. }
  57. }
  58. }
  59. }
  60. if (!file_exists('exports/prevtraff')) {
  61. $prevTraff = $curTraff;
  62. $savePrev = serialize($prevTraff);
  63. file_put_contents('exports/prevtraff', $savePrev);
  64. } else {
  65. $prevTraff_raw = file_get_contents('exports/prevtraff');
  66. $prevTraff = unserialize($prevTraff_raw);
  67. }
  68. //filling diff arrays
  69. if (!empty($curTraff)) {
  70. foreach ($curTraff as $itc => $eachdiff) {
  71. $diffCurr[$eachdiff['login']]['IP'] = $eachdiff['IP'];
  72. $diffCurr[$eachdiff['login']]['traff'] = $eachdiff['traff'];
  73. }
  74. }
  75. if (!empty($prevTraff)) {
  76. foreach ($prevTraff as $itp => $eachprev) {
  77. $diffPrev[$eachprev['login']]['IP'] = $eachprev['IP'];
  78. $diffPrev[$eachprev['login']]['traff'] = $eachprev['traff'];
  79. }
  80. }
  81. //comparing arrays
  82. if (!empty($diffCurr)) {
  83. foreach ($diffCurr as $diffLogin => $diffData) {
  84. if (isset($diffPrev[$diffLogin])) {
  85. if ($diffData['traff'] != $diffPrev[$diffLogin]['traff']) {
  86. $fullScanResult .= 'login ' . $diffLogin . ' ' . $diffData['IP'] . ' looks like alive' . "\n";
  87. }
  88. }
  89. }
  90. }
  91. //writing to cache
  92. $savePrev = serialize($curTraff);
  93. file_put_contents('exports/prevtraff', $savePrev);
  94. }
  95. }
  96. //saving scan data
  97. file_put_contents('exports/nmaphostscan', $fullScanResult);
  98. //postprocessing DN data
  99. if ($alterconf['DN_FULLHOSTSCAN']) {
  100. $activeIps = array();
  101. if (file_exists("exports/nmaphostscan")) {
  102. $nmapData = file_get_contents("exports/nmaphostscan");
  103. $nmapData = explodeRows($nmapData);
  104. if (!empty($nmapData)) {
  105. foreach ($nmapData as $ic => $eachnmaphost) {
  106. $zhost = zb_ExtractIpAddress($eachnmaphost);
  107. if ($zhost) {
  108. $activeIps[$zhost] = $zhost;
  109. }
  110. }
  111. }
  112. }
  113. //renew DN data
  114. if (file_exists(DATA_PATH . "dn")) {
  115. //directory clanup
  116. $oldDnData = rcms_scandir(DATA_PATH . 'dn/');
  117. if (!empty($oldDnData)) {
  118. foreach ($oldDnData as $deleteFile) {
  119. unlink(DATA_PATH . 'dn/' . $deleteFile);
  120. }
  121. }
  122. //store new DN data
  123. if (!empty($activeIps)) {
  124. $allUserIps = zb_UserGetAllIPs();
  125. $allUserIps = array_flip($allUserIps);
  126. foreach ($activeIps as $ix => $aip) {
  127. if (isset($allUserIps[$aip])) {
  128. file_put_contents(DATA_PATH . 'dn/' . $allUserIps[$aip], 'alive');
  129. }
  130. }
  131. }
  132. //put some timeline data
  133. $dnTimelineData = curdatetime() . ';' . sizeof($activeIps) . PHP_EOL;
  134. file_put_contents('content/documents/dntimeline.log', $dnTimelineData, FILE_APPEND);
  135. } else {
  136. die('FAIL:NO_CONTENT_DN_EXISTS');
  137. }
  138. }
  139. //updating build users state cache
  140. if ($alterconf['SWYMAP_ENABLED']) {
  141. $updateBuilCache = um_MapDrawBuilds();
  142. print('OK:USERBUILDCACHE');
  143. }
  144. die('OK:FULLHOSTSCAN');
  145. }