print-smb.c

来自「TCPDUMP的C语言源代码,是在数据链路层的应用」· C语言 代码 · 共 1,511 行 · 第 1/3 页

C
1,511
字号
    TCHECK2(*data, 2);    bcc = EXTRACT_LE_16BITS(data);    printf("smb_bcc=%u\n", bcc);    if (bcc > 0) {	if (f2)	    smb_fdata(data + 2, f2, SMBMIN(data + 2 + EXTRACT_LE_16BITS(data),		maxbuf), unicodestr);	else	    print_data(data + 2, SMBMIN(EXTRACT_LE_16BITS(data), PTR_DIFF(maxbuf, data + 2)));    }    return;trunc:    printf("[|SMB]");    return;}static struct smbfns smb_fns[] = {    { -1, "SMBunknown", 0, DEFDESCRIPT },    { SMBtcon, "SMBtcon", 0,	{ NULL, "Path=[Z]\nPassword=[Z]\nDevice=[Z]\n",	  "MaxXmit=[d]\nTreeId=[d]\n", NULL,	  NULL } },    { SMBtdis, "SMBtdis", 0, DEFDESCRIPT },    { SMBexit,  "SMBexit", 0, DEFDESCRIPT },    { SMBioctl, "SMBioctl", 0, DEFDESCRIPT },    { SMBecho, "SMBecho", 0,	{ "ReverbCount=[d]\n", NULL,	  "SequenceNum=[d]\n", NULL,	  NULL } },    { SMBulogoffX, "SMBulogoffX", FLG_CHAIN, DEFDESCRIPT },    { SMBgetatr, "SMBgetatr", 0,	{ NULL, "Path=[Z]\n",	  "Attribute=[A]\nTime=[T2]Size=[D]\nRes=([w,w,w,w,w])\n", NULL,	  NULL } },    { SMBsetatr, "SMBsetatr", 0,	{ "Attribute=[A]\nTime=[T2]Res=([w,w,w,w,w])\n", "Path=[Z]\n",	  NULL, NULL, NULL } },    { SMBchkpth, "SMBchkpth", 0,       { NULL, "Path=[Z]\n", NULL, NULL, NULL } },    { SMBsearch, "SMBsearch", 0,	{ "Count=[d]\nAttrib=[A]\n",	  "Path=[Z]\nBlkType=[B]\nBlkLen=[d]\n|Res1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\n",	  "Count=[d]\n",	  "BlkType=[B]\nBlkLen=[d]\n*\nRes1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\nAttrib=[a]\nTime=[T1]Size=[D]\nName=[s13]\n",	  NULL } },    { SMBopen, "SMBopen", 0,	{ "Mode=[w]\nAttribute=[A]\n", "Path=[Z]\n",	  "Handle=[d]\nOAttrib=[A]\nTime=[T2]Size=[D]\nAccess=[w]\n",	  NULL, NULL } },    { SMBcreate, "SMBcreate", 0,	{ "Attrib=[A]\nTime=[T2]", "Path=[Z]\n", "Handle=[d]\n", NULL, NULL } },    { SMBmknew, "SMBmknew", 0,	{ "Attrib=[A]\nTime=[T2]", "Path=[Z]\n", "Handle=[d]\n", NULL, NULL } },    { SMBunlink, "SMBunlink", 0,	{ "Attrib=[A]\n", "Path=[Z]\n", NULL, NULL, NULL } },    { SMBread, "SMBread", 0,	{ "Handle=[d]\nByteCount=[d]\nOffset=[D]\nCountLeft=[d]\n", NULL,	  "Count=[d]\nRes=([w,w,w,w])\n", NULL, NULL } },    { SMBwrite, "SMBwrite", 0,	{ "Handle=[d]\nByteCount=[d]\nOffset=[D]\nCountLeft=[d]\n", NULL,	  "Count=[d]\n", NULL, NULL } },    { SMBclose, "SMBclose", 0,	{ "Handle=[d]\nTime=[T2]", NULL, NULL, NULL, NULL } },    { SMBmkdir, "SMBmkdir", 0,	{ NULL, "Path=[Z]\n", NULL, NULL, NULL } },    { SMBrmdir, "SMBrmdir", 0,	{ NULL, "Path=[Z]\n", NULL, NULL, NULL } },    { SMBdskattr, "SMBdskattr", 0,	{ NULL, NULL,	  "TotalUnits=[d]\nBlocksPerUnit=[d]\nBlockSize=[d]\nFreeUnits=[d]\nMedia=[w]\n",	  NULL, NULL } },    { SMBmv, "SMBmv", 0,	{ "Attrib=[A]\n", "OldPath=[Z]\nNewPath=[Z]\n", NULL, NULL, NULL } },    /*     * this is a Pathworks specific call, allowing the     * changing of the root path     */    { pSETDIR, "SMBsetdir", 0, { NULL, "Path=[Z]\n", NULL, NULL, NULL } },    { SMBlseek, "SMBlseek", 0,	{ "Handle=[d]\nMode=[w]\nOffset=[D]\n", "Offset=[D]\n", NULL, NULL, NULL } },    { SMBflush, "SMBflush", 0, { "Handle=[d]\n", NULL, NULL, NULL, NULL } },    { SMBsplopen, "SMBsplopen", 0,	{ "SetupLen=[d]\nMode=[w]\n", "Ident=[Z]\n", "Handle=[d]\n",	  NULL, NULL } },    { SMBsplclose, "SMBsplclose", 0,	{ "Handle=[d]\n", NULL, NULL, NULL, NULL } },    { SMBsplretq, "SMBsplretq", 0,	{ "MaxCount=[d]\nStartIndex=[d]\n", NULL,	  "Count=[d]\nIndex=[d]\n",	  "*Time=[T2]Status=[B]\nJobID=[d]\nSize=[D]\nRes=[B]Name=[s16]\n",	  NULL } },    { SMBsplwr, "SMBsplwr", 0,	{ "Handle=[d]\n", NULL, NULL, NULL, NULL } },    { SMBlock, "SMBlock", 0,	{ "Handle=[d]\nCount=[D]\nOffset=[D]\n", NULL, NULL, NULL, NULL } },    { SMBunlock, "SMBunlock", 0,	{ "Handle=[d]\nCount=[D]\nOffset=[D]\n", NULL, NULL, NULL, NULL } },    /* CORE+ PROTOCOL FOLLOWS */    { SMBreadbraw, "SMBreadbraw", 0,	{ "Handle=[d]\nOffset=[D]\nMaxCount=[d]\nMinCount=[d]\nTimeOut=[D]\nRes=[d]\n",	  NULL, NULL, NULL, NULL } },    { SMBwritebraw, "SMBwritebraw", 0,	{ "Handle=[d]\nTotalCount=[d]\nRes=[w]\nOffset=[D]\nTimeOut=[D]\nWMode=[w]\nRes2=[W]\n|DataSize=[d]\nDataOff=[d]\n",	  NULL, "WriteRawAck", NULL, NULL } },    { SMBwritec, "SMBwritec", 0,	{ NULL, NULL, "Count=[d]\n", NULL, NULL } },    { SMBwriteclose, "SMBwriteclose", 0,	{ "Handle=[d]\nCount=[d]\nOffset=[D]\nTime=[T2]Res=([w,w,w,w,w,w])",	  NULL, "Count=[d]\n", NULL, NULL } },    { SMBlockread, "SMBlockread", 0,	{ "Handle=[d]\nByteCount=[d]\nOffset=[D]\nCountLeft=[d]\n", NULL,	  "Count=[d]\nRes=([w,w,w,w])\n", NULL, NULL } },    { SMBwriteunlock, "SMBwriteunlock", 0,	{ "Handle=[d]\nByteCount=[d]\nOffset=[D]\nCountLeft=[d]\n", NULL,	  "Count=[d]\n", NULL, NULL } },    { SMBreadBmpx, "SMBreadBmpx", 0,	{ "Handle=[d]\nOffset=[D]\nMaxCount=[d]\nMinCount=[d]\nTimeOut=[D]\nRes=[w]\n",	  NULL,	  "Offset=[D]\nTotCount=[d]\nRemaining=[d]\nRes=([w,w])\nDataSize=[d]\nDataOff=[d]\n",	  NULL, NULL } },    { SMBwriteBmpx, "SMBwriteBmpx", 0,	{ "Handle=[d]\nTotCount=[d]\nRes=[w]\nOffset=[D]\nTimeOut=[D]\nWMode=[w]\nRes2=[W]\nDataSize=[d]\nDataOff=[d]\n", NULL,	  "Remaining=[d]\n", NULL, NULL } },    { SMBwriteBs, "SMBwriteBs", 0,	{ "Handle=[d]\nTotCount=[d]\nOffset=[D]\nRes=[W]\nDataSize=[d]\nDataOff=[d]\n",	  NULL, "Count=[d]\n", NULL, NULL } },    { SMBsetattrE, "SMBsetattrE", 0,	{ "Handle=[d]\nCreationTime=[T2]AccessTime=[T2]ModifyTime=[T2]", NULL,	  NULL, NULL, NULL } },    { SMBgetattrE, "SMBgetattrE", 0,	{ "Handle=[d]\n", NULL,	  "CreationTime=[T2]AccessTime=[T2]ModifyTime=[T2]Size=[D]\nAllocSize=[D]\nAttribute=[A]\n",	  NULL, NULL } },    { SMBtranss, "SMBtranss", 0, DEFDESCRIPT },    { SMBioctls, "SMBioctls", 0, DEFDESCRIPT },    { SMBcopy, "SMBcopy", 0,	{ "TreeID2=[d]\nOFun=[w]\nFlags=[w]\n", "Path=[S]\nNewPath=[S]\n",	  "CopyCount=[d]\n",  "|ErrStr=[S]\n",  NULL } },    { SMBmove, "SMBmove", 0,	{ "TreeID2=[d]\nOFun=[w]\nFlags=[w]\n", "Path=[S]\nNewPath=[S]\n",	  "MoveCount=[d]\n",  "|ErrStr=[S]\n",  NULL } },    { SMBopenX, "SMBopenX", FLG_CHAIN,	{ "Com2=[w]\nOff2=[d]\nFlags=[w]\nMode=[w]\nSearchAttrib=[A]\nAttrib=[A]\nTime=[T2]OFun=[w]\nSize=[D]\nTimeOut=[D]\nRes=[W]\n",	  "Path=[S]\n",	  "Com2=[w]\nOff2=[d]\nHandle=[d]\nAttrib=[A]\nTime=[T2]Size=[D]\nAccess=[w]\nType=[w]\nState=[w]\nAction=[w]\nFileID=[W]\nRes=[w]\n",	  NULL, NULL } },    { SMBreadX, "SMBreadX", FLG_CHAIN,	{ "Com2=[w]\nOff2=[d]\nHandle=[d]\nOffset=[D]\nMaxCount=[d]\nMinCount=[d]\nTimeOut=[D]\nCountLeft=[d]\n",	  NULL,	  "Com2=[w]\nOff2=[d]\nRemaining=[d]\nRes=[W]\nDataSize=[d]\nDataOff=[d]\nRes=([w,w,w,w])\n",	  NULL, NULL } },    { SMBwriteX, "SMBwriteX", FLG_CHAIN,	{ "Com2=[w]\nOff2=[d]\nHandle=[d]\nOffset=[D]\nTimeOut=[D]\nWMode=[w]\nCountLeft=[d]\nRes=[w]\nDataSize=[d]\nDataOff=[d]\n",	  NULL,	  "Com2=[w]\nOff2=[d]\nCount=[d]\nRemaining=[d]\nRes=[W]\n",	  NULL, NULL } },    { SMBffirst, "SMBffirst", 0,	{ "Count=[d]\nAttrib=[A]\n",	  "Path=[Z]\nBlkType=[B]\nBlkLen=[d]\n|Res1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\n",	  "Count=[d]\n",	  "BlkType=[B]\nBlkLen=[d]\n*\nRes1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\nAttrib=[a]\nTime=[T1]Size=[D]\nName=[s13]\n",	  NULL } },    { SMBfunique, "SMBfunique", 0,	{ "Count=[d]\nAttrib=[A]\n",	  "Path=[Z]\nBlkType=[B]\nBlkLen=[d]\n|Res1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\n",	  "Count=[d]\n",	  "BlkType=[B]\nBlkLen=[d]\n*\nRes1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\nAttrib=[a]\nTime=[T1]Size=[D]\nName=[s13]\n",	  NULL } },    { SMBfclose, "SMBfclose", 0,	{ "Count=[d]\nAttrib=[A]\n",	  "Path=[Z]\nBlkType=[B]\nBlkLen=[d]\n|Res1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\n",	  "Count=[d]\n",	  "BlkType=[B]\nBlkLen=[d]\n*\nRes1=[B]\nMask=[s11]\nSrv1=[B]\nDirIndex=[d]\nSrv2=[w]\nRes2=[W]\nAttrib=[a]\nTime=[T1]Size=[D]\nName=[s13]\n",	  NULL } },    { SMBfindnclose, "SMBfindnclose", 0,	{ "Handle=[d]\n", NULL, NULL, NULL, NULL } },    { SMBfindclose, "SMBfindclose", 0,	{ "Handle=[d]\n", NULL, NULL, NULL, NULL } },    { SMBsends, "SMBsends", 0,	{ NULL, "Source=[Z]\nDest=[Z]\n", NULL, NULL, NULL } },    { SMBsendstrt, "SMBsendstrt", 0,	{ NULL, "Source=[Z]\nDest=[Z]\n", "GroupID=[d]\n", NULL, NULL } },    { SMBsendend, "SMBsendend", 0,	{ "GroupID=[d]\n", NULL, NULL, NULL, NULL } },    { SMBsendtxt, "SMBsendtxt", 0,	{ "GroupID=[d]\n", NULL, NULL, NULL, NULL } },    { SMBsendb, "SMBsendb", 0,	{ NULL, "Source=[Z]\nDest=[Z]\n", NULL, NULL, NULL } },    { SMBfwdname, "SMBfwdname", 0, DEFDESCRIPT },    { SMBcancelf, "SMBcancelf", 0, DEFDESCRIPT },    { SMBgetmac, "SMBgetmac", 0, DEFDESCRIPT },    { SMBnegprot, "SMBnegprot", 0,	{ NULL, NULL, NULL, NULL, print_negprot } },    { SMBsesssetupX, "SMBsesssetupX", FLG_CHAIN,	{ NULL, NULL, NULL, NULL, print_sesssetup } },    { SMBtconX, "SMBtconX", FLG_CHAIN,	{ "Com2=[w]\nOff2=[d]\nFlags=[w]\nPassLen=[d]\nPasswd&Path&Device=\n",	  NULL, "Com2=[w]\nOff2=[d]\n", "ServiceType=[R]\n", NULL } },    { SMBlockingX, "SMBlockingX", FLG_CHAIN,	{ NULL, NULL, NULL, NULL, print_lockingandx } },    { SMBtrans2, "SMBtrans2", 0, { NULL, NULL, NULL, NULL, print_trans2 } },    { SMBtranss2, "SMBtranss2", 0, DEFDESCRIPT },    { SMBctemp, "SMBctemp", 0, DEFDESCRIPT },    { SMBreadBs, "SMBreadBs", 0, DEFDESCRIPT },    { SMBtrans, "SMBtrans", 0, { NULL, NULL, NULL, NULL, print_trans } },    { SMBnttrans, "SMBnttrans", 0, DEFDESCRIPT },    { SMBnttranss, "SMBnttranss", 0, DEFDESCRIPT },    { SMBntcreateX, "SMBntcreateX", FLG_CHAIN,	{ "Com2=[w]\nOff2=[d]\nRes=[b]\nNameLen=[ld]\nFlags=[W]\nRootDirectoryFid=[D]\nAccessMask=[W]\nAllocationSize=[L]\nExtFileAttributes=[W]\nShareAccess=[W]\nCreateDisposition=[W]\nCreateOptions=[W]\nImpersonationLevel=[W]\nSecurityFlags=[b]\n",	  "Path=[C]\n",	  "Com2=[w]\nOff2=[d]\nOplockLevel=[b]\nFid=[d]\nCreateAction=[W]\nCreateTime=[T3]LastAccessTime=[T3]LastWriteTime=[T3]ChangeTime=[T3]ExtFileAttributes=[W]\nAllocationSize=[L]\nEndOfFile=[L]\nFileType=[w]\nDeviceState=[w]\nDirectory=[b]\n",	  NULL, NULL } },    { SMBntcancel, "SMBntcancel", 0, DEFDESCRIPT },    { -1, NULL, 0, DEFDESCRIPT }};/* * print a SMB message */static voidprint_smb(const u_char *buf, const u_char *maxbuf){    u_int16_t flags2;    int nterrcodes;    int command;    u_int32_t nterror;    const u_char *words, *maxwords, *data;    struct smbfns *fn;    const char *fmt_smbheader =        "[P4]SMB Command   =  [B]\nError class   =  [BP1]\nError code    =  [d]\nFlags1        =  [B]\nFlags2        =  [B][P13]\nTree ID       =  [d]\nProc ID       =  [d]\nUID           =  [d]\nMID           =  [d]\nWord Count    =  [b]\n";    int smboffset;    TCHECK(buf[9]);    request = (buf[9] & 0x80) ? 0 : 1;    flags2 = EXTRACT_LE_16BITS(&buf[10]);    unicodestr = flags2 & 0x8000;    nterrcodes = flags2 & 0x4000;    startbuf = buf;    command = buf[4];    fn = smbfind(command, smb_fns);    if (vflag > 1)	printf("\n");    printf("SMB PACKET: %s (%s)\n", fn->name, request ? "REQUEST" : "REPLY");    if (vflag < 2)	return;    /* print out the header */    smb_fdata(buf, fmt_smbheader, buf + 33, unicodestr);    if (nterrcodes) {    	nterror = EXTRACT_LE_32BITS(&buf[5]);	if (nterror)	    printf("NTError = %s\n", nt_errstr(nterror));    } else {	if (buf[5])	    printf("SMBError = %s\n", smb_errstr(buf[5], EXTRACT_LE_16BITS(&buf[7])));    }    smboffset = 32;    for (;;) {	const char *f1, *f2;	int wct;	u_int bcc;	int newsmboffset;	words = buf + smboffset;	TCHECK(words[0]);	wct = words[0];	data = words + 1 + wct * 2;	maxwords = SMBMIN(data, maxbuf);	if (request) {	    f1 = fn->descript.req_f1;	    f2 = fn->descript.req_f2;	} else {	    f1 = fn->descript.rep_f1;	    f2 = fn->descript.rep_f2;	}	if (fn->descript.fn)	    (*fn->descript.fn)(words, data, buf, maxbuf);	else {	    if (wct) {		if (f1)		    smb_fdata(words + 1, f1, words + 1 + wct * 2, unicodestr);		else {		    int i;		    int v;		    for (i = 0; &words[1 + 2 * i] < maxwords; i++) {			TCHECK2(words[1 + 2 * i], 2);			v = EXTRACT_LE_16BITS(words + 1 + 2 * i);			printf("smb_vwv[%d]=%d (0x%X)\n", i, v, v);		    }		}	    }	    TCHECK2(*data, 2);	    bcc = EXTRACT_LE_16BITS(data);	    printf("smb_bcc=%u\n", bcc);	    if (f2) {		if (bcc > 0)		    smb_fdata(data + 2, f2, data + 2 + bcc, unicodestr);	    } else {		if (bcc > 0) {		    printf("smb_buf[]=\n");		    print_data(data + 2, SMBMIN(bcc, PTR_DIFF(maxbuf, data + 2)));		}	    }	}	if ((fn->flags & FLG_CHAIN) == 0)	    break;	if (wct == 0)	    break;	TCHECK(words[1]);	command = words[1];	if (command == 0xFF)	    break;	TCHECK2(words[3], 2);	newsmboffset = EXTRACT_LE_16BITS(words + 3); 	fn = smbfind(command, smb_fns);	printf("\nSMB PACKET: %s (%s) (CHAINED)\n",	    fn->name, request ? "REQUEST" : "REPLY");	if (newsmboffset <= smboffset) {	    printf("Bad andX offset: %u <= %u\n", newsmboffset, smboffset);	    break;	}	smboffset = newsmboffset;    }    printf("\n");    return;trunc:    printf("[|SMB]");    return;}/* * print a NBT packet received across tcp on port 139 */voidnbt_tcp_print(const u_char *data, int length){    int caplen;    int type;    u_int nbt_len;    const u_char *maxbuf;    if (length < 4)	goto trunc;    if (snapend < data)	goto trunc;    caplen = snapend - data;    if (caplen < 4)	goto trunc;    maxbuf = data + caplen;    type = data[0];    nbt_len = EXTRACT_16BITS(data + 2);    length -= 4;    caplen -= 4;    startbuf = data;    if (vflag < 2) {	printf(" NBT Session Packet: ");	switch (type) {	case 0x00:	    printf("Session Message");	    break;	case 0x81:	    printf("Session Request");	    break;	case 0x82:	    printf("Session Granted");	    break;	case 0x83:	  {	    int ecode;	    if (nbt_len < 4)		goto trunc;	    if (length < 4)		goto trunc;	    if (caplen < 4)		goto trunc;	    ecode = data[4];	    printf("Session Reject, ");	    switch (ecode) {	    case 0x80:		printf("Not listening on called name");		break;	    case 0x81:		printf("Not listening for calling name");		break;	    case 0x82:		printf("Called name not present");		break;	    case 0x83:		printf("Called name present, but insufficient resources");		break;	    default:		printf("Unspecified error 0x%X", ecode);		break;	    }	  }	    break;	case 0x85:	    printf("Session Keepalive");	    break;	default:	    data = smb_fdata(data, "Unknown packet type [rB]", maxbuf, 0);	    break;	}    } else {	printf ("\n>>> NBT Session Packet\n");	switch (type) {	case 0x00:	    data = smb_fdata(data, "[P1]NBT Session Message\nFlags=[B]\nLength=[rd]\n",

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?