/
functions.php
134 lines (127 loc) · 4.71 KB
/
functions.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
<?php
/*
* **************************************************************
**************** MCS EXPT3 ******************************
***************************************************************/
/* Designed & Developed by
/* - Shiburaj Pappu
/* ************ S.P.I.T *************************************
/* ********** M.E (EXTC) Sem-II *****************************
*/
?>
<?php
include_once('Database.class.php');
define("MSCTBL","msc");
$db = new Database('localhost', 'root', 'trdc123', 'mscsim');
//connect to the server
$db->connect();
if($_GET['action']=='register' && !empty($_POST['MSISDN']) && !empty($_POST['IMSI'])){
$preMSISDN = $_POST['MSISDN'][0].$_POST['MSISDN'][1].$_POST['MSISDN'][2].$_POST['MSISDN'][3];
$data['TMSI'] = "91".$preMSISDN.rand('199999','999999');
$sql = "SELECT * FROM ".MSCTBL." WHERE `MSISDN` = '".$_POST['MSISDN']."'";
$row = $db->query_first($sql);
if(empty($row)){
$error_dat['status'] = "error";
$error_dat['reason'] = "Registration Failed...";
}else{
$db->query_update(MSCTBL,$data," `MSISDN` = '".$_POST['MSISDN']."' ");
$error_dat['regdata'] = $row;
$error_dat['regdata']['TMSI'] = $data['TMSI'];
$error_dat['status'] = "success";
$error_dat['reason'] = "Registration Complete...";
}
sleep(3);
echo json_encode($error_dat);
}elseif($_GET['action']=='makesim' && !empty($_POST['MSISDN'])){
$preMSISDN = $_POST['MSISDN'][0].$_POST['MSISDN'][1].$_POST['MSISDN'][2].$_POST['MSISDN'][3];
$data['IMSI'] = "91".$preMSISDN.rand('199999','999999');
$data['IMEI'] = "956647"."894467".rand('199999','999999');
$data['Ki'] = genRandKey(128);
$data['NSP'] = getNSP($preMSISDN);
$sql = "SELECT * FROM ".MSCTBL." WHERE `MSISDN` = '".$_POST['MSISDN']."'";
$row = $db->query_first($sql);
if(empty($row)){
$data['MSISDN'] = $_POST['MSISDN'];
$db->query_insert(MSCTBL,$data);
}else{
$db->query_update(MSCTBL,$data," `MSISDN` = '".$_POST['MSISDN']."' ");
$data['MSISDN'] = $_POST['MSISDN'];
}
echo json_encode($data);
}elseif($_GET['action']=='authenticate' && !empty($_POST['MSISDN']) && !empty($_POST['TMSI'])){
$sql = "SELECT * FROM ".MSCTBL." WHERE `MSISDN` = '".$_POST['MSISDN']."' AND `TMSI` = '".$_POST['TMSI']."'";
$row = $db->query_first($sql);
if(empty($row)){
$error_dat['status'] = "error";
$error_dat['reason'] = "Device Not Registered...";
}else{
$error_dat['RAND'] = genRandKey(128);
$error_dat['RES'] = resGen($error_dat['RAND'],$row['Ki'],128);
$error_dat['status'] = "success";
$error_dat['reason'] = "RAND Generation Complete...";
}
sleep(3);
echo json_encode($error_dat);
}elseif($_GET['action']=='gensres' && !empty($_POST['RAND']) && !empty($_POST['Ki'])){
$error_dat['SRES'] = resGen($_POST['RAND'],$_POST['Ki'],128);
$error_dat['status'] = "success";
$error_dat['reason'] = "RAND Generation Complete...";
sleep(3);
echo json_encode($error_dat);
}elseif($_GET['action']=='sendsres' && !empty($_POST['SRES']) && !empty($_POST['RES'])){
if($_POST['SRES']==$_POST['RES']){
$error_dat['status'] = "success";
$error_dat['reason'] = "Authentication Successfull...";
}else{
$error_dat['status'] = "error";
$error_dat['reason'] = "Authentication Failed...";
}
sleep(3);
echo json_encode($error_dat);
}
function genRandKey($length=128){
$count = round($length/8);
$binData = "";
for($i=1;$i<=$count;$i++){
$binData .= dechex(rand('128','255'))."-";
}
$binData = trim($binData,"-");
return $binData;
}
function getNSP($preMSISDN){
switch($preMSISDN){
case "9209":
return "Tata Docomo";
break;
case "9967":
return "Airtel";
break;
case "9209":
return "Tata Indicom";
break;
case "9773":
return "Aircel";
break;
case "8087":
return "Tata Docomo";
break;
default:
return "Other";
}
}
function resGen($rand,$ki,$length=128){
$count = round($length/16);
$rand_arr = explode("-",$rand);
$ki_arr = explode("-",$ki);
for($i=0;$i<$count;$i++){
$left = hexdec($rand_arr[$i]) ^ hexdec($ki_arr[$i+$count]);
$right = hexdec($ki_arr[$i]) ^ hexdec($rand_arr[$i+$count]);
$res64[$i] = $left ^ $right;
}
$count2 = round($count/2);
for($i=0;$i<$count2;$i++){
$res32[$i] = dechex($res64[$i] ^ $res64[$count2+$i]);
}
return implode('-',$res32);
}
?>