IF OBJECT_ID('deleteDataFlowElement','P') IS NOT NULL
DROP PROCEDURE deleteDataFlowElement
GO
CREATE PROCEDURE deleteDataFlowElement @findingIdToDelete INT
AS
DELETE FROM DataFlowElement WHERE findingId = @findingIdToDELETE;
GO
SELECT f.nativeId AS findingNativeId,rpa.nativeId AS rpaNativeId,f.vulnerabilityId AS findingVulnId,f.id AS findingId into TempTable
FROM Finding f
INNER JOIN Scan s ON f.scanId = s.id
INNER JOIN RemoteProviderApplication rpa ON (s.remoteProviderEntityId=rpa.remoteProviderEntityId AND s.applicationChannelId = rpa.applicationChannel_id AND s.applicationId=rpa.applicationId)
WHERE f.nativeId LIKE '%-legacy';
IF OBJECT_ID('updateLegacyNativeIdVeracode','P') IS NOT NULL
DROP PROCEDURE updateLegacyNativeIdVeracode
GO
CREATE PROCEDURE updateLegacyNativeIdVeracode
AS
DECLARE @findingNativeId varchar(256);
DECLARE @rpaNativeId varchar(1024);
DECLARE @findingVulnId INT;
DECLARE @findingId INT;
DECLARE @findingToDELETE INT;
DECLARE @correctNativeId varchar(256);
DECLARE @findingActive BIT;
DECLARE cursorNativeId
CURSOR FOR
SELECT * FROM TempTable;
OPEN cursorNativeId;
FETCH cursorNativeId INTO @findingNativeId, @rpaNativeId, @findingVulnId, @findingId;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @correctNativeId = concat(substring(@findingNativeId,1,charindex('-',@findingNativeId)),@rpaNativeId);
IF EXISTS (SELECT id FROM Finding WHERE vulnerabilityId = @findingVulnId AND nativeId = @correctNativeId)
BEGIN
SELECT @findingToDELETE = id, @findingActive = active FROM Finding WHERE nativeId = @correctNativeId AND vulnerabilityId = @findingVulnId AND id != @findingId;
EXEC DELETEDataFlowElement @findingIdToDELETE = @findingToDELETE;
UPDATE Finding SET nativeId=@correctNativeId , active=@findingActive WHERE id = @findingId;
DELETE FROM Finding WHERE id = @findingToDELETE;
END
FETCH cursorNativeId INTO @findingNativeId, @rpaNativeId, @findingVulnId, @findingId;
END
CLOSE cursorNativeId
DEALLOCATE cursorNativeId
GO
EXEC updateLegacyNativeIdVeracode;
DROP PROCEDURE updateLegacyNativeIdVeracode;
DROP TABLE TempTable;
SELECT f.nativeId AS findingNativeId,rpa.nativeId AS rpaNativeId,f.vulnerabilityId AS findingVulnId,f.id AS findingId ,substring(f.rawFinding,PATINDEX('%SimilarityId%',f.rawFinding)+14,charindex('"',substring(f.rawFinding,PATINDEX('%SimilarityId%',f.rawFinding)+14,35))-1) AS similarityId,substring(substring(rawFinding,PATINDEX('%PathId%',rawFinding)+7,10),1,CHARINDEX('"',substring(rawFinding,PATINDEX('%PathId%',rawFinding)+7,10))-1) AS pathId into TempTable
FROM Finding AS f
INNER JOIN Scan AS s ON f.scanId = s.id
INNER JOIN RemoteProviderApplication rpa ON (s.remoteProviderEntityId=rpa.remoteProviderEntityId AND s.applicationChannelId = rpa.applicationChannel_id AND s.applicationId=rpa.applicationId)
WHERE f.nativeId LIKE '%-CMlegacy';
IF OBJECT_ID('updateLegacyNativeIdCheckmarx','P') IS NOT NULL
DROP PROCEDURE updateLegacyNativeIdCheckmarx
GO
CREATE PROCEDURE updateLegacyNativeIdCheckmarx
AS
DECLARE @findingNativeId varchar(256);
DECLARE @rpaNativeId varchar(1024);
DECLARE @findingVulnId INT;
DECLARE @findingId INT;
DECLARE @pathId VARCHAR(256);
DECLARE @similarityId VARCHAR(256);
DECLARE @findingIdToDELETE INT;
DECLARE @correctNativeId varchar(256);
DECLARE @findingActive BIT;
DECLARE cursorNativeId
CURSOR FOR
SELECT * FROM TempTable;
OPEN cursorNativeId;
FETCH cursorNativeId INTO @findingNativeId, @rpaNativeId, @findingVulnId, @findingId, @similarityId, @pathId;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @correctNativeId = LOWER(CONVERT(VARCHAR(50),HASHBYTES('MD5', concat('SimilarityId: ', @similarityId, ', PathId: ', @pathId)),2));
IF EXISTS (SELECT id FROM Finding WHERE vulnerabilityId = @findingVulnId AND nativeId = @correctNativeId)
BEGIN
SELECT @findingIdToDELETE = id, @findingActive = active FROM Finding WHERE nativeId = @correctNativeId AND vulnerabilityId = @findingVulnId AND id != @findingId;
EXEC DELETEDataFlowElement @findingIdToDELETE = @findingIdToDELETE;
UPDATE Finding SET nativeId=@correctNativeId , active=@findingActive WHERE id = @findingId;
DELETE FROM Finding WHERE id = @findingIdToDELETE;
END
FETCH cursorNativeId INTO @findingNativeId, @rpaNativeId, @findingVulnId, @findingId, @similarityId, @pathId;
END
CLOSE cursorNativeId;
DEALLOCATE cursorNativeId;
GO
EXEC updateLegacyNativeIdCheckmarx;
DROP PROCEDURE updateLegacyNativeIdCheckmarx;
DROP TABLE TempTable;
SELECT f.nativeId AS findingNativeId,rpa.nativeId AS rpaNativeId,f.vulnerabilityId AS findingVulnId,f.id AS findingId, f.channelVulnerabilityId AS findingChannelId, f.surfaceLocationId AS findingSurfaceId into TempTable
FROM Finding f
INNER JOIN Scan s ON (f.scanId = s.id)
INNER JOIN RemoteProviderApplication rpa ON (s.remoteProviderEntityId=rpa.remoteProviderEntityId AND s.applicationChannelId = rpa.applicationChannel_id AND s.applicationId=rpa.applicationId)
WHERE f.nativeId LIKE '%-QWlegacy';
IF OBJECT_ID('updateLegacyNativeIdQualys','P') IS NOT NULL
DROP PROCEDURE updateLegacyNativeIdQualys
GO
CREATE PROCEDURE updateLegacyNativeIdQualys
AS
DECLARE @findingNativeId varchar(256);
DECLARE @rpaNativeId varchar(1024);
DECLARE @findingVulnId INT;
DECLARE @findingId INT;
DECLARE @findingToDELETE INT;
DECLARE @findingChannelId INT;
DECLARE @findingSurfaceId INT;
DECLARE @channelVulnName varchar(256);
DECLARE @url varchar(256);
DECLARE @parameter varchar(256);
DECLARE @correctNativeId varchar(256);
DECLARE @findingActive BIT;
DECLARE cursorNativeId
CURSOR FOR
SELECT * FROM TempTable;
OPEN cursorNativeId;
FETCH cursorNativeId INTO @findingNativeId, @rpaNativeId, @findingVulnId, @findingId, @findingChannelId, @findingSurfaceId;
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @channelVulnName = cv.name FROM Finding f INNER JOIN ChannelVulnerability cv ON (f.channelVulnerabilityId = cv.id) WHERE f.id = @findingId;
SELECT @url = concat(sl.protocol, '://', sl.host, ':', sl.port, sl.path) FROM Finding f INNER JOIN SurfaceLocation sl ON (f.surfaceLocationId = sl.id) WHERE f.id = @findingId;
SELECT @parameter = sl.parameter FROM Finding f INNER JOIN SurfaceLocation sl ON (f.surfaceLocationId = sl.id) WHERE f.id = @findingId;
IF @parameter is not null
SET @correctNativeId = LOWER(CONVERT(VARCHAR(50),HASHBYTES('MD5', lower(concat(@channelVulnName,@url,@parameter))),2));
ELSE
SET @correctNativeId = LOWER(CONVERT(VARCHAR(50),HASHBYTES('MD5', lower(concat(@channelVulnName,@url))),2));
IF EXISTS (SELECT id FROM Finding WHERE vulnerabilityId = @findingVulnId AND nativeId = @correctNativeId)
BEGIN
SELECT @findingToDELETE = id, @findingActive = active FROM Finding WHERE nativeId = @correctNativeId AND vulnerabilityId = @findingVulnId AND id != @findingId;
EXEC DELETEDataFlowElement @findingIdToDELETE = @findingToDELETE;
UPDATE Finding SET nativeId=@correctNativeId , active=@findingActive WHERE id = @findingId;
DELETE FROM Finding WHERE id = @findingToDELETE;
END
FETCH cursorNativeId INTO @findingNativeId, @rpaNativeId, @findingVulnId, @findingId, @findingChannelId, @findingSurfaceId;
END
CLOSE cursorNativeId;
DEALLOCATE cursorNativeId;
GO
EXEC updateLegacyNativeIdQualys;
DROP PROCEDURE updateLegacyNativeIdQualys;
DROP PROCEDURE DELETEDataFlowElement;
DROP TABLE TempTable; |