Last active
January 30, 2024 01:09
-
-
Save RickyLin/7b4c3ca276a683cf19871d74d5b91957 to your computer and use it in GitHub Desktop.
Get IP addresses that lead to SQL Server login failure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @LogIndex INT | |
DECLARE @LastCheckDate DATETIME | |
SET @LogIndex = 0 | |
SET @LastCheckDate = '2023-12-21' | |
DECLARE @Logs TABLE | |
( | |
LogDate DATETIME, | |
ProcessInfo NVARCHAR(4000), | |
[Text] NVARCHAR(MAX) | |
) | |
INSERT INTO @Logs | |
EXECUTE sp_readerrorlog @p1 = @LogIndex, @p2 = 1, @p3 = N'The login packet used to open the connection is structurally invalid' | |
INSERT INTO @Logs | |
EXECUTE sp_readerrorlog @p1 = @LogIndex, @p2 = 1, @p3 = N'Could not find a login matching the name provided' | |
INSERT INTO @Logs | |
EXECUTE sp_readerrorlog @p1 = @LogIndex, @p2 = 1, @p3 = N'Length specified in network packet payload did not match number of bytes read' | |
SELECT IPAddress, COUNT(*) AS [Count] | |
FROM ( | |
SELECT TRIM(' ] | |
' + CHAR(10) + CHAR(13) FROM RIGHT([Text], LEN([Text]) - CHARINDEX('[CLIENT:', [Text]) - LEN('CLIENT: '))) AS IPAddress | |
FROM @Logs | |
WHERE LogDate >= @LastCheckDate | |
) T | |
GROUP BY IPAddress | |
ORDER BY [Count] DESC | |
DECLARE @IPAddress VARCHAR(128) | |
SET @IPAddress = 'x.x.x.x' | |
SELECT TOP 10 * | |
FROM ( | |
SELECT TRIM(' ] | |
' + CHAR(10) + CHAR(13) FROM RIGHT([Text], LEN([Text]) - CHARINDEX('[CLIENT:', [Text]) - LEN('CLIENT: '))) AS IPAddress, * | |
FROM @Logs | |
WHERE LogDate >= @LastCheckDate | |
) T | |
where IPAddress = @IPAddress | |
ORDER BY LogDate DESC | |
SELECT TOP 10 * | |
FROM ( | |
SELECT TRIM(' ] | |
' + CHAR(10) + CHAR(13) FROM RIGHT([Text], LEN([Text]) - CHARINDEX('[CLIENT:', [Text]) - LEN('CLIENT: '))) AS IPAddress, * | |
FROM @Logs | |
WHERE LogDate >= @LastCheckDate | |
) T | |
where IPAddress = @IPAddress | |
ORDER BY LogDate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment