forked from riogrande75/Dahua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDahuaDelRecords.php
228 lines (216 loc) · 7.01 KB
/
DahuaDelRecords.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
<?PHP
$debug = true;
echo "<** Dahua VTH Clear MissedCall Log **>\n";
$Dahua = new Dahua_Functions("192.168.x.x", "admin", "123456"); # VTH's IP and user/pwd
$status = $Dahua->Main();
logging("All done");
function logging($text){
list($ts) = explode(".",microtime(true));
$dt = new DateTime(date("Y-m-d H:i:s.",$ts));
$logdate = $dt->format("Y-m-d H:i:s.u");
echo $logdate.": ";
print_r($text);
echo "\n";
}
class Dahua_Functions
{
private $sock, $host, $port, $credentials;
private $ID = 0; # Our Request / Responce ID that must be in all requests and initated by us
private $SessionID = 0; # Session ID will be returned after successful login
private $SID = 0; # SID will be returned after we called <service>.attach with 'Object ID'
private $FakeIPaddr = '(null)'; # WebGUI: mask our real IP
private $clientType = ''; # WebGUI: We do not show up in logs or online users
function Dahua_Functions($host, $user, $pass)
{
$this->host = $host;
$this->username = $user;
$this->password = $pass;
}
function Gen_md5_hash($Dahua_random, $Dahua_realm, $username, $password)
{
$PWDDB_HASH = strtoupper(md5($username.':'.$Dahua_realm.':'.$password));
$PASS = $username.':'.$Dahua_random.':'.$PWDDB_HASH;
$RANDOM_HASH = strtoupper(md5($PASS));
return $RANDOM_HASH;
}
function disconnect()
{
global $id;
if ($this->sock)
fclose($this->sock);
}
function Send($packet)
{
if (empty($packet)){
$packet = '';
}
$header = pack("N",0x20000000);
$header .= pack("N",0x44484950);
$header .= pack("V",$this->SessionID);
$header .= pack("V",$this->ID);
$header .= pack("V",strlen($packet));
$header .= pack("V",0);
$header .= pack("V",strlen($packet));
$header .= pack("V",0);
if (strlen($header) != 32){
logging("Binary header != 32 ({})");
return;
}
$this->ID += 1;
try{
$msg = $header.$packet;
$result = fwrite($this->sock, $msg);
} catch (Exception $e) {
logging($e);
}
}
function Receive($timeout = 5)
{
#
# We must expect there is no output from remote device
# Some debug cmd do not return any output, some will return after timeout/failure, most will return directly
#
$data = "";
$P2P_header = "";
$P2P_data = "";
$P2P_return_data = [];
$header_LEN = 0;
try{
$len = strlen($data);
$read = array($this->sock);
$write = null;
$except = null;
$ready = stream_select($read, $write, $except, $timeout);
if ($ready > 0) {
$data .= stream_socket_recvfrom($this->sock, 8192);
}
} catch (Exception $e) {
return "";
}
if (strlen($data)==0){
#logging("Nothing received anything from remote");
return "";
}
$LEN_RECVED = 1;
$LEN_EXPECT = 1;
while (strlen($data)>0){
if (substr($data,0,8) == pack("N",0x20000000).pack("N",0x44484950)){ # DHIP
$P2P_header = substr($data,0,32);
$LEN_RECVED = unpack("V",substr($data,16,4))[1];
$LEN_EXPECT = unpack("V",substr($data,24,4))[1];
$data = substr($data,32);
}
else{
if($LEN_RECVED > 1){
$P2P_data = substr($data,0,$LEN_RECVED);
$P2P_return_data[] = $P2P_data;
}
$data = substr($data,$LEN_RECVED);
if ($LEN_RECVED == $LEN_EXPECT && strlen($data)==0){
break;
}
}
}
return $P2P_return_data;
}
function Login()
{
logging("Start login");
$query_args = array(
'id'=>10000,
'magic'=>"0x1234",
'method'=>"global.login",
'params'=>array(
'clientType'=>$this->clientType,
'ipAddr'=>$this->FakeIPaddr,
'loginType'=>"Direct",
'password'=>"",
'userName'=>$this->username,
),
'session'=>0
);
$this->Send(json_encode($query_args));
$data = $this->Receive();
if (empty($data)){
logging("global.login [random]");
return false;
}
$data = json_decode($data[0], true);
$this->SessionID = $data['session'];
$RANDOM = $data['params']['random'];
$REALM = $data['params']['realm'];
$RANDOM_HASH = $this->Gen_md5_hash($RANDOM, $REALM, $this->username, $this->password);
$query_args = array(
'id'=>10000,
'magic'=>"0x1234",
'method'=>"global.login",
'session'=>$this->SessionID,
'params'=>array(
'userName'=>$this->username,
'password'=>$RANDOM_HASH,
'clientType'=>$this->clientType,
'ipAddr'=>$this->FakeIPaddr,
'loginType'=>"Direct",
'authorityType'=>"Default",
)
);
$this->Send(json_encode($query_args));
$data = $this->Receive();
if (empty($data)){
return false;
}
$data = json_decode($data[0], true);
if (array_key_exists('result', $data) && $data['result']){
logging("Login success");
return true;
}
logging("Login failed: ".$data['error']['code']." ".$data['error']['message']);
return false;
}
function Main()
{
$this->sock = @fsockopen($this->host, 5000, $errno, $errstr, 5);
if($errno){
logging("Socket open failed");
return;
}
if (!$this->Login()){
return;
}
$query_args = array(
'id'=>$this->ID,
'magic'=>"0x1234",
'method'=>"RecordUpdater.factory.instance",
'params'=>array(
'name'=>"VideoTalkMissedLog"
),
'session'=>$this->SessionID);
$this->Send(json_encode($query_args));
$data = $this->Receive();
if (empty($data)){
logging("Nix Empfangen1");
return false;
}
$data = json_decode($data[0], true);
print_r($data);
$findtoken = $data['result'];
$query_args = array(
'id'=>$this->ID,
'magic'=>"0x1234",
'method'=>"RecordUpdater.clear",
'object'=>$findtoken,
'params'=>array(
'name'=>"VideoTalkMissedLog"
),
'session'=>$this->SessionID);
$this->Send(json_encode($query_args));
$data = $this->Receive();
if (empty($data)){
logging("Nix Empfangen2");
return false;
}
$data = json_decode($data[0], true);
print_r($data);
}
}
?>