예제 #1
0
        public virtual void Enc_ndr_string(string s)
        {
            Align(4);
            int i   = Index;
            int len = s.Length;

            Encdec.Enc_uint32le(len + 1, Buf, i);
            i += 4;
            Encdec.Enc_uint32le(0, Buf, i);
            i += 4;
            Encdec.Enc_uint32le(len + 1, Buf, i);
            i += 4;
            try
            {
                Array.Copy(Runtime.GetBytesForString(s, "UTF-16LE"), 0, Buf, i, len
                           * 2);
            }
            catch (UnsupportedEncodingException)
            {
            }
            i       += len * 2;
            Buf[i++] = unchecked ((byte)('\0'));
            Buf[i++] = unchecked ((byte)('\0'));
            Advance(i - Index);
        }
예제 #2
0
        /// <exception cref="WinrtCifs.Dcerpc.Ndr.NdrException"></exception>
        public virtual string Dec_ndr_string()
        {
            Align(4);
            int    i   = Index;
            string val = null;
            int    len = Encdec.Dec_uint32le(Buf, i);

            i += 12;
            if (len != 0)
            {
                len--;
                int size = len * 2;
                try
                {
                    if (size < 0 || size > unchecked (0xFFFF))
                    {
                        throw new NdrException(NdrException.InvalidConformance);
                    }
                    val = Runtime.GetStringForBytes(Buf, i, size, "UTF-16LE");
                    i  += size + 2;
                }
                catch (UnsupportedEncodingException)
                {
                }
            }
            Advance(i - Index);
            return(val);
        }
예제 #3
0
 private string GetNtlmsspListItem(byte[] type2Token, int id0)
 {
     int ri = 58;
     for (;;)
     {
         int id = Encdec.Dec_uint16le(type2Token, ri);
         int len = Encdec.Dec_uint16le(type2Token, ri + 2);
         ri += 4;
         if (id == 0 || (ri + len) > type2Token.Length)
         {
             break;
         }
         if (id == id0)
         {
             try
             {
                 return Runtime.GetStringForBytes(type2Token,
                                                  ri,
                                                  len,
                                                  SmbConstants.UniEncoding);
             }
             catch (UnsupportedEncodingException)
             {
                 break;
             }
         }
         ri += len;
     }
     return null;
 }
예제 #4
0
        public static byte[] GetNtlMv2Response(byte[] responseKeyNt,
                                               byte[] serverChallenge,
                                               byte[] clientChallenge,
                                               long nanos1601,
                                               byte[] targetInfo)
        {
            int targetInfoLength = targetInfo != null
                                        ? targetInfo.Length
                                        : 0;

            byte[] temp = new byte[28 + targetInfoLength + 4];
            Encdec.Enc_uint32le(unchecked (0x00000101), temp, 0);
            // Header
            Encdec.Enc_uint32le(unchecked (0x00000000), temp, 4);
            // Reserved
            Encdec.Enc_uint64le(nanos1601, temp, 8);
            Array.Copy(clientChallenge, 0, temp, 16, 8);
            Encdec.Enc_uint32le(unchecked (0x00000000), temp, 24);
            // Unknown
            if (targetInfo != null)
            {
                Array.Copy(targetInfo, 0, temp, 28, targetInfoLength);
            }
            Encdec.Enc_uint32le(unchecked (0x00000000), temp, 28 + targetInfoLength);
            // mystery bytes!
            return(ComputeResponse(responseKeyNt,
                                   serverChallenge,
                                   temp,
                                   0,
                                   temp.Length));
        }
