Exploit databaseDownload repository ZIP
GH-61d7050b85c9845102b2f5031635a736
natteesetobol/etern-blue-windows-7-checker
Sources for CVE-2017-0143
24 sourcesPreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source
Repository files16
natteesetobol/etern-blue-windows-7-checkerRevision db01d0049e00
- Linux/
- Windows/
- build/
- ErrorHandle.cpp
- ErrorHandle.hpp
- README.md
- build.bat
- build.sh
- linkedlist.cpp
- linkedlist.hpp
- main.cpp
- platform.hpp
- smb.cpp
- smb.hpp
- utils.cpp
- utils.hpp
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
smb.cpp
C++ · 28,384 bytes#include "smb.hpp"
SMB::SMB()
{
srand(time(NULL));
}
SMB::SMB(char *ip, int port)
{
srand(time(NULL));
printf("[!] Connecting to %s on port %i\n", ip, port);
winsock.Connect(ip, port);
printf("[+] Connected!\n");
}
SMB::~SMB()
{
if (winsock.hostname)
{
free(winsock.hostname);
winsock.hostname=NULL;
}
}
uint8_t* SMB::SMBNegotiateReq(Negotiate_Request *negotiateReq , int *packetSize)
{
uint8_t *result = NULL;
uint8_t *resultPtr = NULL;
if (negotiateReq->parameter == NULL)
{
negotiateReq->wordCount = 0;
}
if (negotiateReq->wordCount > 0 && negotiateReq->parameter != NULL)
{
negotiateReq->wordCount = strlen((char*)negotiateReq->parameter);
}
*packetSize += sizeof(negotiateReq->wordCount);
if (negotiateReq->wordCount != 0)
{
*packetSize += negotiateReq->wordCount;
}
*packetSize += sizeof(negotiateReq->byteCount);
*packetSize += sizeof(unsigned char);
if (negotiateReq->byteCount > 0)
{
*packetSize += negotiateReq->byteCount;
}
result = (uint8_t*) Alloc(*packetSize);
resultPtr = result;
memcpy(resultPtr,&negotiateReq->wordCount, sizeof(negotiateReq->wordCount));
resultPtr += sizeof(negotiateReq->wordCount);
if (negotiateReq->wordCount > 0)
{
memcpy(resultPtr,negotiateReq->parameter, negotiateReq->wordCount);
resultPtr += negotiateReq->wordCount;
}
memcpy(resultPtr,&negotiateReq->byteCount, sizeof(negotiateReq->byteCount));
resultPtr+=sizeof(negotiateReq->byteCount);
memcpy(resultPtr, &negotiateReq->bufferFormat,sizeof(negotiateReq->bufferFormat));
resultPtr+=sizeof(negotiateReq->bufferFormat);
if (negotiateReq->byteCount > 0)
{
memcpy(resultPtr,negotiateReq->name,negotiateReq->byteCount);
}
return result;
}
bool SMB::SMB_NegotiateRequest()
{
SMBHeader smbHeader;
Negotiate_Request negotiateRequest={};
uint32_t totalLength = 0;
net_bios netBios={};
LinkedList structures;
uint8_t *packet=NULL;
int totalSize=0;
bool succeeded=true;
Negotiate_Response *negotiateResponse=NULL;
uint8_t* negPacket = NULL;
int negPacketSize = 0;
char* smbHeaderBytes=NULL;
smb_header *recvSMBHeader = NULL;
uint32_t smb_length=0;
int wresults = 0;
int sent = 0;
unsigned netBiosBytes[sizeof(net_bios)];
short* data = NULL;
uint16_t bytesCount = 0;
randomPID = rand() % 6000 + 4000;
smbHeader.SetCommand(SMB_COMMAND_NEGOTIATE);
smbHeader.SetFlag(FLAGS1_PATHCASELESS);
smbHeader.SetFlag2(FLAGS2_UNICODE | FLAGS2_IS_LONG_NAME);
smbHeader.SetTID(0xFFFF);
smbHeader.SetPID(randomPID);
negotiateRequest.wordCount = 0;
negotiateRequest.parameter = NULL;
negotiateRequest.byteCount = 12;
negotiateRequest.bufferFormat = 0x02;
negotiateRequest.name = (unsigned char*) Alloc(11); //Plus \0
strcpy( (char*) negotiateRequest.name,"NT LM 0.12");
negPacket = SMBNegotiateReq(&negotiateRequest, &negPacketSize);
totalLength = sizeof(smbHeader) + sizeof(negotiateRequest) ;
netBios.length =(uint32_t) BigToLittleEndian((uint32_t)totalLength);
totalSize += sizeof(net_bios);
structures.Add(&netBios, sizeof(net_bios));
totalSize += sizeof(smb_header);
structures.Add(&smbHeader.smbHeader,sizeof(smb_header));
totalSize += sizeof(Negotiate_Request);
structures.Add(negPacket,negPacketSize);
packet = CreatePacket(&structures);
if (winsock.IsConnected())
{
sent = winsock.Send((char*)packet,totalSize);
memset(netBiosBytes,0,sizeof(net_bios));
net_bios *netBios = NULL;
smb_header *resultSMBHeader=NULL;
wresults = winsock.Recv( (char*)&netBiosBytes, sizeof(net_bios));
if (wresults < 1)
{
succeeded = false;
goto DONE;
}
netBios = (net_bios*) &netBiosBytes;
smb_length = BigToLittleEndian(netBios->length);
smbHeaderBytes = (char*) Alloc(smb_length);
wresults = winsock.Recv( (char*)smbHeaderBytes, smb_length);
if (wresults < 1)
{
succeeded = false;
goto DONE;
}
recvSMBHeader = (smb_header*) smbHeaderBytes;
negotiateResponse = (Negotiate_Response*) (sizeof(smb_header)+smbHeaderBytes);
if (recvSMBHeader->smbError != SMB_STATUS_OK)
{
succeeded = false;
goto DONE;
} else {
error = recvSMBHeader->smbError;
}
}
DONE:
structures.RemoveAll();
if (smbHeaderBytes)
{
free(smbHeaderBytes);
smbHeaderBytes = NULL;
}
if (negotiateRequest.name)
{
free(negotiateRequest.name);
negotiateRequest.name = NULL;
}
if (packet)
{
free(packet);
packet = NULL;
}
if ( negPacket)
{
free( negPacket);
negPacket = NULL;
}
if (succeeded == false)
{
ThrowError("Error: Target is likey patched");
}
return succeeded;
}
bool SMB::Login(char* username, char* password)
{
smb_header smbHeader;
struct Setup_AndX_Request setupAndXRequest = {};
struct GSS_API gssAPI ={};
net_bios netBios = {};
LinkedList structures;
char OS[5] = {};
char LANManager[6] = {};
uint8_t *packet=NULL;
uint32_t packetTotalSize = 0;
unsigned netBiosBytes[sizeof(net_bios)];
smb_header *resultSMBHeader=NULL;
unsigned char smbHeaderBytes[sizeof(smb_header)];
int respLen = 0;
uint16_t byte_count = 2;
struct SPN_negTokenTarg spnNegTokenTarg = {};
uint8_t *data = NULL;
int dataSize=0;
char *smbBytesRecv=NULL;
char *netBiosRecvBytes = NULL;
char *smbPacket = NULL;
net_bios *netBiosRecv=NULL;
smb_header *smbRespHeader=NULL;
int smbLength = 0;
bool result=true;
memset(netBiosBytes,0,sizeof(net_bios));
memset(smbHeaderBytes,0,sizeof(smb_header));
smbHeader.command = SMB_COMMAND_SESSION_SETUP_ANDX;
smbHeader.flag = FLAGS1_CANONICALIZED_PATHS | FLAGS1_PATHCASELESS;
smbHeader.flag2 = FLAGS2_NT_STATUS | FLAGS2_LONG_NAMES;//0x4001;
smbHeader.tid = 0xFFFF;
smbHeader.pid = randomPID;
smbHeader.mid = 1;
setupAndXRequest.wordCount = 0xD;
setupAndXRequest.andXCommand = 0xff;
setupAndXRequest.reserved = 0x00;
setupAndXRequest.andXOffset = 0x0000;
setupAndXRequest.maxBuffer = (unsigned short) 61440;
setupAndXRequest.maxMpxCount = (unsigned short) 2;
/*
#ifdef WINDOWS
setupAndXRequest.vcNumber = (unsigned short) GetCurrentProcessId() & 0xFFFF;
#else
setupAndXRequest.vcNumber = (unsigned short) getpid() & 0xFFFF;
#endif
*/
setupAndXRequest.vcNumber = (unsigned short) GetPID() & 0xFFFF;
setupAndXRequest.session_key = 0;
setupAndXRequest.ansi_pass_len= 0;
setupAndXRequest.unicode_pass_len = 0;
setupAndXRequest.reserved2=0;
setupAndXRequest.capabilities = 0x00000001 | 0x40 | 0x00008000;
// NOTES(): Hashes password, but no password for now
data = CreateAndXDataPacket(NULL, NULL, username, "WORKGROUP","Unix", "pysmb", &dataSize );
packetTotalSize = sizeof(smb_header) + sizeof(Setup_AndX_Request) + dataSize; // sizeof(GSS_API) + 5 + 6; //length of Unix and Samba + 1
netBios.length = BigToLittleEndian(packetTotalSize);
structures.Add(&netBios, sizeof(net_bios));
structures.Add(&smbHeader, sizeof(smb_header));
structures.Add(&setupAndXRequest, sizeof(Setup_AndX_Request));
structures.Add(data,dataSize);
packet = CreatePacket(&structures);
if (winsock.IsConnected())
{
int wresults = 0;
int sent = 0;
sent = winsock.Send((char*)packet,sizeof(net_bios)+packetTotalSize);
if (sent > 0)
{
netBiosRecvBytes = (char*)Alloc(sizeof(net_bios));
wresults = winsock.Recv( netBiosRecvBytes, sizeof(net_bios));
if (wresults < 1)
{
result = false;
goto DONE;
}
netBiosRecv = (net_bios*) netBiosRecvBytes;
smbLength = BigToLittleEndian(netBiosRecv->length);
smbBytesRecv = (char*) Alloc(smbLength);
if (smbBytesRecv == NULL)
{
result = false;
goto DONE;
}
wresults = winsock.Recv(smbBytesRecv, smbLength);
if (wresults < 1)
{
result = false;
goto DONE;
}
smbRespHeader = (smb_header*) (smbBytesRecv);
if (smbRespHeader->smbError == 0)
{
int8_t wordCount = 0;
AndX_Command *andXResp = NULL;
int8_t *data=NULL;
int8_t *dataPtrStart=NULL;
int8_t *dataPtrCurrent=NULL;
int len = 0;
int dataLen = 0;
uid = smbRespHeader->uid;
wordCount = *(uint8_t*) (smbBytesRecv + sizeof(smb_header));
andXResp = (AndX_Command*) (smbBytesRecv+1+sizeof(smb_header));
dataLen = *(uint8_t*)(smbBytesRecv+1+sizeof(smb_header)+sizeof(AndX_Command));
data = (int8_t*)(smbBytesRecv+3+sizeof(smb_header)+sizeof(AndX_Command));
dataPtrStart = data;
dataPtrCurrent = data;
while(*dataPtrCurrent)
{
dataPtrCurrent++;
}
len = dataPtrCurrent - dataPtrStart;
osName = (char*) Alloc(len+1)
;
memcpy(osName,dataPtrStart, len);
printf("[+] OS: %s\n", osName);
dataPtrCurrent++;
dataPtrStart = dataPtrCurrent;
while(*dataPtrCurrent)
{
dataPtrCurrent++;
}
len = dataPtrCurrent - dataPtrStart;
lanMan = (char*) Alloc(len+1);
memcpy(lanMan,dataPtrStart, len);
printf("[+] lanMan: %s\n", lanMan);
dataPtrCurrent++;
dataPtrStart = dataPtrCurrent;
while(*dataPtrCurrent)
{
dataPtrCurrent++;
}
len = dataPtrCurrent - dataPtrStart;
primaryDomain = (char*) Alloc(len+1);
memcpy(primaryDomain,dataPtrStart, len);
printf("[+] Primary Domain: %s\n", primaryDomain);
goto DONE;
} else {
error = smbRespHeader->smbError;
}
}
}
DONE:
if (netBiosRecvBytes)
{
free(netBiosRecvBytes);
netBiosRecvBytes = NULL;
}
if (primaryDomain)
{
free(primaryDomain);
primaryDomain = NULL;
}
if (lanMan)
{
free(lanMan);
lanMan = NULL;
}
if (osName)
{
free(osName);
osName = NULL;
}
if (smbBytesRecv)
{
free(smbBytesRecv);
smbBytesRecv = NULL;
}
if (packet)
{
free(packet);
packet = NULL;
}
if (data)
{
free(data);
data = NULL;
}
if (result == false)
{
ThrowError("[-] This is likely a patched version of windows.");
}
return result;
}
uint8_t* SMB::CreateAndXDataPacket(char* LMHash, char* NTHash, char* account, char* primaryDomain,char* nativeOS, char* nativeLanMan, int *size )
{
uint8_t *result=NULL;
uint8_t *resultPtr=NULL;
*size += 24; // LMHASH
*size += 24; // NTHASH
*size += strlen(account)+1;
*size += strlen(primaryDomain)+1;
*size += strlen(nativeOS)+1;
*size += strlen(nativeLanMan)+1;
result = (uint8_t*)Alloc(*size);
resultPtr = result;
if (LMHash)
{
memcpy(resultPtr, LMHash, 24);
}
resultPtr += 24;
if (NTHash)
{
memcpy(resultPtr, NTHash,24);
}
resultPtr += 24;
if (account)
{
memcpy(resultPtr, account, strlen(account)+1);
resultPtr += strlen(account)+1;
}
if (primaryDomain)
{
memcpy(resultPtr,primaryDomain, strlen(primaryDomain)+1);
resultPtr += strlen(primaryDomain)+1;
}
if (nativeOS)
{
memcpy(resultPtr, nativeOS, strlen(nativeOS)+1);
resultPtr += strlen(nativeOS)+1;
}
if (nativeLanMan)
{
memcpy(resultPtr,nativeLanMan, strlen(nativeLanMan)+1);
resultPtr += strlen(nativeLanMan)+1;
}
return result;
}
bool SMB::TreeConnectAndX(char* path, char* password, char service[6])
{
bool results = true;
smb_header smbHeader;
char* newPath = NULL;
Tree_AndX_Command treeAndXCommand = {};
int8_t wordCount = 0;
int16_t byteCount = 0;
net_bios netBios = {};
uint32_t packetTotalSize = 0;
LinkedList structures;
uint8_t *packet=NULL;
uint8_t* newPathUTF=NULL;
char *smbBytesRecv=NULL;
char *netBiosRecvBytes = NULL;
char *smbPacket = NULL;
net_bios *netBiosRecv=NULL;
smb_header *smbRespHeader=NULL;
int smbLength = 0;
int sent = 0;
int wresults = 0;
smbHeader.command = SMB_COMMAND_TREE_CONNECT_ANDX;
smbHeader.smbError = 0x00;
smbHeader.flag = FLAGS1_CANONICALIZED_PATHS | FLAGS1_PATHCASELESS;
smbHeader.flag2 = FLAGS2_UNICODE | FLAGS2_NT_STATUS | FLAGS2_EXTENDED_SECURITY | FLAGS2_SMB_SECURITY_SIGNATURE | FLAGS2_EAS | FLAGS2_LONG_NAMES; //0xC807;
smbHeader.tid = 0xFFFF;
smbHeader.pid = randomPID;
smbHeader.uid = uid;
smbHeader.mid = 0;
if (winsock.hostname == NULL)
{
results = false;
goto ENDTAG;
}
/*6 are for the slashes: \\\\ \\*/
newPath = (char*) Alloc(strlen(winsock.hostname)+6+strlen(path)+100);
sprintf(newPath, "\\\\%s\\%s", winsock.hostname, path);
newPathUTF = StringToWideString(newPath);
treeAndXCommand.AndXCommand = 0xFF;
treeAndXCommand.AndXReserve = 0x00;
treeAndXCommand.AndXOffset = 0xFFFF;
treeAndXCommand.flags = 0x00;
treeAndXCommand.passwordLengh = 0;
wordCount = 4;
byteCount = ((strlen(newPath)*2)+2) + strlen(service) + 1 + 1;
packetTotalSize = sizeof(smb_header) + sizeof(int8_t) + sizeof(Tree_AndX_Command) + sizeof(int16_t) + byteCount;
netBios.length = BigToLittleEndian(packetTotalSize);
structures.Add(&netBios, sizeof(net_bios));
structures.Add(&smbHeader, sizeof(smb_header));
structures.Add(&wordCount, sizeof(int8_t));
structures.Add(&treeAndXCommand, sizeof(Tree_AndX_Command));
structures.Add(&byteCount, sizeof(int16_t));
structures.Add((char*)"\x00", 1);
structures.Add(newPathUTF, (strlen(newPath)*2) + 2);
structures.Add(service, strlen(service)+1);
packet = CreatePacket(&structures);
if (packet == NULL)
{
results =false;
goto ENDTAG;
}
if (winsock.IsConnected())
{
sent = winsock.Send((char*)packet,sizeof(net_bios)+packetTotalSize);
if (sent < 1)
{
results=false;
goto ENDTAG;
}
netBiosRecvBytes = (char*)Alloc(sizeof(net_bios));
wresults = winsock.Recv( netBiosRecvBytes, sizeof(net_bios));
if (wresults < 1)
{
results=false;
goto ENDTAG;
}
netBiosRecv = (net_bios*) netBiosRecvBytes;
smbLength = BigToLittleEndian(netBiosRecv->length);
smbBytesRecv = (char*) Alloc(smbLength);
wresults = winsock.Recv(smbBytesRecv, smbLength);
if (wresults < 1)
{
results=false;
goto ENDTAG;
}
smbRespHeader = (smb_header*) (smbBytesRecv);
if (smbRespHeader->smbError == 0)
{
tid = smbRespHeader->tid;
} else {
error = smbRespHeader->smbError;
}
}
ENDTAG:
structures.RemoveAll();
if (newPathUTF)
{
free(newPathUTF);
newPathUTF = NULL;
}
if (netBiosRecvBytes)
{
free(netBiosRecvBytes);
netBiosRecvBytes = NULL;
}
if (smbBytesRecv)
{
free(smbBytesRecv);
smbBytesRecv = NULL;
}
if (newPath)
{
free(newPath);
newPath = NULL;
}
if (packet)
{
free(packet);
packet = NULL;
}
if (results == false) ThrowError("This version of Windows is likely patched.");
return results;
}
bool SMB::SendTransAction(SMB_TRANSACTION *transAction, char* data, bool padding)
{
bool results = true;
smb_header smbHeader;
net_bios netBios={};
int8_t wordCount = 0;
int16_t byteCount = 0;
uint32_t packetTotalSize = 0;
LinkedList structures;
int16_t FID = 0;
uint8_t *packet=NULL;
char* paddings = NULL;
int paddingCount = 0;
char *smbBytesRecv=NULL;
char *netBiosRecvBytes = NULL;
char *smbPacket = NULL;
net_bios *netBiosRecv=NULL;
smb_header *smbRespHeader=NULL;
int smbLength = 0;
int sent = 0;
int wresults = 0;
smbHeader.command = SMB_COMMAND_TRANSACTION;
smbHeader.smbError = 0x00;
smbHeader.flag = FLAGS1_CANONICALIZED_PATHS | FLAGS1_PATHCASELESS;
smbHeader.flag2 = FLAGS2_UNICODE | FLAGS2_NT_STATUS | FLAGS2_EXTENDED_SECURITY | FLAGS2_SMB_SECURITY_SIGNATURE | FLAGS2_EAS | FLAGS2_LONG_NAMES;
smbHeader.tid = tid;
smbHeader.pid = randomPID;
smbHeader.uid = uid;
smbHeader.mid = 0;
if (transAction->MaxSetupCount == 0)
{
transAction->MaxSetupCount = 1;
}
if (transAction->TotalParameterCount == 0)
{
transAction->TotalParameterCount = 2;
}
if (transAction->TotalDataCount == 0)
{
if (data)
{
transAction->TotalDataCount = strlen(data);
} else {
transAction->TotalDataCount = 0;
}
}
transAction->ParameterOffset = 0x60;
if (padding)
{
paddingCount = 32;
paddings = (char*) Alloc(paddingCount);
} else {
paddingCount = 0;
}
byteCount = paddingCount+1;
wordCount = 0x0E;
FID = 0x4000;
//size of smb_header + wordCount (1) + size of SMB_TRANSACTION + byteCount (2) + FID (2bytes)
packetTotalSize = sizeof(smb_header) + sizeof(uint8_t) + sizeof(SMB_TRANSACTION) + paddingCount + sizeof(uint16_t) + sizeof(uint16_t);
netBios.length = BigToLittleEndian(packetTotalSize);
structures.Add(&netBios, sizeof(net_bios));
structures.Add(&smbHeader, sizeof(smb_header));
structures.Add(&wordCount, sizeof(int8_t));
structures.Add(transAction, sizeof(SMB_TRANSACTION));
structures.Add(&byteCount, sizeof(int16_t));
if (padding)
{
structures.Add(paddings,paddingCount);
}
structures.Add(&FID, sizeof(int16_t));
packet = CreatePacket(&structures);
if (winsock.IsConnected())
{
sent = winsock.Send((char*)packet,sizeof(net_bios)+packetTotalSize);
netBiosRecvBytes = (char*)Alloc(sizeof(net_bios));
wresults = winsock.Recv( netBiosRecvBytes, sizeof(net_bios));
if (wresults < 1)
{
goto CLEANUP;
}
netBiosRecv = (net_bios*) netBiosRecvBytes;
smbLength = BigToLittleEndian(netBiosRecv->length);
smbBytesRecv = (char*) Alloc(smbLength);
wresults = winsock.Recv(smbBytesRecv, smbLength);
if (wresults < 1)
{
goto CLEANUP;
}
smbRespHeader = (smb_header*) (smbBytesRecv);
if (smbRespHeader->smbError == 0)
{
} else {
results = false;
error = smbRespHeader->smbError;
}
}
CLEANUP:
structures.RemoveAll();
if (paddings)
{
free(paddings);
paddings=NULL;
}
if (smbBytesRecv)
{
free(smbBytesRecv);
smbBytesRecv = NULL;
}
if (netBiosRecvBytes)
{
free(netBiosRecvBytes);
netBiosRecvBytes = NULL;
}
if (packet)
{
free(packet);
packet = NULL;
}
if (results == false)
{
}
return results;
}
bool SMB::NTCreateAndX(char* pipe)
{
const char service[] = "?????";
bool result = false;
net_bios netBios={};
smb_header smbHeader;
NT_CREATE_ANDX_PARAMETERS ntCreateAndXParams ;
uint32_t packetTotalSize = 0;
LinkedList structures;
int8_t wordCount = 0;
int16_t byteCount = 0;
uint8_t *packet=NULL;
int8_t wordCountTreeConnect = 0;
int16_t byteCountTreeConnect = 0;
uint8_t *pipeUTF16 = NULL;
char *smbBytesRecv=NULL;
char *netBiosRecvBytes = NULL;
char *smbPacket = NULL;
net_bios *netBiosRecv=NULL;
smb_header *smbRespHeader=NULL;
int smbLength = 0;
int sent = 0;
int wresults = 0;
smbHeader.command = SMB_COMMAND_NT_CREATE_ANDX;
smbHeader.smbError = 0x00;
smbHeader.flag = FLAGS1_CANONICALIZED_PATHS | FLAGS1_PATHCASELESS;
smbHeader.flag2 = FLAGS2_UNICODE | FLAGS2_NT_STATUS | FLAGS2_EXTENDED_SECURITY | FLAGS2_SMB_SECURITY_SIGNATURE | FLAGS2_EAS | FLAGS2_LONG_NAMES;
smbHeader.tid = tid;
smbHeader.pid = randomPID;
smbHeader.uid = uid;
smbHeader.mid = 0;
ntCreateAndXParams.FilenameLength = (strlen(pipe)+1)*2;
ntCreateAndXParams.CreateFlags = 0x16;
ntCreateAndXParams.AccessMask = 0x0012019F;
ntCreateAndXParams.SecurityFlags = 0;
ntCreateAndXParams.ShareAccess = 0x00000007; // FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE
ntCreateAndXParams.CreateOptions = 0x00000040; // FILE_NON_DIRECTORY_FILE
ntCreateAndXParams.AndXCommand = 0xFF; // No next command
ntCreateAndXParams.AndXOffset = 0x0000;
pipeUTF16 = StringToWideString(pipe);
wordCount = sizeof(NT_CREATE_ANDX_PARAMETERS) / 2;
byteCount = (strlen(pipe)+1)*2;
wordCountTreeConnect = 4;
packetTotalSize = sizeof(smb_header) + sizeof(int8_t) + sizeof(NT_CREATE_ANDX_PARAMETERS) + sizeof(int16_t)+ (((strlen(pipe)+1)*2));
netBios.length = BigToLittleEndian(packetTotalSize);
structures.Add(&netBios, sizeof(net_bios));
structures.Add(&smbHeader, sizeof(smb_header));
structures.Add(&wordCount, sizeof(int8_t));
structures.Add(&ntCreateAndXParams, sizeof(NT_CREATE_ANDX_PARAMETERS));
structures.Add(&byteCount, sizeof(int16_t));
structures.Add(pipeUTF16, ((strlen(pipe)+1)*2));
packet = CreatePacket(&structures);
if (winsock.IsConnected())
{
error = 0;
sent = winsock.Send((char*)packet,sizeof(net_bios)+packetTotalSize);
netBiosRecvBytes = (char*)Alloc(sizeof(net_bios));
wresults = winsock.Recv( netBiosRecvBytes, sizeof(net_bios));
if (wresults < 1) goto CLEANUP;
netBiosRecv = (net_bios*) netBiosRecvBytes;
smbLength = BigToLittleEndian(netBiosRecv->length);
smbBytesRecv = (char*) Alloc(smbLength);
wresults = winsock.Recv(smbBytesRecv, smbLength);
if (wresults < 1)
{
goto CLEANUP;
}
smbRespHeader = (smb_header*) (smbBytesRecv);
if (smbRespHeader->smbError == 0)
{
} else {
error = smbRespHeader->smbError;
}
}
CLEANUP:
structures.RemoveAll();
if (packet)
{
free(packet);
packet = NULL;
}
if (pipeUTF16)
{
free(pipeUTF16);
pipeUTF16=NULL;
}
if (netBiosRecvBytes)
{
free(netBiosRecvBytes);
netBiosRecvBytes = NULL;
}
if (smbBytesRecv)
{
free(smbBytesRecv);
smbBytesRecv = NULL;
}
return result;
}
uint32_t SMB::GetLastError()
{
return error;
}
uint8_t *SMB::CreatePacket(LinkedList *structureList)
{
uint8_t *result=NULL;
size_t size = 0;
for (int i=0;i< structureList->count;i++)
{
linknode *structs = structureList->GetList(i);
size += structs->size;
}
if (size < 2) ThrowError("Packet Size is too small");
result = (uint8_t*) Alloc(size);
uint8_t *resultPointer = NULL;
resultPointer = result;
for (int i=0;i< structureList->count;i++)
{
linknode *structs = structureList->GetList(i);
memcpy(resultPointer, structs->data, structs->size);
resultPointer += structs->size;
}
structureList->RemoveAll();
return result;
}
void SMB::Connect(char *ip, int port)
{
winsock.Connect(ip, port);
}
/* SMB HEADER */
SMBHeader::SMBHeader()
{
smbHeader.protocol[0] = 0xFF;
memcpy(&smbHeader.protocol[0]+1,"SMB", 3);
}
SMBHeader::~SMBHeader()
{
}
void SMBHeader::SetCommand(unsigned char command)
{
smbHeader.command = command;
return ;
}
unsigned char SMBHeader::GetCommand()
{
return smbHeader.command;
}
void SMBHeader::SetSMBError(unsigned char error)
{
smbHeader.smbError = error;
}
unsigned char SMBHeader::GetSMBError()
{
return smbHeader.smbError;
}
void SMBHeader::SetFlag(unsigned char flag)
{
smbHeader.flag = flag;
}
unsigned char SMBHeader::GetFlag()
{
return smbHeader.flag;
}
void SMBHeader::SetFlag2(unsigned short flag)
{
smbHeader.flag2 = flag;
}
unsigned short SMBHeader::GetFlag2()
{
return smbHeader.flag2;
}
void SMBHeader::SetPIDHigh(unsigned short PIDHigh)
{
smbHeader.PIDHigh = PIDHigh;
}
unsigned short SMBHeader::GetPIDHigh()
{
return smbHeader.PIDHigh;
}
void SMBHeader::SetSecurityFeature(unsigned char securityFeature[8])
{
memcpy(&smbHeader.securityFeature[0],securityFeature, 8);
}
unsigned char* SMBHeader::GetSecurityFeature()
{
return &smbHeader.securityFeature[0];
}
void SMBHeader::SetReserves(unsigned short reserves)
{
smbHeader.reserves = reserves;
}
unsigned short SMBHeader::GetReserves()
{
return smbHeader.reserves;
}
void SMBHeader::SetTID(unsigned short tid)
{
smbHeader.tid = tid;
}
unsigned short SMBHeader::GetTID()
{
return smbHeader.tid;
}
void SMBHeader::SetPID(unsigned short pid)
{
smbHeader.pid = pid;
}
unsigned short SMBHeader::GetPID()
{
return smbHeader.pid;
}
void SMBHeader::SetUID(unsigned short uid)
{
smbHeader.uid = uid;
}
unsigned short SMBHeader::GetUID()
{
return smbHeader.uid;
}
void SMBHeader::SetMID(unsigned short mid)
{
smbHeader.mid = mid;
}
unsigned short SMBHeader::GetMID()
{
return smbHeader.mid;
}
unsigned char* SMBHeader::String()
{
return (unsigned char*) &smbHeader;
}
uint16_t host_to_le16(uint16_t val) {
return ((val & 0xFF) << 8) | ((val >> 8) & 0xFF);
}
uint32_t host_to_le32(uint32_t val) {
return ((val & 0xFF) << 24) | ((val & 0xFF00) << 8) |
((val >> 8) & 0xFF00) | ((val >> 24) & 0xFF);
}
uint64_t host_to_le64(uint64_t val) {
return ((val & 0xFFULL) << 56) | ((val & 0xFF00ULL) << 40) |
((val & 0xFF0000ULL) << 24) | ((val & 0xFF000000ULL) << 8) |
((val >> 8) & 0xFF000000ULL) | ((val >> 24) & 0xFF0000ULL) |
((val >> 40) & 0xFF00ULL) | ((val >> 56) & 0xFFULL);
}