Exemple #1
0
        bool SkipCommand(string command, byte[] buffer, ref int index, int endIndex)
        {
            while (index < endIndex && commandIndex < command.Length)
            {
                if (buffer[index] != (byte)command[commandIndex])
                {
                    state = Pop3AuthCommandState.Error;
                    break;
                }

                commandIndex++;
                index++;
            }

            return(commandIndex == command.Length);
        }
Exemple #2
0
        IList <AuthenticationSecret> DetectApopSecrets(byte[] buffer, int offset, int endIndex)
        {
            var secrets = new List <AuthenticationSecret> ();
            int index   = offset;
            int startIndex;

            if (state == Pop3AuthCommandState.ApopNewLine)
            {
                return(EmptyAuthSecrets);
            }

            if (state == Pop3AuthCommandState.Apop)
            {
                if (SkipCommand("APOP ", buffer, ref index, endIndex))
                {
                    state = Pop3AuthCommandState.ApopUserName;
                }

                if (index >= endIndex || state == Pop3AuthCommandState.Error)
                {
                    return(EmptyAuthSecrets);
                }
            }

            if (state == Pop3AuthCommandState.ApopUserName)
            {
                startIndex = index;

                while (index < endIndex && buffer[index] != (byte)' ')
                {
                    index++;
                }

                if (index > startIndex)
                {
                    secrets.Add(new AuthenticationSecret(startIndex, index - startIndex));
                }

                if (index < endIndex)
                {
                    state = Pop3AuthCommandState.ApopToken;
                    index++;
                }

                if (index >= endIndex)
                {
                    return(secrets);
                }
            }

            startIndex = index;

            while (index < endIndex && buffer[index] != (byte)'\r')
            {
                index++;
            }

            if (index < endIndex)
            {
                state = Pop3AuthCommandState.ApopNewLine;
            }

            if (index > startIndex)
            {
                secrets.Add(new AuthenticationSecret(startIndex, index - startIndex));
            }

            return(secrets);
        }
Exemple #3
0
        IList <AuthenticationSecret> DetectUserPassSecrets(byte[] buffer, int offset, int endIndex)
        {
            var secrets = new List <AuthenticationSecret> ();
            int index   = offset;

            if (state == Pop3AuthCommandState.User)
            {
                if (SkipCommand("USER ", buffer, ref index, endIndex))
                {
                    state = Pop3AuthCommandState.UserName;
                }

                if (index >= endIndex || state == Pop3AuthCommandState.Error)
                {
                    return(EmptyAuthSecrets);
                }
            }

            if (state == Pop3AuthCommandState.UserName)
            {
                int startIndex = index;

                while (index < endIndex && buffer[index] != (byte)'\r')
                {
                    index++;
                }

                if (index > startIndex)
                {
                    secrets.Add(new AuthenticationSecret(startIndex, index - startIndex));
                }

                if (index < endIndex)
                {
                    state = Pop3AuthCommandState.UserNewLine;
                    index++;
                }

                if (index >= endIndex)
                {
                    return(secrets);
                }
            }

            if (state == Pop3AuthCommandState.UserNewLine)
            {
                if (buffer[index] == (byte)'\n')
                {
                    state        = Pop3AuthCommandState.Pass;
                    commandIndex = 0;
                    index++;
                }
                else
                {
                    state = Pop3AuthCommandState.Error;
                }

                if (index >= endIndex || state == Pop3AuthCommandState.Error)
                {
                    return(secrets);
                }
            }

            if (state == Pop3AuthCommandState.Pass)
            {
                if (SkipCommand("PASS ", buffer, ref index, endIndex))
                {
                    state = Pop3AuthCommandState.Password;
                }

                if (index >= endIndex || state == Pop3AuthCommandState.Error)
                {
                    return(EmptyAuthSecrets);
                }
            }

            if (state == Pop3AuthCommandState.Password)
            {
                int startIndex = index;

                while (index < endIndex && buffer[index] != (byte)'\r')
                {
                    index++;
                }

                if (index > startIndex)
                {
                    secrets.Add(new AuthenticationSecret(startIndex, index - startIndex));
                }

                if (index < endIndex)
                {
                    state = Pop3AuthCommandState.PassNewLine;
                    index++;
                }

                if (index >= endIndex)
                {
                    return(secrets);
                }
            }

            if (state == Pop3AuthCommandState.PassNewLine)
            {
                if (buffer[index] == (byte)'\n')
                {
                    state        = Pop3AuthCommandState.None;
                    commandIndex = 0;
                    index++;
                }
                else
                {
                    state = Pop3AuthCommandState.Error;
                }
            }

            return(secrets);
        }