예제 #5
0
        /*
         * /// <exception cref="SharpCifs.Smb.SmbException"></exception>
         * public void WriteChars(string s)
         * {
         *  int clen = s.Length;
         *  int blen = 2 * clen;
         *  byte[] b = new byte[blen];
         *  char[] c = new char[clen];
         *  Sharpen.Runtime.GetCharsForString(s, 0, clen, c, 0);
         *  for (int i = 0, j = 0; i < clen; i++)
         *  {
         *      b[j++] = unchecked((byte)((char)(((uchar)c[i]) >> 8)));
         *      b[j++] = unchecked((byte)((char)(((uchar)c[i]) >> 0)));
         *  }
         *  Write(b, 0, blen);
         * }
         */

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public void WriteUtf(string str)
        {
            int len = str.Length;
            int ch;
            int size = 0;

            byte[] dst;
            for (int i = 0; i < len; i++)
            {
                ch    = str[i];
                size += ch > unchecked (0x07F)
                            ? (ch > unchecked (0x7FF)
                                    ? 3
                                    : 2)
                            : 1;
            }
            dst = new byte[size];
            WriteShort(size);
            try
            {
                Encdec.Enc_utf8(str, dst, 0, size);
            }
            catch (IOException ioe)
            {
                throw new SmbException(string.Empty, ioe);
            }
            Write(dst, 0, size);
        }
예제 #6
0
        public virtual int Dec_ndr_short()
        {
            Align(2);
            int val = Encdec.Dec_uint16le(Buf, Index);

            Advance(2);
            return(val);
        }
예제 #7
0
 /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
 public double ReadDouble()
 {
     if ((Read(_tmp, 0, 8)) < 0)
     {
         throw new SmbException("EOF");
     }
     return(Encdec.Dec_doublebe(_tmp, 0));
 }
예제 #8
0
 /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
 public float ReadFloat()
 {
     if ((Read(_tmp, 0, 4)) < 0)
     {
         throw new SmbException("EOF");
     }
     return(Encdec.Dec_floatbe(_tmp, 0));
 }
예제 #9
0
 /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
 public long ReadLong()
 {
     if ((Read(_tmp, 0, 8)) < 0)
     {
         throw new SmbException("EOF");
     }
     return(Encdec.Dec_uint64be(_tmp, 0));
 }
예제 #10
0
 /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
 public int ReadInt()
 {
     if ((Read(_tmp, 0, 4)) < 0)
     {
         throw new SmbException("EOF");
     }
     return(Encdec.Dec_uint32be(_tmp, 0));
 }
예제 #11
0
 /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
 public char ReadChar()
 {
     if ((Read(_tmp, 0, 2)) < 0)
     {
         throw new SmbException("EOF");
     }
     return((char)Encdec.Dec_uint16be(_tmp, 0));
 }
예제 #12
0
 /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
 public int ReadUnsignedShort()
 {
     if ((Read(_tmp, 0, 2)) < 0)
     {
         throw new SmbException("EOF");
     }
     return(Encdec.Dec_uint16be(_tmp, 0) & unchecked (0xFFFF));
 }
예제 #13
0
 /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
 public short ReadShort()
 {
     if ((Read(_tmp, 0, 2)) < 0)
     {
         throw new SmbException("EOF");
     }
     return(Encdec.Dec_uint16be(_tmp, 0));
 }
예제 #14
0
        public virtual long Dec_ndr_hyper()
        {
            Align(8);
            long val = Encdec.Dec_uint64le(Buf, Index);

            Advance(8);
            return(val);
        }
예제 #15
0
        public virtual int Dec_ndr_long()
        {
            Align(4);
            int val = Encdec.Dec_uint32le(Buf, Index);

            Advance(4);
            return(val);
        }
