?????????????????????????????????????????????????÷????????
???????????????????????????????????????????SSN????
??????????????漲?????SSN?????洢??????
?????????????SSN????????????????????
?????????????????????????Щ?????????????????????????????????????????????????????????????а?????????????????????????????????
<?php
class InsecureExampleOne
{
protected $db;
protected $key;
public function __construct(PDO $db?? string $key = '')
{
$this->db = $db;
$this->key = $key;
}
public function searchByValue(string $query): array
{
$stmt = $this->db->prepare('SELECT * FROM table WHERE column = ?');
$stmt->execute([
$this->insecureEncryptDoNotUse($query)
]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
protected function insecureEncryptDoNotUse(string $plaintext): string
{
return bin2hex(
openssl_encrypt(
$plaintext??
'aes-128-ecb'??
$this->key??
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING
)
);
}
}
???????????????????У?????????????????????????????????????????????????????????ECB???????16????鱻???????????????????Щ????????????
??????????????????????Щ?????????????????????????????????????????????е???????????
???????????????????????????????????????κβ???????????????????????????ECB????CBC?????????????IV??
??????????????????????????????ζ??????????????????????????????????????????????????
??????????
?????????????о??漰???????????????????????????????????
???????????????????????????????????????????????á?
???????磬????????????й???????????????????
?????????????????????′????????????????????????????????
?????????????δ????? RSA???????
??????????????????????????????????????????????????????????????м????????????????????RSA?????????????????е?RSA??????????????????????????????
?????????????μ?AES?????XOR??????
???????????nonce-reuse??CTR??????????????????????????????NMR????????
????????????????????????????????????漰????????????????????????????????????б????????????????????÷?????????????????????????о???ó?????????????????
????????У???????е?????о??п???????????μ???????????????????????????????????Э???????о?????κν???????????????????У?????????????????????????????о?????????????
????????????????????????????????κδ????????μ???????????????????????????????????????????????????????????????????????е???????????????????е???????????????????ó?????????????????????
??????????????????????????????????????????????????ó????????DOS????????????????????????????????Щ???????????????????????????????????????????????????????
??????? ???? ????????ü?
?????????????????????/???????????и????????????????????????????????????????????????д?????????????????????????????????
???????????????????????????????????????????????????????????????????????????????????????????????Щ?????????????XSalsa20-Poly1305??XChacha20-Poly1305?????????CAESAR?ó???????????????NORX64-4-1??????????NaCl????libsodium???????crypto_secretbox????????á?
???????????????????????????????????????????????????????
??????????????????????ü??????????????????????????????????????????????????????????????????????????????????
???????????????????????????????????????????????????????“????洢”???????????SQL???????CRUD??ó???????????????????????????SQL?????????
????????????????????????????????ζ?????????/???????????????????????????????????????????????£?????ó???????????????????????????????????????????????
??????????????????????
???????????????洢??????????????????в????
???????洢??????????????SELECT????????????????????????????????????????????????????????????????????HMAC???洢??????????С???????????????????????????????????????δ???
????????????е???????????????HMAC???????????ü????????ε????????????PBKDF2-SHA256??scrypt??Argon2??????????????????????????κ????????μ?????????????????????????????????????洢????????У???
??????????????????????????????????PostgreSQL?У???
CREATE TABLE humans (
humanid BIGSERIAL PRIMARY KEY??
first_name TEXT??
last_name TEXT??
ssn TEXT?? /* encrypted */
ssn_bidx TEXT /* blind index */
);
CREATE INDEX ON humans (ssn_bidx);
?????????洢??????humans.ssn??????SSN??????????????humans.ssn_bidx?????????????????????????????????
<?php
/* This is not production-quality code.
* It's optimized for readability and understanding?? not security.
*/
function encryptSSN(string $ssn?? string $key): string
{
$nonce = random_bytes(24);
$ciphertext = sodium_crypto_secretbox($ssn?? $nonce?? $key);
return bin2hex($nonce . $ciphertext);
}
function decryptSSN(string $ciphertext?? string $key): string
{
$decoded = hex2bin($ciphertext);
$nonce = mb_substr($decoded?? 0?? 24?? '8bit');
$cipher = mb_substr($decoded?? 24?? null?? '8bit');
return sodium_crypto_secretbox_open($cipher?? $nonce?? $key);
}
function getSSNBlindIndex(string $ssn?? string $indexKey): string
{
return bin2hex(
sodium_crypto_pwhash(
32??
$ssn??
$indexKey??
SODIUM_CRYPTO_PWHASH_OPSLIMIT_MODERATE??
SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE
)
);
}
function findHumanBySSN(PDO $db?? string $ssn?? string $indexKey): array
{
$index = getSSNBlindIndex($ssn?? $indexKey);
$stmt = $db->prepare('SELECT * FROM humans WHERE ssn_bidx = ?');
$stmt->execute([$index]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
???????????B-Sides Orlando 2017????????????????????????POC???????????????CC0???????????????????????????????????“????????”?????