network_tools.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 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. require 'includes/lib_remote.php';
  25. function exec_ogp_module()
  26. {
  27. global $db;
  28. include 'modules/util/util_config.php';
  29. $userInfo = $db->getUserById($_SESSION['user_id']);
  30. $userRole = $userInfo['users_role'];
  31. $command = trim($_POST['command']);
  32. $target = trim($_POST['remote_target']);
  33. // Check if the specified agent exists. If it does, assign it to $servers. Otherwise, return that it's an invalid agent.
  34. if(($server = $db->getRemoteServerById($_POST['agent'])) === false){
  35. die(get_lang('agent_invalid'));
  36. }
  37. $remote = new OGPRemoteLibrary($server['agent_ip'], $server['agent_port'], $server['encryption_key'], 60);
  38. if($remote->status_chk() === 0){
  39. echo get_lang('networktools_agent_offline');
  40. }elseif(empty($target)){
  41. echo get_lang('target_empty');
  42. }elseif(empty($command)){
  43. echo get_lang('command_empty');
  44. }else{
  45. $os = preg_match("/CYGWIN/", $remote->what_os()) ? 'windows' : 'linux';
  46. // Loop over $availableCommands from util_config.php
  47. // 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.
  48. for($x = 0; $x < count($availableCommands); ++$x){
  49. if($availableCommands[$x]['title'] == $command){
  50. $command = $availableCommands[$x][$os];
  51. $allowAccess = $availableCommands[$x][$userRole];
  52. }
  53. }
  54. if(isset($allowAccess) && $allowAccess === true){
  55. // Check the command is available to us. If it's not, echo command_unavilable
  56. $which = $remote->exec('which '.$command);
  57. if(empty($which)){
  58. echo get_lang('command_unavilable');
  59. }else{
  60. // Not completely necessary - gethostbyaddr(gethostbyname()) will return false if it's anything that's not valid.
  61. // This is mostly for logging attempted arbitrary commands.
  62. if(strpbrk($target, $blockedCharacters)){
  63. if($logMaliciousUsage){
  64. $db->logger(get_lang_f('command_bad_characters', $command, htmlentities($target)));
  65. }
  66. echo get_lang('command_hacking_attempt');
  67. }else{
  68. $target = gethostbyaddr(gethostbyname($target));
  69. if(!$target){
  70. echo get_lang('target_invalid');
  71. }else{
  72. $exec = $remote->exec($command.' '.$target);
  73. echo ($exec === null) ? get_lang('exec_failed') : htmlentities(trim($exec));
  74. if($logAllUsage){
  75. $db->logger(get_lang_f('command_executed', $command, htmlentities($target)));
  76. }
  77. }
  78. }
  79. }
  80. }else{
  81. // If the user isn't allowed access but they've somehow got this far then they've changed the value="" attr.
  82. // return with command_no_permissions and log the event.
  83. echo get_lang('command_no_access');
  84. if($logMaliciousUsage){
  85. $db->logger(get_lang_f('command_no_permissions', $command, htmlentities($target)));
  86. }
  87. } //else allowAccess
  88. }
  89. }
  90. ?>