예제 #16
0
        /// <exception cref="System.IO.IOException"></exception>
        protected internal override void DoRecv(Response response)
        {
            ServerMessageBlock resp = (ServerMessageBlock)response;

            resp.UseUnicode       = UseUnicode;
            resp.ExtendedSecurity = (Capabilities & SmbConstants.CapExtendedSecurity) == SmbConstants.CapExtendedSecurity;
            lock (Buf)
            {
                Array.Copy(Sbuf, 0, Buf, 0, 4 + SmbConstants.HeaderLength);
                int size = Encdec.Dec_uint16be(Buf, 2) & 0xFFFF;
                if (size < (SmbConstants.HeaderLength + 1) || (4 + size) > RcvBufSize)
                {
                    throw new IOException("Invalid payload size: " + size);
                }
                int errorCode = Encdec.Dec_uint32le(Buf, 9) & unchecked ((int)(0xFFFFFFFF));
                if (resp.Command == ServerMessageBlock.SmbComReadAndx && (errorCode == 0 || errorCode
                                                                          == unchecked ((int)(0x80000005))))
                {
                    // overflow indicator normal for pipe
                    SmbComReadAndXResponse r = (SmbComReadAndXResponse)resp;
                    int off = SmbConstants.HeaderLength;
                    Readn(In, Buf, 4 + off, 27);
                    off += 27;
                    resp.Decode(Buf, 4);
                    int pad = r.DataOffset - off;
                    if (r.ByteCount > 0 && pad > 0 && pad < 4)
                    {
                        Readn(In, Buf, 4 + off, pad);
                    }
                    if (r.DataLength > 0)
                    {
                        Readn(In, r.B, r.Off, r.DataLength);
                    }
                }
                else
                {
                    Readn(In, Buf, 4 + 32, size - 32);
                    resp.Decode(Buf, 4);
                    if (resp is SmbComTransactionResponse)
                    {
                        ((SmbComTransactionResponse)resp).Current();
                    }
                }
                if (Digest != null && resp.ErrorCode == 0)
                {
                    Digest.Verify(Buf, 4, resp);
                }
                if (Log.Level >= 4)
                {
                    Log.WriteLine(response);
                    if (Log.Level >= 6)
                    {
                        Hexdump.ToHexdump(Log, Buf, 4, size);
                    }
                }
            }
        }
예제 #17
0
        /// <exception cref="System.IO.IOException"></exception>
        protected internal override void DoSkip()
        {
            int size = Encdec.Dec_uint16be(Sbuf, 2) & 0xFFFF;

            if (size < 33 || (4 + size) > RcvBufSize)
            {
                In.Skip(In.Available());
            }
            else
            {
                In.Skip(size - 32);
            }
        }
예제 #18
0
        /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
        public string ReadUtf()
        {
            int size = ReadUnsignedShort();

            byte[] b = new byte[size];
            Read(b, 0, size);
            try
            {
                return(Encdec.Dec_utf8(b, 0, size));
            }
            catch (IOException ioe)
            {
                throw new SmbException(string.Empty, ioe);
            }
        }
예제 #19
0
        public static byte[] ToByteArray(Rpc.SidT sid)
        {
            byte[] dst = new byte[1 + 1 + 6 + sid.SubAuthorityCount * 4];
            int    di  = 0;

            dst[di++] = sid.Revision;
            dst[di++] = sid.SubAuthorityCount;
            Array.Copy(sid.IdentifierAuthority, 0, dst, di, 6);
            di += 6;
            for (int ii = 0; ii < sid.SubAuthorityCount; ii++)
            {
                Encdec.Enc_uint32le(sid.SubAuthority[ii], dst, di);
                di += 4;
            }
            return(dst);
        }
예제 #20
0
        /// <exception cref="System.IO.IOException"></exception>
        protected internal override ServerMessageBlock PeekKey()
        {
            int n;

            do
            {
                if ((n = Readn(In, Sbuf, 0, 4)) < 4)
                {
                    return(null);
                }
            }while (Sbuf[0] == 0x85);
            if ((n = Readn(In, Sbuf, 4, 32)) < 32)
            {
                return(null);
            }
            if (Log.Level >= 4)
            {
                Log.WriteLine("New data read: " + this);
                Hexdump.ToHexdump(Log, Sbuf, 4, 32);
            }
            for (;;)
            {
                if (Sbuf[0] == 0x00 && Sbuf[1] == 0x00 &&
                    Sbuf[4] == 0xFF &&
                    Sbuf[5] == 'S' &&
                    Sbuf[6] == 'M' &&
                    Sbuf[7] == 'B')
                {
                    break;
                }
                for (int i = 0; i < 35; i++)
                {
                    Sbuf[i] = Sbuf[i + 1];
                }
                int b;
                if ((b = In.Read()) == -1)
                {
                    return(null);
                }
                Sbuf[35] = unchecked ((byte)b);
            }
            Key.Mid = Encdec.Dec_uint16le(Sbuf, 34) & 0xFFFF;
            return(Key);
        }
