1
0

network_tools.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2017 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. include 'util_config.php';
  25. session_name($sessionName);
  26. session_start();
  27. if(!empty($_SESSION['user_id'])){
  28. // This entire section is nothing but a big, messy, workaround.
  29. // Make ogpLang happy.
  30. $_REQUEST['m'] = 'util';
  31. // We need to change directory to be able to include lib_remote and make a database connection without any errors
  32. // This is becasue the following files include other files via their relative path rather than absolute path... could be fixed by editing them... but until then, this is just a hacky workaround.
  33. $cwd = getcwd();
  34. if(chdir('../../') === true){
  35. require_once('includes/config.inc.php');
  36. require_once('includes/functions.php');
  37. require_once('includes/helpers.php');
  38. include_once("includes/lang.php");
  39. ogpLang();
  40. require_once('includes/lib_remote.php');
  41. $db = createDatabaseConnection($db_type, $db_host, $db_user, $db_pass, $db_name, $table_prefix);
  42. }else{
  43. die(get_lang('chdir_failed'));
  44. }
  45. if(chdir($cwd) === false){
  46. die(get_lang('chdir_failed'));
  47. }
  48. // Actual script functions now.
  49. $userInfo = $db->getUserById($_SESSION['user_id']);
  50. $userRole = $userInfo['users_role'];
  51. $command = trim($_POST['command']);
  52. $target = trim($_POST['remote_target']);
  53. // Check if the specified agent exists. If it does, assign it to $servers. Otherwise, return that it's an invalid agent.
  54. if(($server = $db->getRemoteServerById($_POST['agent'])) === false){
  55. die(get_lang('agent_invalid'));
  56. }
  57. $remote = new OGPRemoteLibrary($server['agent_ip'], $server['agent_port'], $server['encryption_key'], 60);
  58. if($remote->status_chk() === 0){
  59. echo get_lang('networktools_agent_offline');
  60. }elseif(empty($target)){
  61. echo get_lang('target_empty');
  62. }elseif(empty($command)){
  63. echo get_lang('command_empty');
  64. }else{
  65. $os = preg_match("/CYGWIN/", $remote->what_os()) ? 'windows' : 'linux';
  66. // Loop over $availableCommands from util_config.php
  67. // Assign a variable, $allowAccess based on the current user's role and if the config file states the user's role is allowed access to this command.
  68. for($x = 0; $x < count($availableCommands); ++$x){
  69. if($availableCommands[$x]['title'] == $command){
  70. $command = $availableCommands[$x][$os];
  71. $allowAccess = $availableCommands[$x][$userRole];
  72. }
  73. }
  74. if(isset($allowAccess) && $allowAccess === true){
  75. // Check the command is available to us. If it's not, echo command_unavilable
  76. $which = $remote->exec('which '.$command);
  77. if(empty($which)){
  78. echo get_lang('command_unavilable');
  79. }else{
  80. // Not completely necessary - gethostbyaddr(gethostbyname()) will return false if it's anything that's not valid.
  81. // This is mostly for logging attempted arbitrary commands.
  82. if(strpbrk($target, $blockedCharacters)){
  83. if($logMaliciousUsage){
  84. $db->logger(get_lang_f('command_bad_characters', $command, htmlentities($target)));
  85. }
  86. echo get_lang('command_hacking_attempt');
  87. }else{
  88. $target = gethostbyaddr(gethostbyname($target));
  89. if(!$target){
  90. echo get_lang('target_invalid');
  91. }else{
  92. $exec = $remote->exec($command.' '.$target);
  93. echo ($exec === null) ? get_lang('exec_failed') : htmlentities(trim($exec));
  94. if($logAllUsage){
  95. $db->logger(get_lang_f('command_executed', $command, htmlentities($target)));
  96. }
  97. }
  98. }
  99. }
  100. }else{
  101. // If the user isn't allowed access but they've somehow got this far then they've changed the value="" attr.
  102. // return with command_no_permissions and log the event.
  103. echo get_lang('command_no_access');
  104. if($logMaliciousUsage){
  105. $db->logger(get_lang_f('command_no_permissions', $command, htmlentities($target)));
  106. }
  107. } //else allowAccess
  108. } // else status_chk / empty target / empty command
  109. }else{//_SESSION check.
  110. header('HTTP/1.0 403 Forbidden');
  111. exit;
  112. }
  113. ?>