├── .gitignore ├── LICENSE.md ├── README.md ├── client.js ├── cryptochat.js ├── encryption.js ├── package.json └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | #The MIT License (MIT) 2 | 3 | *Copyright (c) 2015 Mateo Gianolio* 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cryptochat 2 | 3 | [![npm](https://img.shields.io/npm/dm/cryptochat.svg?style=flat-square)]() 4 | 5 | [Encrypted](https://github.com/mateogianolio/cryptochat/blob/master/encryption.js) P2P chat over ICMP ([**I**nternet **C**ontrol **M**essage **P**rotocol](https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol)). 6 | 7 | I strongly advise you to pick a high-entropy encryption key to avoid the possibility of brute-force attacks. 8 | 9 | Uses [raw-socket](http://npmjs.org/package/raw-socket) for ICMP handling and [terminal-colors](https://github.com/tinganho/terminal-colors) to spice it up a bit. 10 | 11 | ### Install and usage 12 | 13 | Make sure you have node ```0.10.x``` (*tip:* use [n](https://www.npmjs.com/package/n)) and then install the package globally with ```sudo```. 14 | 15 | ```bash 16 | sudo npm install -g cryptochat 17 | ``` 18 | 19 | Three variants of cryptochat are available depending on your use case: 20 | 21 | * **Send** and **receive** messages 22 | ```bash 23 | $ sudo cryptochat 24 | ``` 25 | 26 | * **Receive** messages 27 | ```bash 28 | $ sudo cryptochat server 29 | ``` 30 | 31 | * **Send** messages 32 | ```bash 33 | $ sudo cryptochat client 34 | ``` 35 | 36 | Because it relies on ```stdin``` for input, it is possible to use pipes to send data: 37 | 38 | ```bash 39 | cat cryptochat.js | sudo cryptochat client 40 | ``` 41 | 42 | ### [ICMP Echo request](https://en.wikipedia.org/wiki/Ping_(networking_utility)) format 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 |
bits 0-7bits 8-15bits 16-31
type = 0x08code = 0x00checksum
identifiersequence number
payload
63 | 64 | The message data is attached as the ICMP payload. 65 | 66 | ### Message 67 | Messages are piped from ```stdin``` and split into payload packages, which are encrypted and sent as ICMP Echo requests. The payload size per request is currently set to 32 bytes. The first byte is the length of the message and the rest is the message itself. 68 | 69 | The first request contains a salt and an initialization vector needed to decrypt the payloads. 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 |
byte 0bytes 1-15bytes 16-31
0x3esaltinitialization vector
83 | 84 | An "end" request is sent in order for the receiver to know when a message is completed. The end request has the following format: 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 |
byte 0bytes 1-31
0x3e0xffffffff...
96 | 97 | When the end request is received, the full message is printed to the screen. 98 | 99 | ### Contribute 100 | 101 | As always, contributions are much appreciated. 102 | -------------------------------------------------------------------------------- /client.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | 'use strict'; 3 | module.exports = function(address, key) { 4 | if(!address) { 5 | console.log('Error: no IP address provided.'); 6 | console.log('cryptochat
'); 7 | return; 8 | } 9 | 10 | if(!key) { 11 | console.log('Error: encryption key missing.'); 12 | console.log('cryptochat
'); 13 | return; 14 | } 15 | 16 | var raw = require('raw-socket'), 17 | crypto = require('./encryption.js'), 18 | socket = raw.createSocket({ 19 | protocol: raw.Protocol.ICMP 20 | }); 21 | 22 | process.stdin.setEncoding('utf8'); 23 | process.stdin.on('data', send); 24 | 25 | function send(msg) { 26 | var payloads = msg.match(/.{1,31}/g), 27 | packets = [], 28 | payload, 29 | length; 30 | 31 | var salt = crypto.salt(), 32 | iv = crypto.iv(), 33 | derivedKey = crypto.key(key, salt); 34 | 35 | packets.push(packet('3e' + salt.toString('hex') + iv.toString('hex'))); 36 | 37 | for(var i = 0; i < payloads.length; i++) { 38 | payload = payloads[i]; 39 | length = ('00' + (payload.length * 2).toString(16)).substr(-2); 40 | payload = length + crypto.encrypt(payload, derivedKey, iv); 41 | 42 | packets.push(packet(payload)); 43 | } 44 | 45 | packets.push(packet('3e')); 46 | 47 | function ping() { 48 | if(!packets.length) 49 | return; 50 | 51 | var packet = packets.shift(); 52 | socket.send(packet, 0, packet.length, address, function(error) { 53 | if(error) 54 | throw error; 55 | 56 | ping(); 57 | }); 58 | } 59 | 60 | ping(); 61 | } 62 | 63 | function packet(message) { 64 | while(message.length < 64) 65 | message += 'f'; 66 | 67 | var buffer = new Buffer(40); 68 | buffer[0] = 0x08; // type 69 | buffer[1] = 0x00; // code 70 | buffer[2] = 0x00; // checksum placeholder 71 | buffer[3] = 0x00; // checksum placeholder 72 | buffer[4] = 0x00; // identifier 73 | buffer[5] = 0x01; // identifier 74 | buffer[6] = 0x0a; // seqnum 75 | buffer[7] = 0x09; // seqnum 76 | buffer.write(message, 8, 'hex'); 77 | raw.writeChecksum(buffer, 2, raw.createChecksum(buffer)); 78 | 79 | return buffer; 80 | } 81 | }; 82 | }()); 83 | -------------------------------------------------------------------------------- /cryptochat.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | (function() { 4 | 'use strict'; 5 | 6 | var args = process.argv; 7 | 8 | args.shift(); // ignore 'node' 9 | args.shift(); // ignore path 10 | 11 | var ip, 12 | key; 13 | 14 | var type = args.shift(); 15 | if(type === 'server') { 16 | key = args.pop(); 17 | require('./server.js')(key); 18 | } else if(type === 'client') { 19 | key = args.pop(); 20 | ip = args.pop(); 21 | require('./client.js')(ip, key); 22 | } else if(args.length === 1) { 23 | ip = type; 24 | key = args.pop(); 25 | require('./server.js')(key); 26 | require('./client.js')(ip, key); 27 | } else { 28 | console.log('Error: invalid format.\n'); 29 | console.log('cryptochat \t# send and receive messages'); 30 | console.log('cryptochat server \t# receive messages'); 31 | console.log('cryptochat client \t# send messages\n'); 32 | process.exit(); 33 | } 34 | }()); 35 | -------------------------------------------------------------------------------- /encryption.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | 'use strict'; 3 | 4 | var ENCRYPTION = 'AES-256-CTR'; 5 | var crypto = require('crypto'); 6 | 7 | module.exports = { 8 | encrypt: function(str, key, iv) { 9 | var cipher = crypto.createCipheriv(ENCRYPTION, key, iv), 10 | encrypted = cipher.update(str, 'utf8', 'hex'); 11 | encrypted += cipher.final('hex'); 12 | return encrypted; 13 | }, 14 | decrypt: function(str, key, iv) { 15 | var decipher = crypto.createDecipheriv(ENCRYPTION, key, iv), 16 | decrypted = decipher.update(str, 'hex', 'utf8'); 17 | 18 | decrypted += decipher.final('utf8'); 19 | return decrypted; 20 | }, 21 | salt: function() { 22 | return crypto.randomBytes(15); 23 | }, 24 | iv: function() { 25 | return crypto.randomBytes(16); 26 | }, 27 | key: function(secret, salt) { 28 | return crypto.pbkdf2Sync(secret, salt, 4096, 32); 29 | }, 30 | hex2bytes: function(hex) { 31 | if(hex.length % 2 !== 0) 32 | throw new Error(hex + ' is not a valid hex string'); 33 | 34 | var bytes = new Buffer(hex.length / 2); 35 | for(var i = 0; i < hex.length; i += 2) { 36 | bytes[i / 2] = parseInt(hex.substr(i, 2), 16); 37 | } 38 | 39 | return bytes; 40 | } 41 | }; 42 | }()); 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cryptochat", 3 | "version": "1.1.5", 4 | "description": "encrypted P2P chat over ICMP", 5 | "bin": { 6 | "cryptochat": "./cryptochat.js" 7 | }, 8 | "dependencies": { 9 | "raw-socket": "^1.3.2", 10 | "terminal-colors": "^0.1.3" 11 | }, 12 | "devDependencies": {}, 13 | "scripts": { 14 | "test": "echo \"Error: no test specified\" && exit 1" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/mateogianolio/cryptochat.git" 19 | }, 20 | "keywords": [ 21 | "aes", 22 | "aes-256", 23 | "encrypted", 24 | "encrypted chat", 25 | "chat", 26 | "chat application", 27 | "chatting", 28 | "crypto", 29 | "cryptography", 30 | "icmp", 31 | "ping", 32 | "internet control message protocol", 33 | "ip", 34 | "internet protocol" 35 | ], 36 | "author": "Mateo Gianolio", 37 | "license": "MIT" 38 | } 39 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | 'use strict'; 3 | module.exports = function(key) { 4 | require('terminal-colors'); 5 | 6 | var DEBUG = false; 7 | var raw = require('raw-socket'), 8 | crypto = require('./encryption.js'), 9 | socket = raw.createSocket({ 10 | protocol: raw.Protocol.ICMP 11 | }); 12 | 13 | var message = '', 14 | count = 0, 15 | salt, 16 | iv, 17 | derivedKey, 18 | offset, 19 | type, 20 | length, 21 | hex; 22 | 23 | socket.on('message', listen); 24 | function listen(buffer, source) { 25 | // convert buffer to hex string, ignore IP header (20 bytes) 26 | buffer = buffer.toString('hex', 20, buffer.length); 27 | 28 | /** 29 | * types 30 | * 0x00 = ICMP Echo reply, 31 | * 0x08 = ICMP Echo request 32 | **/ 33 | type = parseInt(buffer.substring(0, 2), 16); 34 | length = parseInt(buffer.substring(16, 18), 16); 35 | 36 | // get message 37 | offset = 18; 38 | hex = buffer.substring(offset, offset + length); 39 | 40 | if(DEBUG) { 41 | console.log('\nping #' + count); 42 | console.log('buffer:', buffer); 43 | console.log('type:', type === 0 ? 'ICMP Echo reply' : 'ICMP Echo request'); 44 | console.log('message length:', length); 45 | console.log('message content:', hex); 46 | } 47 | 48 | // ignore ICMP Echo replies 49 | if(type === 0) 50 | return; 51 | 52 | // encountered end ping 53 | if((hex.match(/f/g) || []).length === length) { 54 | if(message) { 55 | process.stdout.write(source.green + ' '); 56 | process.stdout.write('[' + (count + 1) + ']: '); 57 | process.stdout.write(message.bold + '\n'); 58 | } 59 | 60 | count = 0; 61 | message = ''; 62 | 63 | return; 64 | } 65 | 66 | // first ping 67 | if(count === 0) { 68 | salt = crypto.hex2bytes(hex.substring(0, 30)); 69 | iv = crypto.hex2bytes(hex.substring(30, 62)); 70 | derivedKey = crypto.key(key, salt); 71 | 72 | count++; 73 | return; 74 | } 75 | 76 | message += crypto.decrypt(hex, derivedKey, iv); 77 | count++; 78 | } 79 | }; 80 | }()); 81 | --------------------------------------------------------------------------------