2
2
namespace samdark \webshell ;
3
3
4
4
use Yii ;
5
+ use yii \base \Action ;
5
6
use yii \web \ForbiddenHttpException ;
6
7
7
8
/**
@@ -53,6 +54,17 @@ class Module extends \yii\base\Module
53
54
*/
54
55
public $ allowedIPs = ['127.0.0.1 ' , '::1 ' ];
55
56
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
+
56
68
/**
57
69
* @inheritdoc
58
70
*/
@@ -71,7 +83,7 @@ public function beforeAction($action)
71
83
return false ;
72
84
}
73
85
74
- if (Yii::$ app instanceof \yii \web \Application && !$ this ->checkAccess ()) {
86
+ if (Yii::$ app instanceof \yii \web \Application && !$ this ->checkAccess ($ action )) {
75
87
throw new ForbiddenHttpException ('You are not allowed to access this page. ' );
76
88
}
77
89
@@ -81,16 +93,28 @@ public function beforeAction($action)
81
93
/**
82
94
* @return boolean whether the module can be accessed by the current user
83
95
*/
84
- protected function checkAccess ()
96
+ protected function checkAccess (Action $ action )
85
97
{
98
+ $ allowed = false ;
99
+
86
100
$ ip = Yii::$ app ->getRequest ()->getUserIP ();
87
101
foreach ($ this ->allowedIPs as $ filter ) {
88
102
if ($ filter === '* ' || $ filter === $ ip || (($ pos = strpos ($ filter , '* ' )) !== false && !strncmp ($ ip , $ filter , $ pos ))) {
89
- return true ;
103
+ $ allowed = true ;
104
+ break ;
90
105
}
91
106
}
92
- Yii::warning ('Access to web shell is denied due to IP address restriction. The requested IP is ' . $ ip , __METHOD__ );
93
107
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 ;
95
119
}
96
120
}
0 commit comments