28.10.2014 Views

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

122 Chapter 3 • Reviewing Code for <strong>SQL</strong> <strong>Injection</strong><br />

if @dbname is null<br />

begin<br />

end<br />

else<br />

--enumerate all databases.<br />

DECLARE #db CURSOR FOR Select Name from master…sysdatabases<br />

declare @c_dbname varchar(64)<br />

OPEN #db FETCH #db INTO @c_dbname<br />

while @@FETCH_STATUS -1<br />

begin<br />

end<br />

execute find_text_in_sp @text, @c_dbname<br />

FETCH #db INTO @c_dbname<br />

CLOSE #db DEALLOCATE #db<br />

begin<br />

declare @sql varchar(250)<br />

--create the find like comm<strong>and</strong><br />

select @sql = 'select ''' + @dbname + ''' as db, o.name,m.definition '<br />

select @sql = @sql + ' from '+@dbname+'.sys.sql_modules m '<br />

select @sql = @sql + ' inner join '+@dbname+'…sysobjects o on<br />

m.object_id=o.id'<br />

END<br />

end<br />

select @sql = @sql + ' where [definition] like ''%'+@text+'%'''<br />

execute (@sql)<br />

Make sure you drop the procedure when you’re finished! You can invoke the stored<br />

procedure like so:<br />

execute grep_sp 'sp_executesql';<br />

execute grep_sp 'EXEC';<br />

You can use the following T-<strong>SQL</strong> comm<strong>and</strong> to list user-defined stored procedures on an<br />

<strong>SQL</strong> Server 2008 database:<br />

SELECT name FROM sys.procedures ORDER BY name asc<br />

You can use the following T-<strong>SQL</strong> script to search all stored procedures within an <strong>SQL</strong><br />

Server 2008 database server <strong>and</strong> print their source, if the respective line is uncommented.<br />

You will need to closely scrutinize the output, but it should help you narrow your search.<br />

DECLARE @name VARCHAR(50) -- database name<br />

DECLARE db_cursor CURSOR FOR<br />

SELECT name FROM sys.procedures;<br />

OPEN db_cursor

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!