【问题标题】:How to protect chat messages from man in the middle (PHP, JavaScript)如何保护来自中间人的聊天消息(PHP、JavaScript)
【发布时间】:2014-11-02 06:30:49
【问题描述】:

我目前正在为我的网站开发聊天系统。我不知道如何保护消息的完整性。我目前正在通过

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);
?>

我有三样东西,我可能会用到。

  1. MD5 散列我的消息
  2. 使用 SSL
  3. 通过客户端生成的密码加密消息,该密码使用用户密码加密发送到服务器,然后使用用户密码加密发送回。

该应用程序仍在开发中,无法运行。我想使用长轮询从服务器检索消息或心跳。

【问题讨论】:

  • 您为什么怀疑中间人攻击是一种可能的情况?
  • 我认为这不太可能发生。但我想构建一个 100% 安全的聊天应用程序。如果中间有人可以编辑聊天协议以进行网络钓鱼或点击劫持等操作,这还不够好。
  • MitM 攻击非常难以处理,因为根据定义,攻击者可以使用所有通信。客户端必须登录服务,这不可避免地会暴露密码。
  • 是的。但是您可以使用 md5 或 sha256 将其加密甚至在客户端对其进行哈希处理,甚至可以在服务器端使用 blowfish 进行加密。
  • 2.是唯一的出路。 3.如果通过SSL完成密钥的初始建立是可能的。

标签: javascript php encryption chat data-integrity


【解决方案1】:

选项#1 本身并不是一个答案,任何人都可以散列,包括攻击者。

如果不使用 SSL,MITM 可以更改客户端上的代码。只是也许您可以使用消息身份验证代码在不使用 SSL 的情况下交换 XML 编码消息(如选项 #3),但我想知道您会通过 SSL 获得什么; SSL非常可能更高效且安全。

所以最后 - 通常情况下 - 如果客户端是浏览器,您将使用 SSL。那将是选项 #2。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多