Issues (1626)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

phpsysinfo/plugins/ps/class.ps.inc.php (10 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * PS Plugin
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_Plugin_PS
9
 * @author    Michael Cramer <[email protected]>
10
 * @copyright 2009 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
12
 * @version   SVN: $Id: class.ps.inc.php 692 2012-09-08 17:12:08Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * process Plugin, which displays all running processes
17
 * a simple tree view which is filled with the running processes which are determined by
18
 * calling the "ps" command line utility, another way is to provide
19
 * a file with the output of the ps utility, so there is no need to run a execute by the
20
 * webserver, the format of the command is written down in the phpsysinfo.ini file, where also
21
 * the method of getting the information is configured
22
 *
23
 * @category  PHP
24
 * @package   PSI_Plugin_PS
25
 * @author    Michael Cramer <[email protected]>
26
 * @copyright 2009 phpSysInfo
27
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28
 * @version   Release: 3.0
29
 * @link      http://phpsysinfo.sourceforge.net
30
 */
31
class PS extends PSI_Plugin
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
32
{
33
    /**
34
     * variable, which holds the content of the command
35
     * @var array
36
     */
37
    private $_filecontent = array();
38
    /**
39
     * variable, which holds the result before the xml is generated out of this array
40
     * @var array
41
     */
42
    private $_result = array();
43
    /**
44
     * read the data into an internal array and also call the parent constructor
45
     *
46
     * @param String $enc encoding
47
     */
48
    public function __construct($enc)
49
    {
50
        parent::__construct(__CLASS__, $enc);
51
        switch (strtolower(PSI_PLUGIN_PS_ACCESS)) {
52
        case 'command':
53
            if (PSI_OS == 'WINNT') {
54
                try {
55
                    $objLocator = new COM("WbemScripting.SWbemLocator");
0 ignored issues
show
The call to com::__construct() has too many arguments starting with 'WbemScripting.SWbemLocator'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
56
                    $wmi = $objLocator->ConnectServer();
57
                    $os_wmi = $wmi->InstancesOf('Win32_OperatingSystem');
58
                    foreach ($os_wmi as $os) {
59
                        $memtotal = $os->TotalVisibleMemorySize * 1024;
60
                    }
61
                    $process_wmi = $wmi->InstancesOf('Win32_Process');
62
                    foreach ($process_wmi as $process) {
63
                        if (strlen(trim($process->CommandLine)) > 0) {
64
                            $ps = trim($process->CommandLine);
65
                        } else {
66
                            $ps = trim($process->Caption);
67
                        }
68
                        if (trim($process->ProcessId) != 0) {
69
                            $memusage = round(trim($process->WorkingSetSize) * 100 / $memtotal, 1);
0 ignored issues
show
The variable $memtotal does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
70
                            //ParentProcessId
71
                            //Unique identifier of the process that creates a process. Process identifier numbers are reused, so they
72
                            //only identify a process for the lifetime of that process. It is possible that the process identified by
73
                            //ParentProcessId is terminated, so ParentProcessId may not refer to a running process. It is also
74
                            //possible that ParentProcessId incorrectly refers to a process that reuses a process identifier. You can
75
                            //use the CreationDate property to determine whether the specified parent was created after the process
76
                            //represented by this Win32_Process instance was created.
77
                            //=> subtrees of processes may be missing (WHAT TODO?!?)
78
                            $this->_filecontent[] = trim($process->ProcessId)." ".trim($process->ParentProcessId)." ".$memusage." ".$ps;
79
                        }
80
                    }
81
                } catch (Exception $e) {
82
                }
83
            } else {
84
                CommonFunctions::executeProgram("ps", "axo pid,ppid,pmem,args", $buffer, PSI_DEBUG);
85
                if (((PSI_OS == 'Linux') || (PSI_OS == 'Android')) && (!preg_match("/^[^\n]+\n\s*\d+\s+\d+\s+[\d\.]+\s+.+/", $buffer))) { //alternative method if no data
86
                    if (CommonFunctions::rfts('/proc/meminfo', $mbuf)) {
87
                        $bufe = preg_split("/\n/", $mbuf, -1, PREG_SPLIT_NO_EMPTY);
88
                        $totalmem = 0;
89
                        foreach ($bufe as $buf) {
90
                            if (preg_match('/^MemTotal:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
91
                                $totalmem = $ar_buf[1];
92
                                break;
93
                            }
94
                        }
95
                        $buffer = "  PID  PPID %MEM COMMAND\n";
96
97
                        $processlist = glob('/proc/*/status', GLOB_NOSORT);
98
                        if (($total = count($processlist)) > 0) {
99
                            natsort($processlist); //first sort
100
                            $prosess = array();
0 ignored issues
show
$prosess is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
101
                            foreach ($processlist as $processitem) { //second sort
102
                                $process[] = $processitem;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$process was never initialized. Although not strictly required by PHP, it is generally a good practice to add $process = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
103
                            }
104
105
                            $buf = "";
106
                            for ($i = 0; $i < $total; $i++) {
107
                                if (CommonFunctions::rfts($process[$i], $buf, 0, 4096, false)) {
108
109
                                    if (($totalmem != 0) && (preg_match('/^VmRSS:\s+(\d+)\s+kB/m', $buf, $tmppmem))) {
110
                                        $pmem = round(100 * $tmppmem[1] / $totalmem, 1);
111
                                    } else {
112
                                        $pmem = 0;
113
                                    }
114
115
                                    $name = null;
116
                                    if (CommonFunctions::rfts(substr($process[$i], 0, strlen($process[$i])-6)."cmdline", $namebuf, 0, 4096, false)) {
0 ignored issues
show
The variable $process does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
117
                                        $name = str_replace(chr(0), ' ', trim($namebuf));
118
                                    }
119
                                    if (preg_match('/^Pid:\s+(\d+)/m', $buf, $tmppid) &&
120
                                        preg_match('/^PPid:\s+(\d+)/m', $buf, $tmpppid) &&
121
                                        preg_match('/^Name:\s+(.+)/m', $buf, $tmpargs)) {
122
                                        $pid = $tmppid[1];
123
                                        $ppid = $tmpppid[1];
124
                                        $args = $tmpargs[1];
125
                                        if ($name !== null) {
126
                                            if ($name !== "") {
127
                                                $args = $name;
128
                                            } else {
129
                                                $args = "[".$args."]";
130
                                            }
131
                                        }
132
                                        $buffer .= $pid." ".$ppid." ".$pmem." ".$args."\n";
133
                                    }
134
135
                                }
136
                            }
137
                        }
138
                    }
139
                }
140
            }
141
            break;
142
        case 'data':
143
            CommonFunctions::rfts(APP_ROOT."/data/ps.txt", $buffer);
144
            break;
145
        default:
146
            $this->global_error->addConfigError("__construct()", "PSI_PLUGIN_PS_ACCESS");
147
            break;
148
        }
149
        if (PSI_OS != 'WINNT') {
150
            if (trim($buffer) != "") {
151
                $this->_filecontent = preg_split("/\n/", $buffer, -1, PREG_SPLIT_NO_EMPTY);
0 ignored issues
show
The variable $buffer does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
152
                unset($this->_filecontent[0]);
153
            } else {
154
                $this->_filecontent = array();
155
            }
156
        }
157
    }
158
    /**
159
     * doing all tasks to get the required informations that the plugin needs
160
     * result is stored in an internal array<br>the array is build like a tree,
161
     * so that it is possible to get only a specific process with the childs
162
     *
163
     * @return void
164
     */
165
    public function execute()
166
    {
167
        if (empty($this->_filecontent)) {
168
            return;
169
        }
170
        $items = array();
171
        foreach ($this->_filecontent as $roworig) {
172
            $row = preg_split("/[\s]+/", trim($roworig), 4);
173
            if (count($row) != 4) {
174
                break;
175
            }
176
            foreach ($row as $key=>$val) {
177
                $items[$row[0]][$key] = $val;
178
            }
179
            if ($row[1] !== $row[0]) {
180
                $items[$row[1]]['childs'][$row[0]] = &$items[$row[0]];
181
            }
182
        }
183
        foreach ($items as $item) { //find zombie
184
            if (!isset($item[0])) {
185
                foreach ($item["childs"] as $subitem) {
186
                    $zombie = $subitem[1];
187
                    if ($zombie != 0) {
188
                        $items[$zombie]["0"] = $zombie;
189
                        $items[$zombie]["1"] = "0";
190
                        $items[$zombie]["2"] = "0";
191
                        $items[$zombie]["3"] = "unknown";
192
                        $items[0]['childs'][$zombie] = &$items[$zombie];
193
                    }
194
                    break; //first is sufficient
195
                }
196
            }
197
        }
198
        if (isset($items[0])) {
199
            $this->_result = $items[0];
200
        } else {
201
            $_result = array();
0 ignored issues
show
$_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
202
        }
203
    }
204
    /**
205
     * generates the XML content for the plugin
206
     *
207
     * @return SimpleXMLElement entire XML content for the plugin
208
     */
209
    public function xml()
210
    {
211
        if ($this->_result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
212
            $positions = array(0=>0);
213
            $this->_addchild($this->_result['childs'], $this->xml, $positions);
214
        }
215
216
        return $this->xml->getSimpleXmlElement();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->xml->getSimpleXmlElement(); (SimpleXMLElement) is incompatible with the return type declared by the interface PSI_Interface_Plugin::xml of type SimpleXMLObject.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
217
    }
218
    /**
219
     * recursive function to allow appending child processes to a parent process
220
     *
221
     * @param Array             $child      part of the array which should be appended to the XML
222
     * @param SimpleXMLExtended $xml        XML-Object to which the array content is appended
223
     * @param Array             &$positions array with parent positions in xml structure
224
     *
225
     * @return SimpleXMLExtended Object with the appended array content
226
     */
227
    private function _addchild($child, SimpleXMLExtended $xml, &$positions)
228
    {
229
        foreach ($child as $key=>$value) {
230
            $xmlnode = $xml->addChild("Process");
231
            if (isset($value[0])) {
232
                array_push($positions, $value[0]);
233
                $xmlnode->addAttribute('PID', $value[0]);
234
                $parentid = array_search($value[1], $positions);
235
                $xmlnode->addAttribute('ParentID', $parentid);
236
                $xmlnode->addAttribute('PPID', $value[1]);
237
                $xmlnode->addAttribute('MemoryUsage', $value[2]);
238
                $xmlnode->addAttribute('Name', $value[3]);
239
                if (PSI_OS !== 'WINNT') {
240
                    if ($parentid === 1) {
241
                        $xmlnode->addAttribute('Expanded', 0);
242
                    }
243
                    if (defined('PSI_PLUGIN_PS_SHOW_KTHREADD_EXPANDED') && (PSI_PLUGIN_PS_SHOW_KTHREADD_EXPANDED === false) && ($value[3] === "[kthreadd]")) {
244
                        $xmlnode->addAttribute('Expanded', 0);
245
                    }
246
                }
247
            }
248
            if (isset($value['childs'])) {
249
                $this->_addChild($value['childs'], $xml, $positions);
250
            }
251
        }
252
253
        return $xml;
254
    }
255
}
256