-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.phpPythonPipe.php
125 lines (115 loc) · 3.75 KB
/
class.phpPythonPipe.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/****************************************************************************
* A class to execute python code from PHP without using temp files
*
* Example usage:
* require_once("class.phpPythonPipe.php");
* $pythonCode = "import sys\n";
* $pythonCode .= "print(\"Hello\")\n";
* $python = new phpPythonPipe();
* $python->kernelPath = "~/anaconda3/bin/python";
* $python->code = $pythonCode;
* $python->exec();
* $python->print();
*
* @package phpPythonPipe
* @author Filipi Vianna
* @version 0.0.1
* $access public
* @see https://github.com/filipi/phpPythonPipe
*/
class phpPythonPipe{
public $kernelPath = '/usr/bin/python';
public $code = '';
public $output = '';
public $showSTDERR = false;
public $debugLevel = 0;
private $blackListCommands = array();
/**
* https://www.owasp.org/index.php/Testing_for_Command_Injection_(OTG-INPVAL-013)
* black list system near commands:
*/
public function __construct(){
$this->blackListCommands['python'][] = "eval";
$this->blackListCommands['python'][] = "os.system";
$this->blackListCommands['python'][] = "os.popen";
$this->blackListCommands['python'][] = "subprocess.popen";
$this->blackListCommands['python'][] = "subprocess.call";
$this->blackListCommands['PHP'][] = "system";
$this->blackListCommands['PHP'][] = "shell_exec";
$this->blackListCommands['PHP'][] = "exec";
$this->blackListCommands['PHP'][] = "proc_open";
$this->blackListCommands['PHP'][] = "eval";
$this->blackListCommands['PHP'][] = "passthru";
$this->blackListCommands['PHP'][] = "proc_open";
$this->blackListCommands['PHP'][] = "expect_open";
$this->blackListCommands['PHP'][] = "ssh2_exec";
$this->blackListCommands['PHP'][] = "popen";
}
/**
* Removes system near python commands to prevent code injection
* @param void
* @return void
* @access private
*/
private function codeInjectionCheck(){
foreach($this->blackListCommands['python'] as $command){
$this->code = str_replace( $command, '', $this->code);
if ($this->debugLevel > 3){
echo "-------------------------\n";
echo "CODE: " . $this->code;
echo "-------------------------\n";
}
}
}
/**
* Executes the python code and stores output in $this->output property
* @param void
* @return void
* @access public
*/
public function exec() {
$this->codeInjectionCheck();
/*
-s Don't add user site directory to sys.path.
-S Disable the import of the module site and the site-dependent
manipulations of sys.path that it entails.
(think to create a black list of modules...)
*/
//$command = "export PYTHONDUMPREFS=1 & " . $this->kernelPath . " -s -c '" . $this->code . "'";
$command = $this->kernelPath . " -c '" . $this->code . "' " . ($this->showSTDERR ? " 2>&1 " : "");
$this->output = `$command`;
}
/**
* Echos the output from $this->output property
* @param void
* @return void
* @access public
*/
public function print(){
echo $this->output;
}
/**
* pass all variables to PHP
* https://stackoverflow.com/questions/192109/is-there-a-built-in-function-to-print-all-the-current-properties-and-values-of-a
* >>> l = dir(__builtins__)
* >>> d = __builtins__.__dict__
* Print that dictionary however fancy you like:
*
* >>> print l
* ['ArithmeticError', 'AssertionError', 'AttributeError',...
* or
*
* >>> from pprint import pprint
* >>> pprint(l)
* ['ArithmeticError',
* 'AssertionError',
* 'AttributeError',
* 'BaseException',
* 'DeprecationWarning',
* ...
*
* >>> pprint(d, indent=2)
*/
}
?>