Exemplo n.º 1
0
    public static async void InstantInfoUser(HttpListenerContext ctx)
    {
        WebSocketContext wsc;
        WebSocket        ws;

        try {
            wsc = await ctx.AcceptWebSocketAsync(null);

            ws = wsc.WebSocket;
        } catch (WebSocketException ex) {
            ctx.Response.Close();
            Logging.Err(ex);
            return;
        }

        string sessionId = ctx.Request.Cookies["sessionid"]?.Value ?? null;

        if (sessionId is null)
        {
            ctx.Response.Close();
            return;
        }

        try {
            byte[] buff = new byte[2048];
            WebSocketReceiveResult receiveResult = await ws.ReceiveAsync(new ArraySegment <byte>(buff), CancellationToken.None);

            string filename = Encoding.Default.GetString(buff, 0, receiveResult.Count);
            if (!Database.users.ContainsKey(filename))
            {
                await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);

                return;
            }
            Database.DbEntry user = (Database.DbEntry)Database.users[filename];

            string username;
            if (user.hash.ContainsKey("USERNAME"))
            {
                username = ((string[])user.hash["USERNAME"])[0];
            }
            else
            {
                await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);

                return;
            }

            try {
                SearchResult sr = ActiveDirectory.GetUser(username);
                if (sr != null)
                {
                    if (sr.Properties["lastLogonTimestamp"].Count > 0)
                    {
                        if (Int64.TryParse(sr.Properties["lastLogonTimestamp"][0].ToString(), out long time) && time > 0)
                        {
                            WsWriteText(ws, $"last logon{(char)127}{DateTime.FromFileTime(time)}{(char)127}Active directory");
                        }
                    }

                    if (sr.Properties["lastLogoff"].Count > 0)
                    {
                        if (Int64.TryParse(sr.Properties["lastLogoff"][0].ToString(), out long time) && time > 0)
                        {
                            WsWriteText(ws, $"last logoff{(char)127}{DateTime.FromFileTime(time)}{(char)127}Active directory");
                        }
                    }

                    if (sr.Properties["badPasswordTime"].Count > 0)
                    {
                        if (Int64.TryParse(sr.Properties["badPasswordTime"][0].ToString(), out long time) && time > 0)
                        {
                            WsWriteText(ws, $"bad password time{(char)127}{DateTime.FromFileTime(time)}{(char)127}Active directory");
                        }
                    }

                    if (sr.Properties["lockoutTime"].Count > 0)
                    {
                        if (Int64.TryParse(sr.Properties["lockoutTime"][0].ToString(), out long time) && time > 0)
                        {
                            WsWriteText(ws, $"lockout time{(char)127}{DateTime.FromFileTime(time)}{(char)127}Active directory");
                        }
                    }
                }
            } catch { }

            if (user.hash.ContainsKey("PASSWORD"))
            {
                string password = ((string[])user.hash["PASSWORD"])[0];
                if (password.Length > 0 && PasswordStrength.Entropy(password) < 28)
                {
                    WsWriteText(ws, $"!{(char)127}{"Weak password"}{(char)127}");
                }
            }

            await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
        } catch (Exception ex) {
            Logging.Err(ex);
        }
    }