예제 #21
0
 /// <exception cref="System.IO.IOException"></exception>
 protected internal override void DoSend(ServerMessageBlock request)
 {
     lock (Buf)
     {
         ServerMessageBlock smb = request;
         int n = smb.Encode(Buf, 4);
         Encdec.Enc_uint32be(n & 0xFFFF, Buf, 0);
         if (Log.Level >= 4)
         {
             do
             {
                 Log.WriteLine(smb);
             }while (smb is AndXServerMessageBlock &&
                     (smb = ((AndXServerMessageBlock)smb).Andx) != null);
             if (Log.Level >= 6)
             {
                 Hexdump.ToHexdump(Log, Buf, 4, n);
             }
         }
         Out.Write(Buf, 0, 4 + n);
     }
 }
예제 #22
0
        /// <exception cref="System.IO.IOException"></exception>
        protected internal override void DoReceiveFragment(byte[] buf, bool isDirect)
        {
            int off;
            int flags;
            int length;

            if (buf.Length < MaxRecv)
            {
                throw new ArgumentException("buffer too small");
            }
            if (IsStart && !isDirect)
            {
                // start of new frag, do trans
                off = In.Read(buf, 0, 1024);
            }
            else
            {
                off = In.ReadDirect(buf, 0, buf.Length);
            }
            if (buf[0] != 5 && buf[1] != 0)
            {
                throw new IOException("Unexpected DCERPC PDU header");
            }
            flags = buf[3] & unchecked (0xFF);
            // next read is start of new frag
            IsStart = (flags & DcerpcConstants.DcerpcLastFrag) == DcerpcConstants.DcerpcLastFrag;
            length  = Encdec.Dec_uint16le(buf, 8);
            if (length > MaxRecv)
            {
                throw new IOException("Unexpected fragment length: " + length);
            }
            while (off < length)
            {
                off += In.ReadDirect(buf, off, length - off);
            }
        }
예제 #23
0
 /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
 public void WriteChar(int v)
 {
     Encdec.Enc_uint16be((short)v, _tmp, 0);
     Write(_tmp, 0, 2);
 }
예제 #24
0
        /// <exception cref="System.IO.IOException"></exception>
        private void Negotiate(int port, ServerMessageBlock resp)
        {
            lock (Sbuf)
            {
                if (port == 139)
                {
                    Ssn139();
                }
                else
                {
                    if (port == -1)
                    {
                        port = SmbConstants.DefaultPort;
                    }
                    // 445
                    Socket = new SocketEx(AddressFamily.InterNetwork,
                                          SocketType.Stream,
                                          ProtocolType.Tcp);

                    //TCPローカルポートは、毎回空いているものを使う。
                    //https://blogs.msdn.microsoft.com/dgorti/2005/09/18/only-one-usage-of-each-socket-address-protocolnetwork-addressport-is-normally-permitted/
                    Socket.Bind(new IPEndPoint(LocalAddr, 0));

                    Socket.Connect(new IPEndPoint(IPAddress.Parse(Address.GetHostAddress()),
                                                  port), // <- 445
                                   SmbConstants.ConnTimeout);

                    Socket.SoTimeOut = SmbConstants.SoTimeout;
                    Out = Socket.GetOutputStream();
                    In  = Socket.GetInputStream();
                }
                if (++Mid == 32000)
                {
                    Mid = 1;
                }
                NegotiateRequest.Mid = Mid;
                int n = NegotiateRequest.Encode(Sbuf, 4);
                Encdec.Enc_uint32be(n & 0xFFFF, Sbuf, 0);
                if (Log.Level >= 4)
                {
                    Log.WriteLine(NegotiateRequest);
                    if (Log.Level >= 6)
                    {
                        Hexdump.ToHexdump(Log, Sbuf, 4, n);
                    }
                }
                Out.Write(Sbuf, 0, 4 + n);
                Out.Flush();
                if (PeekKey() == null)
                {
                    throw new IOException("transport closed in negotiate");
                }
                int size = Encdec.Dec_uint16be(Sbuf, 2) & 0xFFFF;
                if (size < 33 || (4 + size) > Sbuf.Length)
                {
                    throw new IOException("Invalid payload size: " + size);
                }
                Readn(In, Sbuf, 4 + 32, size - 32);
                resp.Decode(Sbuf, 4);
                if (Log.Level >= 4)
                {
                    Log.WriteLine(resp);
                    if (Log.Level >= 6)
                    {
                        Hexdump.ToHexdump(Log, Sbuf, 4, n);
                    }
                }
            }
        }
