-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathinterface.php
343 lines (298 loc) · 8.04 KB
/
interface.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
include_once(dirname(__FILE__).'/Crypt/AES.php');
include_once(dirname(__FILE__).'/Crypt/Random.php');
include_once(dirname(__FILE__).'/config.php');
include_once(dirname(__FILE__).'/common.php');
include_once(dirname(__FILE__).'/CASignCert.php');
include_once(dirname(__FILE__).'/DBAdopt/_class/BLL/operator.php');
session_start();
/**
* Created by PhpStorm.
* User: abcdlzy
* Date: 15/1/20
* Time: 下午1:22
*/
ini_set('mbstring.internal_encoding','UTF-8');
ini_set('mbstring.func_overload',7);
header('Content-Type: text/html; charset=UTF-8');
//开始处理数据
@$isforceEncrypt=$_POST["forceEncrypt"];//用于跨CA的数据跳转
@$action=$_POST["action"];
@$interfaceData=$_POST["data"];
if($action==="GetPublishX509")
{
GetPublishX509();
echo ',';
echo create_guid();
}
else if($action==="UILoginStart")
{
$_SESSION['AESKEY']=bin2hex(crypt_random_string(8));
echo $_SESSION['AESKEY'];
}
else if($action==="UIdoLogin")
{
UIdoLogin($interfaceData);
}
else if($action==="doGetCertStep2")
{
doGetCertStep2($interfaceData);
}
else if($action==="doGetCertStep4")
{
doGetCertStep4($interfaceData);
}
else if ($action==="UIFinishLogin")
{
UIFinishLogin();
}
else if ($action==="deleteSignCert")
{
deleteSignCert($interfaceData);
}
else if ($action==="insertShell")
{
insertShell($interfaceData);
}
else if ($action==="updateShell")
{
updateShell($interfaceData);
}
else if ($action==="deleteShell")
{
deleteShell($interfaceData);
}
else if ($action==="getServerGUID")
{
getServerGUID();
}
else if ($action==="requestGUIDPEM") {
requestGUIDPEM($interfaceData);
}
else if ($action==="getActiveServerList")
{
getActiveServerList($interfaceData);
}
else if ($action==="randOneActiveServer")
{
randOneActiveServer();
}
elseif($action==="modifyPassword"){
checkLogin();
modifyPassword($interfaceData);
}
elseif($action==="remakeCAKey"){
checkLogin();
remakeCAKey();
}
elseif($action==="saveSysMySQLSetting"){
checkLogin();
saveSysMySQLSetting($interfaceData);
}
else{
echo '404';
}
function saveSysMySQLSetting($data){
$dataArray=explode(",",$data);
setConfig_MySQL_Host($dataArray[0]);
setConfig_MySQL_UserName($dataArray[1]);
setConfig_MySQL_Password($dataArray[2]);
setConfig_MySQL_DataBase($dataArray[3]);
echo 'end';
}
function remakeCAKey(){
echo CreateNewCA()?"successful":"failed";
}
function modifyPassword($data){
$dataArray=explode(",",$data);
count($dataArray)===3 or die('length check error');
if($dataArray[0]===getConfig_LoginPassword()){
if($dataArray[1]===$dataArray[2]){
setConfig_LoginPassword($dataArray[2]);
echo 'successful';
}
else{
echo '重复新密码校验失败';
}
}
else{
echo '旧密码填写错误';
}
}
function randOneActiveServer(){
$rs=SQLAdopt::getOne(new activeServerModel('rand','',''));
echo $rs->GUID.",".$rs->interfaceURL;
}
function getActiveServerList($data){
$decodeData=unpacketInAESData($data);
$model=new activeServerModel(splitByComma($decodeData)[0],splitByComma($decodeData)[1],'');
echo SQLAdopt::insert($model);
echo get_ActiveServerAllJson();
}
function requestGUIDPEM($packetGUID){
$reciveGUID=splitByComma($packetGUID)[0];
echo response_PemOut($reciveGUID,unpacketInAESData($packetGUID));
}
function getServerGUID(){
echo getConfig_SelfGUID();
}
function UIdoLogin($data)
{
$data1=$data;
try{
if(@isset($_SESSION['AESKEY'])&&@isset($_SESSION['AESIV'])){
$data1=AESDecrypt($data,$_SESSION['AESKEY'],$_SESSION['AESIV']);
}
}catch (Exception $e){
}
$dataArray=preg_split("/[\s,]+/", $data1);
$host=$dataArray[0];
$passwd=$dataArray[1];
if($GLOBALS['loginPassWord']===$passwd){
if(!isset($_SESSION['host'])){
$_SESSION['host']=$host;
$_SESSION['passwd']=$passwd;
}
UIFinishLogin();
}
else
{
echo 'failed';
}
}
function GetPublishX509(){
if($GLOBALS['isCA']==true) {
if(isset($GLOBALS['CAPubX509'])){
echo $GLOBALS['CAPubX509'];
}
else{
echo 'CA Public X509 is not set';
}
}else{
echo 'this is not CA';
}
}
function doGetCertStep2($data){
//todo s=>c enc by aes (privatekey,x509)
//是否设置
if(isset($GLOBALS['CAPrivKeyStr'])){
//解密
$decryptData= RSADecrypt($data,$GLOBALS['CAPrivKeyStr']);
//分割
$dataArray=preg_split("/,{1,1}/", $decryptData);
$key=$dataArray[0];
$iv=$dataArray[1];
//生成证书
$newCert=requestSignCert();
//生成GUID
$userGUID= create_guid();
//写入记录进入数据库
$certmodel=new certModel($userGUID,getip(),$newCert['publicX509'],'');
SQLAdopt::insert($certmodel);
if(count($dataArray)==3){
$clientIURL=$dataArray[2];
$acitveServermodel=new activeServerModel($userGUID,$clientIURL,'');
SQLAdopt::insert($acitveServermodel);
}
//返回数据
$returnStr=$userGUID.",".$newCert['privateKey'].",".$newCert['publicX509'].",".getConfig_CAGUID();
//加密
echo AESEncrypt($returnStr,$key,$iv);
}
else{
echo 'It look like is not CA';
}
}
function doGetCertStep4($data){
//解密
$decryptData= RSADecrypt($data,$GLOBALS['CAPrivKeyStr']);
//分割
$usercert=SQLAdopt::getOne(new certModel($decryptData,'','',''));
if(!empty($usercert)){
$keyII=md5(uniqid(mt_rand(), true));
$ivII=md5(uniqid(mt_rand(), true));
$_SESSION['AESKEY']=$keyII;
$_SESSION['AESIV']=$ivII;
save_AESKeyIV($decryptData,$keyII,$ivII,'in');
save_AESKeyIV($decryptData,$keyII,$ivII,'out');
echo RSAEncrypt($keyII.','.$ivII,$usercert->x509);
}
else{
echo 'noEncrypt';
}
}
function requestSignCert(){
if($GLOBALS['isCA']==true){
if(!(isset($GLOBALS['CAPrivKeyStr'])&&!is_null($GLOBALS['CAPrivKeyStr'])&&isset($GLOBALS['CAPubX509'])&&!is_null($GLOBALS['CAPubX509']))){
CreateNewCA();
}
return signNewCert();
}
else{
return false;
}
}
function UIFinishLogin(){
$_SESSION['loginToken']=create_guid();
echo $_SESSION['loginToken'];
}
function deleteSignCert($data){
$dataArray=preg_split("/,{1,1}/", $data);
$reciveToken=$dataArray[0];
$CertGUID=$dataArray[1];
if($reciveToken===$_SESSION['loginToken']){
SQLAdopt::delete(new certModel($CertGUID,'','',''));
}
echo 'completed';
}
function deleteShell($data){
$dataArray=preg_split("/,{1,1}/", $data);
$reciveToken=$dataArray[0];
$shellUrl=$dataArray[1];
if($reciveToken===$_SESSION['loginToken']){
SQLAdopt::delete(new shellModel($shellUrl,'','','',''));
}
echo 'completed';
}
function insertShell($data){
$dataArray=preg_split("/,{1,1}/", $data);
//loginToken+","+url+","+pass+","+type+","+extraConfig+","+notes
$reciveToken=$dataArray[0];
$url=$dataArray[1];
$pass=$dataArray[2];
$type=$dataArray[3];
$extraConfig=$dataArray[4];
$notes=$dataArray[5];
if($reciveToken===$_SESSION['loginToken']){
SQLAdopt::insert(new shellModel($url,$pass,$extraConfig,$notes,$type));
}
echo 'completed';
}
function updateShell($data){
$dataArray=preg_split("/,{1,1}/", $data);
//loginToken+","+url+","+pass+","+type+","+extraConfig+","+notes
$reciveToken=$dataArray[0];
$url=$dataArray[1];
$pass=$dataArray[2];
$type=$dataArray[3];
$extraConfig=$dataArray[4];
$notes=$dataArray[5];
if($reciveToken===$_SESSION['loginToken']){
SQLAdopt::update(new shellModel($url,$pass,$extraConfig,$notes,$type));
}
echo 'completed';
}
function checkLogin(){
if(isset($_POST['Token'])) {
if (@$_POST['Token'] === $_SESSION['loginToken']) {
return true;
}
else{
die('need login');
}
}
else{
die('Token Need Set');
}
}
?>