-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpastebin.php
More file actions
140 lines (118 loc) · 3.22 KB
/
pastebin.php
File metadata and controls
140 lines (118 loc) · 3.22 KB
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
<?php
/*
* This file is part of Defuse Security's Secure Pastebin
* Find updates at: https://defuse.ca/pastebin.htm
* Developer contact: havoc AT defuse.ca
* This code is in the public domain. There is no warranty.
*/
require_once('PasswordGenerator.php');
// Database connection
require_once('/etc/creds.php');
$creds = Creds::getCredentials("pastebin");
mysql_connect($creds[C_HOST],$creds[C_USER],$creds[C_PASS]);
@mysql_select_db($creds[C_DATB]) or die( "Unable to select database");
unset($creds);
// Constants
define("IV_BYTES", 16);
function commit_post($text, $jsCrypt, $lifetime_seconds, $short = false)
{
do {
$urlKey = PasswordGenerator::getAlphaNumericPassword($short ? 8 : 22);
} while( retrieve_post( $urlKey ) !== false );
$id = get_database_id($urlKey);
$encryptionKey = get_encryption_key($urlKey);
$iv = mcrypt_create_iv(IV_BYTES, MCRYPT_DEV_URANDOM);
$encrypted = SafeEncode(
$iv .
mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
$encryptionKey,
$text,
MCRYPT_MODE_CBC,
$iv
)
);
$jsCrypted = $jsCrypt ? 1 : 0;
$time = (int)(time() + $lifetime_seconds);
mysql_query(
"INSERT INTO pastes (token, data, time, jscrypt)
VALUES('$id', '$encrypted', '$time', '$jsCrypted')"
);
return $urlKey;
}
function retrieve_post($urlKey)
{
$id = mysql_real_escape_string(get_database_id($urlKey));
$query = mysql_query("SELECT * FROM `pastes` WHERE token='$id'");
if(mysql_num_rows($query) > 0)
{
$cols = mysql_fetch_array($query);
$postInfo = array();
$postInfo['timeleft'] = $cols['time'] - time();
$postInfo['jscrypt'] = $cols['jscrypt'] == "1";
$encryptionKey = get_encryption_key($urlKey);
$ciphertext = SafeDecode($cols['data']);
$iv = substr($ciphertext, 0, IV_BYTES);
$encryptedText = substr($ciphertext, IV_BYTES);
$postInfo['text'] =
str_replace("\0", "",
mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$encryptionKey,
$encryptedText,
MCRYPT_MODE_CBC,
$iv
)
);
return $postInfo;
}
else
return false;
}
function delete_expired_posts()
{
$now = time();
mysql_query("DELETE FROM pastes WHERE time <= '$now'");
}
function get_database_id($urlKey)
{
return hash_hmac("SHA256", "database_identity", $urlKey, false);
}
function get_encryption_key($urlKey)
{
return hash_hmac("SHA256", "encryption_key", $urlKey, true);
}
function SafeEncode($data)
{
return mysql_real_escape_string(base64_encode($data));
}
function SafeDecode($data)
{
return base64_decode($data);
}
function smartslashes($data)
{
if(get_magic_quotes_gpc())
{
return stripslashes($data);
}
else
{
return $data;
}
}
// Escapes a string so that it is safe to include into a JavaScript string
// literal.
function js_string_escape($data)
{
$safe = "";
for($i = 0; $i < strlen($data); $i++)
{
if(ctype_alnum($data[$i]))
$safe .= $data[$i];
else
$safe .= sprintf("\\x%02X", ord($data[$i]));
}
return $safe;
}
?>