This repository has been archived by the owner on Dec 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
/
snapchat_agent.php
560 lines (502 loc) · 14.7 KB
/
snapchat_agent.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/**
* @file
* Provides the Snapchat class with a lower-level API layer to handle
* requests and decrypt responses.
*/
abstract class SnapchatAgent {
/*
* Before updating this value, confirm
* that the library requests everything in the same way as the app.
*/
const USER_AGENT = 'Snapchat/9.16.2.0 (HTC One; Android 5.0.2#482424.2#21; gzip)';
/*
* The API URL. We're using the /bq endpoint, the one that the iPhone
* uses. Android clients still seem to be using the /ph endpoint.
*
* @todo
* Make library capable of using different endpoints (some of the
* resource names are different, so they aren't interchangeable).
*/
const URL = 'https://feelinsonice-hrd.appspot.com';
/*
* The API secret. Used to create access tokens.
*/
const SECRET = 'iEk21fuwZApXlz93750dmW22pw389dPwOk';
/*
* The static token. Used when no session is available.
*/
const STATIC_TOKEN = 'm198sOkJEn37DjqZ32lpRu76xmw288xSQ9';
/*
* The blob encryption key. Used to encrypt and decrypt media.
*/
const BLOB_ENCRYPTION_KEY = 'M02cnQ51Ji97vwT4';
/*
* The hash pattern.
*
* @see self::hash()
*/
const HASH_PATTERN = '0001110111101110001111010101111011010001001110011000110001000110'; // Hash pattern
protected $proxyServer;
/**
* Default cURL options. It doesn't appear that the UA matters, but
* authenticity, right?
*/
public static $CURL_OPTIONS = array(
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 10,
CURLOPT_USERAGENT => self::USER_AGENT,
CURLOPT_HTTPHEADER => array('Accept-Language: en', 'Accept-Locale: en_US'),
);
public $gauth = "";
public static $CURL_HEADERS = array(
'Accept-Language: en',
'Accept-Locale: en_US'
);
public function getGAuth(){
return (!empty($this->gauth) ? $this->gauth : "");
}
public function setGAuth($auth)
{
if (is_array($auth))
$this->gauth = $auth['auth'];
else
$this->gauth = $auth;
}
/**
* Returns the current timestamp.
*
* @return int
* The current timestamp, expressed in milliseconds since epoch.
*/
public function timestamp()
{
return round(microtime(TRUE) * 1000);
}
/**
* Pads data using PKCS5.
*
* @param data $data
* The data to be padded.
* @param int $blocksize
* The block size to pad to. Defaults to 16.
*
* @return data
* The padded data.
*/
public function pad($data, $blocksize = 16)
{
$pad = $blocksize - (strlen($data) % $blocksize);
return $data . str_repeat(chr($pad), $pad);
}
/**
* Decrypts blob data for standard images and videos.
*
* @param data $data
* The data to decrypt.
*
* @return data
* The decrypted data.
*/
public function decryptECB($data)
{
return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, self::BLOB_ENCRYPTION_KEY, self::pad($data), MCRYPT_MODE_ECB);
}
/**
* Encrypts blob data for standard images and videos.
*
* @param data $data
* The data to encrypt.
*
* @return data
* The encrypted data.
*/
public function encryptECB($data)
{
return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, self::BLOB_ENCRYPTION_KEY, self::pad($data), MCRYPT_MODE_ECB);
}
/**
* Decrypts blob data for stories.
*
* @param data $data
* The data to decrypt.
* @param string $key
* The base64-encoded key.
* @param string $iv
* $iv The base64-encoded IV.
*
* @return data
* The decrypted data.
*/
public function decryptCBC($data, $key, $iv)
{
// Decode the key and IV.
$iv = base64_decode($iv);
$key = base64_decode($key);
// Decrypt the data.
$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);
$padding = ord($data[strlen($data) - 1]);
return substr($data, 0, -$padding);
}
/**
* Implementation of Snapchat's hashing algorithm.
*
* @param string $first
* The first value to use in the hash.
* @param string $second
* The second value to use in the hash.
*
* @return string
* The generated hash.
*/
public function hash($first, $second)
{
// Append the secret to the values.
$first = self::SECRET . $first;
$second = $second . self::SECRET;
// Hash the values.
$hash = hash_init('sha256');
hash_update($hash, $first);
$hash1 = hash_final($hash);
$hash = hash_init('sha256');
hash_update($hash, $second);
$hash2 = hash_final($hash);
// Create a new hash with pieces of the two we just made.
$result = '';
for($i = 0; $i < strlen(self::HASH_PATTERN); $i++)
{
$result .= substr(self::HASH_PATTERN, $i, 1) ? $hash2[$i] : $hash1[$i];
}
return $result;
}
/**
* Checks to see if a blob looks like a media file.
*
* @param data $data
* The blob data (or just the header).
*
* @return bool
* TRUE if the blob looks like a media file, FALSE otherwise.
*/
function isMedia($data)
{
// Check for a JPG header.
if($data[0] == chr(0xFF) && $data[1] == chr(0xD8))
{
return TRUE;
}
// Check for a MP4 header.
if($data[0] == chr(0x00) && $data[1] == chr(0x00))
{
return TRUE;
}
return FALSE;
}
/**
* Checks to see if a blob looks like a compressed file.
*
* @param data $data
* The blob data (or just the header).
*
* @return bool
* TRUE if the blob looks like a compressed file, FALSE otherwise.
*/
function isCompressed($data)
{
// Check for a PK header.
if($data[0] == chr(0x50) && $data[1] == chr(0x4B))
{
return TRUE;
}
return FALSE;
}
/**
* Uncompress the blob and put the data into an Array.
* Array(
* overlay~zip-CE6F660A-4A9F-4BD6-8183-245C9C75B8A0 => overlay_file_data,
* media~zip-CE6F660A-4A9F-4BD6-8183-245C9C75B8A0 => m4v_file_data
* )
*
* @param data $data
* The blob data (or just the header).
*
* @return array
* Array containing both file contents, or FALSE if couldn't extract.
*/
function unCompress($data)
{
if(!file_put_contents("./temp", $data))
{
exit('Should have write access to own folder');
}
$resource = zip_open("./temp");
$result = FALSE;
if(is_resource($resource))
{
while($zip_entry = zip_read($resource))
{
$filename = zip_entry_name($zip_entry);
if(zip_entry_open($resource, $zip_entry, "r"))
{
$result[$filename] = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}
else
{
unlink("./temp");
return FALSE;
}
}
zip_close($resource);
}
unlink("./temp");
return $result;
}
/**
* Performs a GET request. Currently only used for story blobs.
*
* @todo
* cURL-ify this and maybe combine with the post function.
*
* @param string $endpoint
* The address of the resource being requested (e.g. '/story_blob' or
* '/story_thumbnail').
*
* @return data
* The retrieved data.
*/
public function get($endpoint)
{
$ch = curl_init();
curl_setopt_array($ch, array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_USERAGENT => self::USER_AGENT, CURLOPT_HTTPHEADER => array('Accept-Language: en', 'Accept-Locale: en_US'),CURLOPT_URL => self::URL . $endpoint, CURLOPT_CAINFO => dirname(__FILE__) . '/ca_bundle.crt'));
return curl_exec($ch);
}
/**
* Performs a POST request. Used for pretty much everything.
*
* @todo
* Replace the blob endpoint check with a more robust check for
* application/octet-stream.
*
* @param string $endpoint
* The address of the resource being requested (e.g. '/update_snaps' or
* '/friend').
* @param array $data
* An dictionary of values to send to the API. A request token is added
* automatically.
* @param array $params
* An array containing the parameters used to generate the request token.
* @param bool $multipart
* If TRUE, sends the request as multipart/form-data. Defaults to FALSE.
*
* @return mixed
* The data returned from the API (decoded if JSON). Returns FALSE if
* the request failed.
*/
public function post($endpoint, $data, $params, $multipart = FALSE, $debug = FALSE)
{
$ch = curl_init();
$data['req_token'] = self::hash($params[0], $params[1]);
$boundary = "Boundary+0xAbCdEfGbOuNdArY";//md5(time());
if(!$multipart)
{
$data = http_build_query($data);
}
else
{
$datas = "--".$boundary."\r\n" . 'Content-Disposition: form-data; name="req_token"' . "\r\n\r\n" . self::hash($params[0], $params[1]) . "\r\n";
foreach($data as $key => $value)
{
if($key == "req_token") continue;
if($key != 'data')
{
$datas .= "--".$boundary."\r\n" . 'Content-Disposition: form-data; name="' . $key . '"' . "\r\n\r\n" . $value . "\r\n";
}
else
{
$datas .= "--".$boundary."\r\n" . 'Content-Disposition: form-data; name="data"; filename="data"'."\r\n" . 'Content-Type: application/octet-stream'."\r\n\r\n" . $value . "\r\n";
}
}
$data = $datas . "--".$boundary."--";
}
$options = self::$CURL_OPTIONS;
if($debug)
{
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
}
if($endpoint == "/loq/login" || $endpoint == "/loq/register_username")
{
$headers = array_merge(self::$CURL_HEADERS, array(
"X-Snapchat-Client-Auth-Token: Bearer {$params[2]}",
"X-Snapchat-Client-Auth: {$params[3]}",
"Accept-Encoding: gzip"));
}
else
{
$headers = array_merge(self::$CURL_HEADERS, array("X-Snapchat-Client-Auth-Token: Bearer ". $this->gauth));
}
if($multipart)
{
$headers = array_merge($headers, array("X-Timestamp: 0","Content-Type: multipart/form-data; boundary=$boundary"));
}
if($endpoint == '/ph/blob' || $endpoint == '/bq/blob' || $endpoint == '/bq/chat_media')
{
$headers = array_merge($headers, array("X-Timestamp: " . $params[1]));
$options += array(
CURLOPT_URL => self::URL . $endpoint . "?{$data}"
);
}
else
{
$options += array(
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $data,
CURLOPT_URL => self::URL . $endpoint
);
}
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$headerBuff = tmpfile();
curl_setopt($ch, CURLOPT_WRITEHEADER, $headerBuff);
curl_setopt($ch, CURLOPT_PROXY, $this->proxyServer);
$result = curl_exec($ch);
// If cURL doesn't have a bundle of root certificates handy, we provide
// ours (see http://curl.haxx.se/docs/sslcerts.html).
if (curl_errno($ch) == 60) {
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/ca_bundle.crt');
$result = curl_exec($ch);
}
if(strlen($result) > 0) //make sure curl worked. if not, keep going
{
if($endpoint == "/loq/login") $result = gzdecode($result);
}
if($debug)
{
$info = curl_getinfo($ch);
echo "\nREQUEST TO: " .self::URL . $endpoint . "\n";
if(isset($info['request_header']))
echo "\nSent Request info: " .print_r($info['request_header'], true). "\n";
if(is_array($data))
{
if ($multipart)
echo 'DATA: ' . strlen($data) . " byte data\n";
else
echo 'DATA: ' . print_r($data) . "\n";
}
else
{
if ($multipart)
echo 'DATA: ' . strlen($data) . " byte data\n";
else
echo 'DATA: ' . $data . "\n";
}
if($endpoint == "/loq/login" || $endpoint == "/all_updates")
{
if (strpos($result,'401 UNAUTHORIZED') !== false)
{
echo "\nRESULT: 401 UNAUTHORIZED\n";
exit();
}
$jsonResult = json_decode($result);
echo 'RESULT: ' . print_r($jsonResult) . "\n";
if (property_exists($jsonResult, "status") && $jsonResult->status == '-103')
exit();
}
else
{
if (strpos($result,'400 BAD_REQUEST') !== false)
echo "\nRESULT: 400 BAD REQUEST\n";
else
echo 'RESULT: ' . $result . "\n";
}
if($endpoint == '/loq/register_username' || $endpoint == '/loq/register')
{
$jsonResult = json_decode($result);
if(isset($jsonResult->logged) && $jsonResult->logged == false)
{
echo "\n" . 'ERROR: There was an error registering your account: ' . $jsonResult->message . "\n";
exit();
}
}
}
if($endpoint == "/bq/get_captcha")
{
file_put_contents(__DIR__."/captcha.zip", $result);
rewind($headerBuff);
$headers = stream_get_contents($headerBuff);
if(preg_match('/^Content-Disposition: .*?filename=(?<f>[^\s]+|\x22[^\x22]+\x22)\x3B?.*$/m', $headers, $matches))
{
$filename = trim($matches['f'],' ";');
rename(__DIR__."/captcha.zip", __DIR__."/{$filename}");
return $filename;
}
fclose($headerBuff);
return "captcha.zip";
}
$gi = curl_getinfo($ch);
// If the cURL request fails, return FALSE. Also check the status code
// since the API generally won't return friendly errors.
if($result === FALSE)
{
curl_close($ch);
return $result;
}
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200)
{
$return['data'] = $result;
$return['test'] = 1;
$return['code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $return;
}
curl_close($ch);
$return['error'] = 0;
if($endpoint == '/ph/blob' || $endpoint == '/bq/blob' || $endpoint == "/bq/snaptag_download" || $endpoint == '/bq/chat_media')
{
$return['data'] = $result;
return $result;
}
// Add support for foreign characters in the JSON response.
$result = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($result));
$return['data'] = json_decode($result);
return $return;
}
public function posttourl($url, $data) {
$ch = curl_init();
$options = self::$CURL_OPTIONS + array(
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $data,
CURLOPT_URL => $url,
);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_HTTPHEADER, self::$CURL_HEADERS);
$result = curl_exec($ch);
if (curl_errno($ch) == 60) {
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/ca_bundle.crt');
$result = curl_exec($ch);
}
if ($result === FALSE)
{
curl_close($ch);
return $result;
}
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200)
{
$return['data'] = $result;
$return['test'] = 1;
$return['code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $return;
}
curl_close($ch);
$return['error'] = 0;
$result = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($result));
$return['data'] = json_decode($result);
return $return;
}
public function setProxyServer ($proxyServer)
{
$this->proxyServer = $proxyServer;
}
}