예제 #25
0
 public virtual void Enc_ndr_hyper(long h)
 {
     Align(8);
     Encdec.Enc_uint64le(h, Buf, Index);
     Advance(8);
 }
예제 #26
0
        /// <exception cref="System.IO.IOException"></exception>
        private void Negotiate(int port, ServerMessageBlock resp)
        {
            lock (Sbuf)
            {
                if (port == 139)
                {
                    Ssn139();
                }
                else
                {
                    if (port == -1)
                    {
                        port = SmbConstants.DefaultPort;
                    }
                    // 445
                    Socket = new SocketEx(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    if (LocalAddr != null)
                    {
                        Socket.Bind2(new IPEndPoint(LocalAddr, LocalPort));
                    }

                    Socket.Connect(Address.GetHostAddress(), port);
                    //Socket.Connect(new IPEndPoint(IPAddress.Parse(Address.GetHostAddress()), port), SmbConstants.ConnTimeout);
                    Socket.SoTimeOut = SmbConstants.SoTimeout;
                    Out = Socket.GetOutputStream();
                    In  = Socket.GetInputStream();
                }
                if (++Mid == 32000)
                {
                    Mid = 1;
                }
                NegotiateRequest.Mid = Mid;
                int n = NegotiateRequest.Encode(Sbuf, 4);
                Encdec.Enc_uint32be(n & 0xFFFF, Sbuf, 0);
                if (Log.Level >= 4)
                {
                    Log.WriteLine(NegotiateRequest);
                    if (Log.Level >= 6)
                    {
                        Hexdump.ToHexdump(Log, Sbuf, 4, n);
                    }
                }
                Out.Write(Sbuf, 0, 4 + n);
                Out.Flush();
                if (PeekKey() == null)
                {
                    throw new IOException("transport closed in negotiate");
                }
                int size = Encdec.Dec_uint16be(Sbuf, 2) & 0xFFFF;
                if (size < 33 || (4 + size) > Sbuf.Length)
                {
                    throw new IOException("Invalid payload size: " + size);
                }
                Readn(In, Sbuf, 4 + 32, size - 32);
                resp.Decode(Sbuf, 4);
                if (Log.Level >= 4)
                {
                    Log.WriteLine(resp);
                    if (Log.Level >= 6)
                    {
                        Hexdump.ToHexdump(Log, Sbuf, 4, n);
                    }
                }
            }
        }
예제 #27
0
 /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
 public void WriteInt(int v)
 {
     Encdec.Enc_uint32be(v, _tmp, 0);
     Write(_tmp, 0, 4);
 }
예제 #28
0
 /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
 public void WriteDouble(double v)
 {
     Encdec.Enc_doublebe(v, _tmp, 0);
     Write(_tmp, 0, 8);
 }
예제 #29
0
 /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
 public void WriteFloat(float v)
 {
     Encdec.Enc_floatbe(v, _tmp, 0);
     Write(_tmp, 0, 4);
 }
예제 #30
0
 /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
 public void WriteLong(long v)
 {
     Encdec.Enc_uint64be(v, _tmp, 0);
     Write(_tmp, 0, 8);
 }