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({ ...@@ -14,6 +14,7 @@ module.exports = message({
'Command':'CLOSE' 'Command':'CLOSE'
, 'SessionId':connection.SessionId , 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId , 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
} }
, request:{ , request:{
'FileId':params.FileId 'FileId':params.FileId
......
...@@ -16,6 +16,7 @@ module.exports = message({ ...@@ -16,6 +16,7 @@ module.exports = message({
'Command':'CREATE' 'Command':'CREATE'
, 'SessionId':connection.SessionId , 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId , 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
} }
, request:{ , request:{
'Buffer':buffer 'Buffer':buffer
......
...@@ -16,6 +16,7 @@ module.exports = message({ ...@@ -16,6 +16,7 @@ module.exports = message({
'Command':'CREATE' 'Command':'CREATE'
, 'SessionId':connection.SessionId , 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId , 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
} }
, request:{ , request:{
'Buffer':buffer 'Buffer':buffer
......
...@@ -12,6 +12,7 @@ module.exports = message({ ...@@ -12,6 +12,7 @@ module.exports = message({
return new SMB2Message({ return new SMB2Message({
headers:{ headers:{
'Command':'NEGOTIATE' 'Command':'NEGOTIATE'
, 'ProcessId':connection.ProcessId
} }
}); });
......
...@@ -16,6 +16,7 @@ module.exports = message({ ...@@ -16,6 +16,7 @@ module.exports = message({
'Command':'CREATE' 'Command':'CREATE'
, 'SessionId':connection.SessionId , 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId , 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
} }
, request:{ , request:{
'Buffer':buffer 'Buffer':buffer
......
...@@ -16,6 +16,7 @@ module.exports = message({ ...@@ -16,6 +16,7 @@ module.exports = message({
'Command':'CREATE' 'Command':'CREATE'
, 'SessionId':connection.SessionId , 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId , 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
} }
, request:{ , request:{
'Buffer':buffer 'Buffer':buffer
......
...@@ -14,6 +14,7 @@ module.exports = message({ ...@@ -14,6 +14,7 @@ module.exports = message({
'Command':'QUERY_DIRECTORY' 'Command':'QUERY_DIRECTORY'
, 'SessionId':connection.SessionId , 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId , 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
} }
, request:{ , request:{
'FileId':params.FileId 'FileId':params.FileId
......
...@@ -14,6 +14,7 @@ module.exports = message({ ...@@ -14,6 +14,7 @@ module.exports = message({
'Command':'READ' 'Command':'READ'
, 'SessionId':connection.SessionId , 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId , 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
} }
, request:{ , request:{
'FileId':file.FileId 'FileId':file.FileId
......
...@@ -13,6 +13,7 @@ module.exports = message({ ...@@ -13,6 +13,7 @@ module.exports = message({
return new SMB2Message({ return new SMB2Message({
headers:{ headers:{
'Command':'SESSION_SETUP' 'Command':'SESSION_SETUP'
, 'ProcessId':connection.ProcessId
} }
, request:{ , request:{
'Buffer':ntlm.encodeType1( 'Buffer':ntlm.encodeType1(
......
...@@ -14,6 +14,7 @@ module.exports = message({ ...@@ -14,6 +14,7 @@ module.exports = message({
headers:{ headers:{
'Command':'SESSION_SETUP' 'Command':'SESSION_SETUP'
, 'SessionId':connection.SessionId , 'SessionId':connection.SessionId
, 'ProcessId':connection.ProcessId
} }
, request:{ , request:{
'Buffer':ntlm.encodeType3( 'Buffer':ntlm.encodeType3(
......
...@@ -29,6 +29,7 @@ module.exports = message({ ...@@ -29,6 +29,7 @@ module.exports = message({
'Command':'SET_INFO' 'Command':'SET_INFO'
, 'SessionId':connection.SessionId , 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId , 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
} }
, request:{ , request:{
'FileInfoClass':fileInfoClasses[params.FileInfoClass] 'FileInfoClass':fileInfoClasses[params.FileInfoClass]
......
...@@ -13,6 +13,7 @@ module.exports = message({ ...@@ -13,6 +13,7 @@ module.exports = message({
headers:{ headers:{
'Command':'TREE_CONNECT' 'Command':'TREE_CONNECT'
, 'SessionId':connection.SessionId , 'SessionId':connection.SessionId
, 'ProcessId':connection.ProcessId
} }
, request:{ , request:{
'Buffer':new Buffer(connection.fullPath, 'ucs2') 'Buffer':new Buffer(connection.fullPath, 'ucs2')
......
...@@ -14,6 +14,7 @@ module.exports = message({ ...@@ -14,6 +14,7 @@ module.exports = message({
'Command':'WRITE' 'Command':'WRITE'
, 'SessionId':connection.SessionId , 'SessionId':connection.SessionId
, 'TreeId':connection.TreeId , 'TreeId':connection.TreeId
, 'ProcessId':connection.ProcessId
} }
, request:{ , request:{
'FileId':params.FileId 'FileId':params.FileId
......
...@@ -36,6 +36,9 @@ var SMB = module.exports = function(opt){ ...@@ -36,6 +36,9 @@ var SMB = module.exports = function(opt){
// set default port // set default port
this.port = opt.port || port; this.port = opt.port || port;
// set message id
this.messageId = 0;
// extract share // extract share
this.share = matches[2]; this.share = matches[2];
...@@ -54,7 +57,19 @@ var SMB = module.exports = function(opt){ ...@@ -54,7 +57,19 @@ var SMB = module.exports = function(opt){
this.password = opt.password; this.password = opt.password;
// set session id // 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) // init connection (socket)
SMB2Connection.init(this); SMB2Connection.init(this);
...@@ -68,15 +83,15 @@ var proto = SMB.prototype = {}; ...@@ -68,15 +83,15 @@ var proto = SMB.prototype = {};
proto.close = require('./api/close'); 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.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.readdir = SMB2Connection.requireConnect(require('./api/readdir'));
proto.rmdir = SMB2Connection.requireConnect(require('./api/rmdir')); proto.rmdir = SMB2Connection.requireConnect(require('./api/rmdir'));
proto.mkdir = SMB2Connection.requireConnect(require('./api/mkdir')); proto.mkdir = SMB2Connection.requireConnect(require('./api/mkdir'));
......
...@@ -89,7 +89,7 @@ SMB2Forge.response = function(c){ ...@@ -89,7 +89,7 @@ SMB2Forge.response = function(c){
* HELPERS * HELPERS
*/ */
function sendNetBiosMessage(connection, message) { function sendNetBiosMessage(connection, message) {
var smbRequest = message.getBuffer(); var smbRequest = message.getBuffer(connection);
if(connection.debug){ if(connection.debug){
console.log('--request'); console.log('--request');
......
/*
* STATICS
*/
var messageId = 0;
/* /*
* CONSTANTS * CONSTANTS
*/ */
const protocolId = new Buffer([0xFE, 'S'.charCodeAt(0), 'M'.charCodeAt(0), 'B'.charCodeAt(0)]) const protocolId = new Buffer([0xFE, 'S'.charCodeAt(0), 'M'.charCodeAt(0), 'B'.charCodeAt(0)])
, processId = new Buffer([0xFF, 0xFE, 0x00, 0x00])
, headerTranslates = { , headerTranslates = {
'Command': { 'Command': {
'NEGOTIATE': 0x0000 'NEGOTIATE': 0x0000
...@@ -50,38 +41,42 @@ const protocolId = new Buffer([0xFE, 'S'.charCodeAt(0), 'M'.charCodeAt(0), 'B'.c ...@@ -50,38 +41,42 @@ const protocolId = new Buffer([0xFE, 'S'.charCodeAt(0), 'M'.charCodeAt(0), 'B'.c
, headerLength = 64 , headerLength = 64
, headerSync = [ , headerSync = function(processId, sessionId) {
['ProtocolId',4,protocolId] return [
, ['StructureSize',2,headerLength] ['ProtocolId',4,protocolId]
, ['CreditCharge',2,0] , ['StructureSize',2,headerLength]
, ['Status',4,0] , ['CreditCharge',2,0]
, ['Command',2] , ['Status',4,0]
, ['Credit',2,126] , ['Command',2]
, ['Flags',4,0] , ['Credit',2,126]
, ['NextCommand',4,0] , ['Flags',4,0]
, ['MessageId',4] , ['NextCommand',4,0]
, ['MessageIdHigh',4,0] , ['MessageId',4]
, ['ProcessId',4,processId] , ['MessageIdHigh',4,0]
, ['TreeId',4,0] , ['ProcessId',4,processId]
, ['SessionId',8,0] , ['TreeId',4,0]
, ['Signature',16,0] , ['SessionId',8,sessionId]
] , ['Signature',16,0]
];
, headerASync = [ }
['ProtocolId',4,protocolId]
, ['StructureSize',2,headerLength] , headerASync = function(processId, sessionId) {
, ['CreditCharge',2,0] return [
, ['Status',4,0] ['ProtocolId',4,protocolId]
, ['Command',2] , ['StructureSize',2,headerLength]
, ['Credit',2,126] , ['CreditCharge',2,0]
, ['Flags',4,0] , ['Status',4,0]
, ['NextCommand',4,0] , ['Command',2]
, ['MessageId',4] , ['Credit',2,126]
, ['MessageIdHigh',4,0] , ['Flags',4,0]
, ['AsyncId',8] , ['NextCommand',4,0]
, ['SessionId',8,0] , ['MessageId',4]
, ['Signature',16,0] , ['MessageIdHigh',4,0]
] , ['AsyncId',8]
, ['SessionId',8,sessionId]
, ['Signature',16,0]
];
}
; ;
...@@ -134,7 +129,7 @@ proto.getResponse = function(){ ...@@ -134,7 +129,7 @@ proto.getResponse = function(){
proto.getBuffer = function(){ proto.getBuffer = function(connection){
var buffer = new Buffer(0xFFFF) var buffer = new Buffer(0xFFFF)
, length = 0 , length = 0
; ;
...@@ -142,7 +137,7 @@ proto.getBuffer = function(){ ...@@ -142,7 +137,7 @@ proto.getBuffer = function(){
// SET MESSAGE ID // SET MESSAGE ID
if(!this.isMessageIdSetted){ if(!this.isMessageIdSetted){
this.isMessageIdSetted = true; this.isMessageIdSetted = true;
this.headers['MessageId'] = messageId++; this.headers['MessageId'] = connection.messageId++;
} }
// HEADERS // HEADERS
...@@ -243,7 +238,7 @@ function unTranslate(key, value){ ...@@ -243,7 +238,7 @@ function unTranslate(key, value){
* PRIVATE FUNCTIONS * PRIVATE FUNCTIONS
*/ */
function readHeaders(message, buffer){ function readHeaders(message, buffer){
var header = message.isAsync ? headerASync : headerSync var header = (message.isAsync ? headerASync : headerSync)(message.ProcessId, message.SessionId)
, offset = 0 , offset = 0
; ;
for(var i in header){ for(var i in header){
...@@ -265,7 +260,7 @@ function readHeaders(message, buffer){ ...@@ -265,7 +260,7 @@ function readHeaders(message, buffer){
function writeHeaders(message, buffer){ function writeHeaders(message, buffer){
var header = message.isAsync ? headerASync : headerSync var header = (message.isAsync ? headerASync : headerSync)(message.ProcessId, message.SessionId)
, offset = 0 , offset = 0
; ;
for(var i in header){ 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