Exemple #4
0
        public IList <AuthenticationSecret> DetectSecrets(byte[] buffer, int offset, int count)
        {
            if (!IsAuthenticating || state == Pop3AuthCommandState.Error || count == 0)
            {
                return(EmptyAuthSecrets);
            }

            int endIndex = offset + count;
            int index    = offset;

            if (state == Pop3AuthCommandState.None)
            {
                switch ((char)buffer[index])
                {
                case 'A':
                    state = Pop3AuthCommandState.A;
                    index++;
                    break;

                case 'U':
                    state        = Pop3AuthCommandState.User;
                    commandIndex = 1;
                    index++;
                    break;

                default:
                    state = Pop3AuthCommandState.Error;
                    break;
                }

                if (index >= endIndex || state == Pop3AuthCommandState.Error)
                {
                    return(EmptyAuthSecrets);
                }
            }

            if (state == Pop3AuthCommandState.A)
            {
                switch ((char)buffer[index])
                {
                case 'P':
                    state        = Pop3AuthCommandState.Apop;
                    commandIndex = 2;
                    index++;
                    break;

                case 'U':
                    state        = Pop3AuthCommandState.Auth;
                    commandIndex = 2;
                    index++;
                    break;

                default:
                    state = Pop3AuthCommandState.Error;
                    break;
                }

                if (index >= endIndex || state == Pop3AuthCommandState.Error)
                {
                    return(EmptyAuthSecrets);
                }
            }

            if (state >= Pop3AuthCommandState.Apop && state <= Pop3AuthCommandState.ApopNewLine)
            {
                return(DetectApopSecrets(buffer, index, endIndex));
            }

            if (state >= Pop3AuthCommandState.Auth && state <= Pop3AuthCommandState.AuthToken)
            {
                return(DetectAuthSecrets(buffer, index, endIndex));
            }

            return(DetectUserPassSecrets(buffer, index, endIndex));
        }
Exemple #5
0
        IList <AuthenticationSecret> DetectAuthSecrets(byte[] buffer, int offset, int endIndex)
        {
            int index = offset;

            if (state == Pop3AuthCommandState.Auth)
            {
                if (SkipCommand("AUTH ", buffer, ref index, endIndex))
                {
                    state = Pop3AuthCommandState.AuthMechanism;
                }

                if (index >= endIndex || state == Pop3AuthCommandState.Error)
                {
                    return(EmptyAuthSecrets);
                }
            }

            if (state == Pop3AuthCommandState.AuthMechanism)
            {
                while (index < endIndex && buffer[index] != (byte)' ' && buffer[index] != (byte)'\r')
                {
                    index++;
                }

                if (index < endIndex)
                {
                    if (buffer[index] == (byte)' ')
                    {
                        state = Pop3AuthCommandState.AuthToken;
                    }
                    else
                    {
                        state = Pop3AuthCommandState.AuthNewLine;
                    }

                    index++;
                }

                if (index >= endIndex)
                {
                    return(EmptyAuthSecrets);
                }
            }

            if (state == Pop3AuthCommandState.AuthNewLine)
            {
                if (buffer[index] == (byte)'\n')
                {
                    state = Pop3AuthCommandState.AuthToken;
                    index++;
                }
                else
                {
                    state = Pop3AuthCommandState.Error;
                }

                if (index >= endIndex || state == Pop3AuthCommandState.Error)
                {
                    return(EmptyAuthSecrets);
                }
            }

            int startIndex = index;

            while (index < endIndex && buffer[index] != (byte)'\r')
            {
                index++;
            }

            if (index < endIndex)
            {
                state = Pop3AuthCommandState.AuthNewLine;
            }

            if (index == startIndex)
            {
                return(EmptyAuthSecrets);
            }

            var secret = new AuthenticationSecret(startIndex, index - startIndex);

            if (state == Pop3AuthCommandState.AuthNewLine)
            {
                index++;

                if (index < endIndex)
                {
                    if (buffer[index] == (byte)'\n')
                    {
                        state = Pop3AuthCommandState.AuthToken;
                    }
                    else
                    {
                        state = Pop3AuthCommandState.Error;
                    }
                }
            }

            return(new AuthenticationSecret[] { secret });
        }