Skip to content

Commit 2508d87

Browse files
committed
Added checkAccessCallback in order to allow custom access checks
1 parent 0ddf839 commit 2508d87

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

Module.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace samdark\webshell;
33

44
use Yii;
5+
use yii\base\Action;
56
use yii\web\ForbiddenHttpException;
67

78
/**
@@ -53,6 +54,17 @@ class Module extends \yii\base\Module
5354
*/
5455
public $allowedIPs = ['127.0.0.1', '::1'];
5556

57+
/**
58+
* @var callable A valid PHP callback that returns true if user is allowed to use web shell and false otherwise
59+
*
60+
* The signature is the following:
61+
*
62+
* function (Action $action)
63+
*
64+
* @since 2.0.0
65+
*/
66+
public $checkAccessCallback;
67+
5668
/**
5769
* @inheritdoc
5870
*/
@@ -71,7 +83,7 @@ public function beforeAction($action)
7183
return false;
7284
}
7385

74-
if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess()) {
86+
if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess($action)) {
7587
throw new ForbiddenHttpException('You are not allowed to access this page.');
7688
}
7789

@@ -81,16 +93,28 @@ public function beforeAction($action)
8193
/**
8294
* @return boolean whether the module can be accessed by the current user
8395
*/
84-
protected function checkAccess()
96+
protected function checkAccess(Action $action)
8597
{
98+
$allowed = false;
99+
86100
$ip = Yii::$app->getRequest()->getUserIP();
87101
foreach ($this->allowedIPs as $filter) {
88102
if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
89-
return true;
103+
$allowed = true;
104+
break;
90105
}
91106
}
92-
Yii::warning('Access to web shell is denied due to IP address restriction. The requested IP is ' . $ip, __METHOD__);
93107

94-
return false;
108+
if ($allowed === false) {
109+
Yii::warning('Access to web shell is denied due to IP address restriction. The requested IP is ' . $ip, __METHOD__);
110+
return false;
111+
}
112+
113+
if ($this->checkAccessCallback !== null && call_user_func_array($this->checkAccessCallback, [$action]) !== true) {
114+
Yii::warning('Access to web shell is denied due to checkAccessCallback.', __METHOD__);
115+
return false;
116+
}
117+
118+
return true;
95119
}
96120
}

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,24 @@ return [
4444
With the above configuration, you will be able to access web shell in your browser using
4545
the URL `http://localhost/path/to/index.php?r=webshell`
4646

47+
Access control
48+
--------------
49+
50+
By default access is restricted to local IPs. It could be changed via `allowedIPs` property. Additionally,
51+
`checkAccessCallback` is available to be able to introduce custom access control:
52+
53+
```php
54+
return [
55+
'modules' => [
56+
'webshell' => [
57+
'class' => 'samdark\webshell\Module',
58+
// 'yiiScript' => Yii::getAlias('@root'). '/yii', // adjust path to point to your ./yii script
59+
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.2'],
60+
'checkAccessCallback' => function (\yii\base\Action $action) {
61+
// return true if access is granted or false otherwise
62+
return true;
63+
}
64+
],
65+
],
66+
]
67+
```

0 commit comments

Comments
 (0)