fullhostscan.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. //demo mode - random users will be alive
  97. if (file_exists('DEMO_MODE')) {
  98. $allUsers = zb_UserGetAllDataCache();
  99. if (!empty($allUsers)) {
  100. foreach ($allUsers as $io => $each) {
  101. if ($each['Cash'] >= '-' . $each['Credit']) {
  102. $seed = rand(0, 8);
  103. if ($seed) {
  104. $fullScanResult .= 'login ' . $each['login'] . ' ' . $each['ip'] . ' demo mode alive' . "\n";
  105. }
  106. }
  107. }
  108. }
  109. }
  110. //saving scan data
  111. file_put_contents('exports/nmaphostscan', $fullScanResult);
  112. //postprocessing DN data
  113. if ($alterconf['DN_FULLHOSTSCAN']) {
  114. $activeIps = array();
  115. if (file_exists("exports/nmaphostscan")) {
  116. $nmapData = file_get_contents("exports/nmaphostscan");
  117. $nmapData = explodeRows($nmapData);
  118. if (!empty($nmapData)) {
  119. foreach ($nmapData as $ic => $eachnmaphost) {
  120. $zhost = zb_ExtractIpAddress($eachnmaphost);
  121. if ($zhost) {
  122. $activeIps[$zhost] = $zhost;
  123. }
  124. }
  125. }
  126. }
  127. //renew DN data
  128. if (file_exists(DATA_PATH . "dn")) {
  129. //directory clanup
  130. $oldDnData = rcms_scandir(DATA_PATH . 'dn/');
  131. if (!empty($oldDnData)) {
  132. foreach ($oldDnData as $deleteFile) {
  133. unlink(DATA_PATH . 'dn/' . $deleteFile);
  134. }
  135. }
  136. //store new DN data
  137. if (!empty($activeIps)) {
  138. $allUserIps = zb_UserGetAllIPs();
  139. $allUserIps = array_flip($allUserIps);
  140. foreach ($activeIps as $ix => $aip) {
  141. if (isset($allUserIps[$aip])) {
  142. file_put_contents(DATA_PATH . 'dn/' . $allUserIps[$aip], 'alive');
  143. }
  144. }
  145. }
  146. //put some timeline data
  147. $dnTimelineData = curdatetime() . ';' . sizeof($activeIps) . PHP_EOL;
  148. file_put_contents('content/documents/dntimeline.log', $dnTimelineData, FILE_APPEND);
  149. } else {
  150. die('FAIL:NO_CONTENT_DN_EXISTS');
  151. }
  152. }
  153. //updating build users state cache
  154. if ($alterconf['SWYMAP_ENABLED']) {
  155. $updateBuilCache = um_MapDrawBuilds();
  156. print('OK:USERBUILDCACHE');
  157. }
  158. die('OK:FULLHOSTSCAN');
  159. }