如何保护聊天消息免受中间人的攻击(PHP、JavaScript)
How to protect chat messages from man in the middle (PHP, JavaScript)
我目前正在为我的网站开发聊天系统。我不知道如何保护信息的完整性。我目前正在通过进行此操作
chat.class.php
class Chat{
private $config;
private $randomJSONrpc;
private $MySQL;
function __construct($config = 'chat.config.json') {
$this->config = $config;
unset($config);
if(file_exists($this->config)) {
$config = json_decode(file_get_contents($this->config), false);
$config->configfile = $this->config;
$this->config = $config;
unset($config);
} else {
$this->error('Configtest');
}
require_once 'jsonrpc.class.php';
$jsonrpc = new JsonRpcClient('https://api.random.org/json-rpc/1/invoke');
$this->randomJSONrpc = $jsonrpc;
unset($jsonrpc);
$this->MySQL = $this->database();
}
private function database() {
if($this->config->salt == 'random') {
$random = $this->random(8, 'string');
$this->config->salt = $random;
$file = $this->config->configfile;
unset($this->config->configfile);
file_put_contents($file, json_encode($this->config));
}
$mysql_function = $this->config->drivers->mysql;
if($mysql_function == 'mysqli') {
$connection = new MySqLi($this->config->mysql->host, $this->config->mysql->user, $this->config->mysql->password, $this->config->mysql->database)or $this->error('MySQL connection', mysqli_error());
return $connection;
} else {
error('MySQLi connection driver');
}
}
public function hash($input, $algo = 'blowfish') {
if($algo == 'blowfish') {
$hash_algo = '$2a';
$cost = '$10';
} elseif($algo == 'md5') {
$hash_algo = '$1';
$cost = '';
} else {
$this->error('Algo availibility check', 'chat.class.php#class:Chat->hash('.$input.', '.$algo.')');
}
$salt = substr(sha1($this->config->salt),0,22);
return crypt($input, $hash_algo.$cost.'$'.$salt);
}
public function random($length, $address = 'string') {
$jsonrpc = $this->randomJSONrpc;
if($address == 'string') {
$params = new stdClass;
$params->apiKey = $this->config->RANDOMapiKey;
$params->n = 1;
$params->length = $length;
$params->characters = 'abcdefghijklmnopqrstuvwxyz1234567890';
$params->replacement = true;
$data = $jsonrpc->generateStrings($params);
return $data->random->data[0];
} else {
$this->error('JSON-RPC address test');
}
}
public function readNewMessages() {
return 'dev.testing';
}
private function error($test, $extrainfo = false, $status = false) {
if($status == false AND $extrainfo == false) {
die($test.': <span style="color: red;">FAILED</span><br />'.PHP_EOL);
} elseif($status != false AND $extrainfo == false) {
echo $test.': <span style="color: green;">OK</span><br />'.PHP_EOL;
} elseif($status == false AND $extrainfo != false) {
die($test.': <span style="color: red;">FAILED('.$extrainfo.')</span><br />'.PHP_EOL);
} elseif($status != false AND $extrainfo != false) {
echo $test.': <span style="color: green;">OK('.$extrainfo.')</span><br />'.PHP_EOL;
}
}
}
?>
chat.php应该检索新的帖子
<?php
header('Content-Type: application/json');
include 'chat.class.php';
$chat = new Chat();
if(session_id()) {
session_close();
}
$i = 1;
$message = null;
while(!$message) {
sleep(1);
$data = $chat->readNewMessages();
$i++;
}
$response = array('data' => $data, 'itnegrity' => //here I wondered how to save the integrity. );
echo json_encode($message);
?>
我有三样东西,我可能会用到。
- MD5哈希我的消息
- 使用SSL
- 通过客户端生成的密码对消息进行加密,该密码使用用户密码加密后发送到服务器,然后使用用户密码将消息加密后发送回
该应用程序仍在开发中,无法运行。我想使用长轮询从服务器检索消息或检测信号。
选项#1本身不是一个答案,任何人都可以散列,包括攻击者。
如果不使用SSL,MITM可以更改客户端上的代码。只是也许您可以使用消息身份验证代码在没有SSL的情况下交换XML编码的消息(如选项#3所示),但我想知道您会从SSL获得什么;SSL非常可能更高效和更安全。
因此,如果客户端是浏览器,那么最终(通常情况下)就只能使用SSL了。这将是第二种选择。
相关文章:
- 如何照顾CSRF&单页应用程序中不使用cookie的XSS攻击
- Rails:如何在浏览器中显示动态html内容,而不会对其进行转义,也不会引起XSS攻击
- 在Javascript中实现对哈希值的暴力攻击
- XSS脚本攻击攻击->可以't调用javascript
- 域特定cookie是否易受CSRF攻击
- 远程javascript ajax php攻击
- 如何允许HTML标记用于输入而不易受XSS攻击
- 如何防范此类攻击
- 防止鼠标移动对elementFromPoint的攻击
- 通过Node中间人将表单数据发送到Java服务器
- 如何在Java应用程序中阻止XSS攻击并防止用户在网页上执行JavaScript
- 如何保护我的php、jquery和ajax请求免受黑客攻击
- 使用firebase进行javascript注入攻击的风险
- 在我的示例fiddle中,option.text()易受xs攻击
- 如何执行简单的基于 DOM 的 XSS 攻击
- 如果所有浏览器都使用同源策略,CSRF 攻击如何工作?
- 代理第三方脚本是否会引入 XSS 攻击
- 是否有可能在具有内联样式的样式标签上使用XSS进行攻击
- HTML 文本框值属性是否安全免受 XSS 攻击
- 如何保护聊天消息免受中间人的攻击(PHP、JavaScript)