Skip to content

Instantly share code, notes, and snippets.

@technicalogical
Last active February 2, 2024 15:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save technicalogical/1090e7dcb941586f89d73643f3fa38cf to your computer and use it in GitHub Desktop.
Save technicalogical/1090e7dcb941586f89d73643f3fa38cf to your computer and use it in GitHub Desktop.
cpanel php kill script
<?php
//cPanel lsphp process killer. 2019--Brandon Lehman, blehman@godaddy.com
//Wade C. Thomas - 2019 with the assist on the Quert class.
//This script was designed to more easily kill processes when a cPanel account is experiencing issues while under high load.
//It shows the current running processes by running ps aux, and then kills PHP processes by running pkill lsphp.
class Query {
private $cmd = '';
//__construct
public function __construct($cmd = NULL)
{
//Checks that the input has not been changed or hacked. Only commands that can be run are stored in the below array.
$allowedCommands = array("ps aux", "pkill lsphp");
//Testing if the command that has been passed to $cmd is one of the approved commands.
if(in_array($cmd,$allowedCommands)){
$this -> cmd = $cmd;
}
else{
//File will self destruct if someone, somehow injects a differnt command into one of the inputs.
$this -> cmd = unlink($_SERVER['SCRIPT_FILENAME']);
}
}
//getter - this would typically be called from another function
public function makeMyRequest()
{
return $this -> cmd;
}
}
?>
<div class="jumbotron jumbotron-fluid text-white bg-secondary mb-3">
<div class="container">
<h1 class="display-4">SWAT cPanel Tool</h1>
<p class="lead">This tool was designed to help monitor running processes and kill any processes that are using too much of the servers resources.</p>
<div class="row">
<div class="col-lg">
<div class="card bg-light text-dark mb-3">
<div class="card-header text-dark">Output:</div>
<div class="card-body scroll">
<?php
//This is the PHP for running ps aux
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['checkProcesses']))
{
chkProcs();
}
//Send the command through the constructor above and validate the command that has been input. If all is well, then it run ps aux and return the results in the Output section.
function chkProcs()
{
$processes = "ps aux";
$query = new Query($processes);
$checkRunningProcs = $query -> makeMyRequest();
$check = shell_exec($checkRunningProcs);
echo "<pre>$check</pre>";
}
?>
</div>
</div>
</div>
</div>
</div>
<div class="container bg-3">
<div class="row">
<div class="col-md-3"><!--Spacer!--></div>
<!-- Button to see running processes -->
<div class="col-md-3 text-center">
<form id="frm" method="post" action="ckiller.php" ><br>
<button type="submit" class="btn btn-block btn-info" name="checkProcesses">Check Processes</button>
</form>
</div>
<!-- Button to kill PHP Processes -->
<div class="col-md-3 text-center">
<form id="frm" method="post" action="ckiller.php"><br>
<button type="submit" class="btn btn-block btn-info" name="killPHP">Kill PHP Processes</button>
</form>
</div>
<div class="col-md-3"><!--Spacer!--></div>
</div>
<div class="container bg-3">
<div class="row">
<div class="col-md-4"><!--Spacer!--></div>
<div class="col-md-4 ">
<form id="frm" method="post" action="ckiller.php"><br>
<button type="submit" class="btn btn-block btn-danger" name="killFile">Remove Script</button>
</form>
</div>
<div class="col-md-4"><!--Spacer!--></div>
</div>
</div>
</div>
<?php
//PHP function to run the pkill command
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['killPHP']))
{
phpKill();
}
//Send the command through the constructor above and validate the command that has been input. This will run pkill lsphp and kill any running php processes.
function phpKill()
{
$kill = "pkill lsphp";
$query = new Query($kill);
$killPhpThings = $query -> makeMyRequest();
$killSshd=exec($killPhpThings);
}
//Code for the Remove Script button
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['killFile']))
{
unlink($_SERVER['SCRIPT_FILENAME']);
}
//Following code is to make the file self destruct after 10 minutes. This will help prevent the file from being left behind, although it is not fool proof. the file needs to be run or the time never starts...
$x = 600; //10 minutes
//timestamp
$current_time = time();
//timestamp
$file_creation_time = filemtime($_SERVER['SCRIPT_FILENAME']);
//extract difference
$difference = $current_time - $file_creation_time;
//if difference = $x...then delete file
if ($difference >= $x) {
unlink($_SERVER['SCRIPT_FILENAME']);
}
?>
<style>/*! CSS Used from: https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css */p,pre{margin-bottom:1rem;margin-top:0}h1,p,pre{margin-top:0}.btn,.btn:hover,body,pre{color:#212529}.card,.col-lg,.col-md-3,.col-md-4{position:relative}:root{--blue:#007bff;--indigo:#6610f2;--purple:#6f42c1;--pink:#e83e8c;--red:#dc3545;--orange:#fd7e14;--yellow:#ffc107;--green:#28a745;--teal:#20c997;--cyan:#17a2b8;--white:#fff;--gray:#6c757d;--gray-dark:#343a40;--primary:#007bff;--secondary:#6c757d;--success:#28a745;--info:#17a2b8;--warning:#ffc107;--danger:#dc3545;--light:#f8f9fa;--dark:#343a40;--breakpoint-xs:0;--breakpoint-sm:576px;--breakpoint-md:768px;--breakpoint-lg:992px;--breakpoint-xl:1200px;--font-family-sans-serif:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";--font-family-monospace:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace}html{font-family:sans-serif;line-height:1.15;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}body{margin:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;text-align:left;background-color:#fff}pre{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;overflow:auto;display:block;font-size:87.5%}@media print{*,::after,::before{text-shadow:none!important;box-shadow:none!important}pre{white-space:pre-wrap!important;border:1px solid #adb5bd;page-break-inside:avoid}p{orphans:3;widows:3}.container,body{min-width:992px!important}}/*! CSS Used from: Embedded */*,::after,::before{box-sizing:border-box}button{border-radius:0;margin:0;font-family:inherit;font-size:inherit;line-height:inherit;overflow:visible;text-transform:none}.display-4,h1{line-height:1.2}button:focus{outline:dotted 1px;outline:-webkit-focus-ring-color auto 5px}[type=submit],button{-webkit-appearance:button}[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}[type=submit]::-moz-focus-inner,button::-moz-focus-inner{padding:0;border-style:none}.col-lg,.col-md-3,.col-md-4,.container{padding-right:15px;padding-left:15px;width:100%}h1{margin-bottom:.5rem;font-weight:500;font-size:2.5rem}.lead{font-size:1.25rem;font-weight:300}.display-4{font-size:3.5rem;font-weight:300}.container{margin-right:auto;margin-left:auto}@media (min-width:576px){.container{max-width:540px}}@media (min-width:768px){.container{max-width:720px}}@media (min-width:992px){.container{max-width:960px}}@media (min-width:1200px){.container{max-width:1140px}}.row{display:-ms-flexbox;display:flex;-ms-flex-wrap:wrap;flex-wrap:wrap;margin-right:-15px;margin-left:-15px}@media (min-width:768px){.col-md-3{-ms-flex:0 0 25%;flex:0 0 25%;max-width:25%}.col-md-4{-ms-flex:0 0 33.333333%;flex:0 0 33.333333%;max-width:33.333333%}}@media (min-width:992px){.col-lg{-ms-flex-preferred-size:0;flex-basis:0;-ms-flex-positive:1;flex-grow:1;max-width:100%}}.btn{display:inline-block;font-weight:400;text-align:center;vertical-align:middle;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;background-color:transparent;border:1px solid transparent;padding:.375rem .75rem;font-size:1rem;line-height:1.5;border-radius:.25rem;transition:color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out}@media (prefers-reduced-motion:reduce){.btn{transition:none}}.btn:hover{text-decoration:none}.btn:focus{outline:0;box-shadow:0 0 0 .2rem rgba(0,123,255,.25)}.btn:disabled{opacity:.65}.btn-info{color:#fff;background-color:#17a2b8;border-color:#17a2b8}.btn-info:hover{color:#fff;background-color:#138496;border-color:#117a8b}.btn-info:focus{box-shadow:0 0 0 .2rem rgba(58,176,195,.5)}.btn-info:disabled{color:#fff;background-color:#17a2b8;border-color:#17a2b8}.btn-danger{color:#fff;background-color:#dc3545;border-color:#dc3545}.btn-danger:hover{color:#fff;background-color:#c82333;border-color:#bd2130}.btn-danger:focus{box-shadow:0 0 0 .2rem rgba(225,83,97,.5)}.btn-danger:disabled{color:#fff;background-color:#dc3545;border-color:#dc3545}.btn-block{display:block;width:100%}.card{display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;min-width:0;word-wrap:break-word;background-color:#fff;background-clip:border-box;border:1px solid rgba(0,0,0,.125);border-radius:.25rem}.card-body{-ms-flex:1 1 auto;flex:1 1 auto;padding:1.25rem}.card-header{padding:.75rem 1.25rem;margin-bottom:0;background-color:rgba(0,0,0,.03);border-bottom:1px solid rgba(0,0,0,.125)}.card-header:first-child{border-radius:calc(.25rem - 1px) calc(.25rem - 1px) 0 0}.jumbotron{padding:2rem 1rem;margin-bottom:2rem;background-color:#e9ecef;border-radius:.3rem}@media (min-width:576px){.jumbotron{padding:4rem 2rem}}.jumbotron-fluid{padding-right:0;padding-left:0;border-radius:0}.bg-secondary{background-color:#6c757d!important}.bg-light{background-color:#f8f9fa!important}.mb-3{margin-bottom:1rem!important}.text-center{text-align:center!important}.text-white{color:#fff!important}.text-dark{color:#343a40!important}@media print{*,::after,::before{text-shadow:none!important;box-shadow:none!important}p{orphans:3;widows:3}.container{min-width:992px!important}}</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment