Commit 47d84f80 authored by Benjamin Chelli's avatar Benjamin Chelli

Make MessageId, ProcessId, SessionId unique at the connection level

parent 23582d1d
......@@ -14,6 +14,7 @@ module.exports = message({
'Command':'CLOSE'
, 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
}
, request:{
'FileId':params.FileId
......
......@@ -16,6 +16,7 @@ module.exports = message({
'Command':'CREATE'
, 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
}
, request:{
'Buffer':buffer
......
......@@ -16,6 +16,7 @@ module.exports = message({
'Command':'CREATE'
, 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
}
, request:{
'Buffer':buffer
......
......@@ -12,6 +12,7 @@ module.exports = message({
return new SMB2Message({
headers:{
'Command':'NEGOTIATE'
, 'ProcessId':connection.ProcessId
}
});
......
......@@ -16,6 +16,7 @@ module.exports = message({
'Command':'CREATE'
, 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
}
, request:{
'Buffer':buffer
......
......@@ -16,6 +16,7 @@ module.exports = message({
'Command':'CREATE'
, 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
}
, request:{
'Buffer':buffer
......
......@@ -14,6 +14,7 @@ module.exports = message({
'Command':'QUERY_DIRECTORY'
, 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
}
, request:{
'FileId':params.FileId
......
......@@ -14,6 +14,7 @@ module.exports = message({
'Command':'READ'
, 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
}
, request:{
'FileId':file.FileId
......
......@@ -13,6 +13,7 @@ module.exports = message({
return new SMB2Message({
headers:{
'Command':'SESSION_SETUP'
, 'ProcessId':connection.ProcessId
}
, request:{
'Buffer':ntlm.encodeType1(
......
......@@ -14,6 +14,7 @@ module.exports = message({
headers:{
'Command':'SESSION_SETUP'
, 'SessionId':connection.SessionId
, 'ProcessId':connection.ProcessId
}
, request:{
'Buffer':ntlm.encodeType3(
......
......@@ -29,6 +29,7 @@ module.exports = message({
'Command':'SET_INFO'
, 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
}
, request:{
'FileInfoClass':fileInfoClasses[params.FileInfoClass]
......
......@@ -13,6 +13,7 @@ module.exports = message({
headers:{
'Command':'TREE_CONNECT'
, 'SessionId':connection.SessionId
, 'ProcessId':connection.ProcessId
}
, request:{
'Buffer':new Buffer(connection.fullPath, 'ucs2')
......
......@@ -14,6 +14,7 @@ module.exports = message({
'Command':'WRITE'
, 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
}
, request:{
'FileId':params.FileId
......
......@@ -36,6 +36,9 @@ var SMB = module.exports = function(opt){
// set default port
this.port = opt.port || port;
// set message id
this.messageId = 0;
// extract share
this.share = matches[2];
......@@ -54,7 +57,19 @@ var SMB = module.exports = function(opt){
this.password = opt.password;
// set session id
this.SessionId = 0;
this.SessionId = Math.floor(Math.random()*256) & 0xFF;
// set the process id
// https://msdn.microsoft.com/en-us/library/ff470100.aspx
this.ProcessId = new Buffer([
Math.floor(Math.random()*256) & 0xFF,
Math.floor(Math.random()*256) & 0xFF,
Math.floor(Math.random()*256) & 0xFF,
Math.floor(Math.random()*256) & 0xFE
]);
// activate debug mode
this.debug = opt.debug;
// init connection (socket)
SMB2Connection.init(this);
......@@ -68,15 +83,15 @@ var proto = SMB.prototype = {};
proto.close = require('./api/close');
proto.exists = SMB2Connection.requireConnect(require('./api/exists'));
proto.exists = SMB2Connection.requireConnect(require('./api/exists'));
proto.readFile = SMB2Connection.requireConnect(require('./api/readfile'));
proto.readFile = SMB2Connection.requireConnect(require('./api/readfile'));
proto.writeFile = SMB2Connection.requireConnect(require('./api/writefile'));
proto.unlink = SMB2Connection.requireConnect(require('./api/unlink'));
proto.unlink = SMB2Connection.requireConnect(require('./api/unlink'));
proto.readdir = SMB2Connection.requireConnect(require('./api/readdir'));
proto.rmdir = SMB2Connection.requireConnect(require('./api/rmdir'));
proto.mkdir = SMB2Connection.requireConnect(require('./api/mkdir'));
proto.readdir = SMB2Connection.requireConnect(require('./api/readdir'));
proto.rmdir = SMB2Connection.requireConnect(require('./api/rmdir'));
proto.mkdir = SMB2Connection.requireConnect(require('./api/mkdir'));
......
......@@ -89,7 +89,7 @@ SMB2Forge.response = function(c){
* HELPERS
*/
function sendNetBiosMessage(connection, message) {
var smbRequest = message.getBuffer();
var smbRequest = message.getBuffer(connection);
if(connection.debug){
console.log('--request');
......
/*
* STATICS
*/
var messageId = 0;
/*
* CONSTANTS
*/
const protocolId = new Buffer([0xFE, 'S'.charCodeAt(0), 'M'.charCodeAt(0), 'B'.charCodeAt(0)])
, processId = new Buffer([0xFF, 0xFE, 0x00, 0x00])
, headerTranslates = {
'Command': {
'NEGOTIATE': 0x0000
......@@ -50,38 +41,42 @@ const protocolId = new Buffer([0xFE, 'S'.charCodeAt(0), 'M'.charCodeAt(0), 'B'.c
, headerLength = 64
, headerSync = [
['ProtocolId',4,protocolId]
, ['StructureSize',2,headerLength]
, ['CreditCharge',2,0]
, ['Status',4,0]
, ['Command',2]
, ['Credit',2,126]
, ['Flags',4,0]
, ['NextCommand',4,0]
, ['MessageId',4]
, ['MessageIdHigh',4,0]
, ['ProcessId',4,processId]
, ['TreeId',4,0]
, ['SessionId',8,0]
, ['Signature',16,0]
]
, headerASync = [
['ProtocolId',4,protocolId]
, ['StructureSize',2,headerLength]
, ['CreditCharge',2,0]
, ['Status',4,0]
, ['Command',2]
, ['Credit',2,126]
, ['Flags',4,0]
, ['NextCommand',4,0]
, ['MessageId',4]
, ['MessageIdHigh',4,0]
, ['AsyncId',8]
, ['SessionId',8,0]
, ['Signature',16,0]
]
, headerSync = function(processId, sessionId) {
return [
['ProtocolId',4,protocolId]
, ['StructureSize',2,headerLength]
, ['CreditCharge',2,0]
, ['Status',4,0]
, ['Command',2]
, ['Credit',2,126]
, ['Flags',4,0]
, ['NextCommand',4,0]
, ['MessageId',4]
, ['MessageIdHigh',4,0]
, ['ProcessId',4,processId]
, ['TreeId',4,0]
, ['SessionId',8,sessionId]
, ['Signature',16,0]
];
}
, headerASync = function(processId, sessionId) {
return [
['ProtocolId',4,protocolId]
, ['StructureSize',2,headerLength]
, ['CreditCharge',2,0]
, ['Status',4,0]
, ['Command',2]
, ['Credit',2,126]
, ['Flags',4,0]
, ['NextCommand',4,0]
, ['MessageId',4]
, ['MessageIdHigh',4,0]
, ['AsyncId',8]
, ['SessionId',8,sessionId]
, ['Signature',16,0]
];
}
;
......@@ -134,7 +129,7 @@ proto.getResponse = function(){
proto.getBuffer = function(){
proto.getBuffer = function(connection){
var buffer = new Buffer(0xFFFF)
, length = 0
;
......@@ -142,7 +137,7 @@ proto.getBuffer = function(){
// SET MESSAGE ID
if(!this.isMessageIdSetted){
this.isMessageIdSetted = true;
this.headers['MessageId'] = messageId++;
this.headers['MessageId'] = connection.messageId++;
}
// HEADERS
......@@ -243,7 +238,7 @@ function unTranslate(key, value){
* PRIVATE FUNCTIONS
*/
function readHeaders(message, buffer){
var header = message.isAsync ? headerASync : headerSync
var header = (message.isAsync ? headerASync : headerSync)(message.ProcessId, message.SessionId)
, offset = 0
;
for(var i in header){
......@@ -265,7 +260,7 @@ function readHeaders(message, buffer){
function writeHeaders(message, buffer){
var header = message.isAsync ? headerASync : headerSync
var header = (message.isAsync ? headerASync : headerSync)(message.ProcessId, message.SessionId)
, offset = 0
;
for(var i in header){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment