<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SQL, Analysis Services &amp; related stories. : SQL Server Administration</title><link>http://blogs.msdn.com/ikovalenko/archive/tags/SQL+Server+Administration/default.aspx</link><description>Tags: SQL Server Administration</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Switching context with using EXECUTE AS ... not always switch it in fact as you wish.</title><link>http://blogs.msdn.com/ikovalenko/archive/2007/03/01/switching-context-with-using-execute-as-not-always-switch-it-in-fact-as-you-wish.aspx</link><pubDate>Thu, 01 Mar 2007 11:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1777963</guid><dc:creator>Igor Kovalenko</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ikovalenko/comments/1777963.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ikovalenko/commentrss.aspx?PostID=1777963</wfw:commentRss><description>&lt;P&gt;In this demo i will&amp;nbsp;try to explain why SET TRUSTWORTHY ON on some databases may&amp;nbsp;make the sysadmin job unsafe. In previous posts i explained how db owner (or any developer) can try to implemet simplest luring attack against server sysadmin. According to BOL to avoid this problem sysadmin should switch to the context of the account/login with lowest possible privileges. Lets try to test this approach.&lt;/P&gt;
&lt;P&gt;USE master&lt;BR&gt;GO&lt;/P&gt;
&lt;P&gt;-- create test database&lt;BR&gt;CREATE DATABASE [TestDB] ON PRIMARY&amp;nbsp;( NAME = N'TestDB', FILENAME = N'C:\TestDB.mdf', SIZE = 3072KB, FILEGROWTH = 1024KB )&lt;BR&gt;&amp;nbsp;LOG ON ( NAME = N'TestDB_log', FILENAME = N'C:\TestDB_log.ldf', SIZE = 1024KB, FILEGROWTH = 10%)&lt;BR&gt;GO&lt;/P&gt;
&lt;P&gt;-- set option&lt;BR&gt;EXEC dbo.sp_dbcmptlevel @dbname=N'TestDB', @new_cmptlevel=90&lt;BR&gt;GO&lt;/P&gt;
&lt;P&gt;-- first time test with TRUSTWORTHY = OFF&lt;/P&gt;
&lt;P&gt;ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF&lt;BR&gt;GO&lt;/P&gt;
&lt;P&gt;-- create login Test&lt;BR&gt;USE [master]&lt;BR&gt;GO&lt;BR&gt;CREATE LOGIN [Test] WITH PASSWORD=N'111', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF&lt;BR&gt;GO&lt;/P&gt;
&lt;P&gt;USE [TestDB]&lt;BR&gt;GO&lt;/P&gt;
&lt;P&gt;-- create user test&lt;BR&gt;CREATE USER [Test] FOR LOGIN [Test]&lt;BR&gt;GO&lt;/P&gt;
&lt;P&gt;-- create test DDL trigger - fired on any DENY event&lt;BR&gt;CREATE TRIGGER [ddl_test_trigger]&lt;BR&gt;ON DATABASE &lt;BR&gt;FOR DENY_DATABASE&lt;BR&gt;AS &lt;BR&gt;&amp;nbsp;&lt;BR&gt;SET NOCOUNT ON&lt;/P&gt;
&lt;P&gt;&amp;nbsp;IF IS_SRVROLEMEMBER ('sysadmin') = 1&lt;BR&gt;&amp;nbsp; PRINT 'Sysadmin found, security can be broken easily.'&lt;BR&gt;&amp;nbsp;ELSE&lt;BR&gt;&amp;nbsp; PRINT 'Sysadmin not found, you are in safe.'&lt;BR&gt;&amp;nbsp;&lt;BR&gt;GO&lt;/P&gt;
&lt;P&gt;-- test under sysadmin (sa) account&lt;BR&gt;DECLARE @cmd nvarchar(1000)&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;SET @cmd = 'DENY BACKUP DATABASE TO [Test]'&lt;/EM&gt;&lt;/STRONG&gt; &lt;BR&gt;&lt;STRONG&gt;&lt;EM&gt;EXEC (@cmd)&lt;/EM&gt;&lt;BR&gt;&lt;/STRONG&gt;GO&lt;/P&gt;
&lt;P&gt;-- &lt;STRONG&gt;you'll see "Sysadmin found, security can be broken easily."&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;-- so the next step according to BOL - switch to lowest privileges context for desired operation&lt;BR&gt;DECLARE @cmd nvarchar(1000)&lt;BR&gt;&lt;STRONG&gt;&lt;EM&gt;SET @cmd = 'DENY BACKUP DATABASE TO [Test]' &lt;BR&gt;EXEC (@cmd) AS USER = 'dbo'&lt;BR&gt;&lt;/EM&gt;&lt;/STRONG&gt;GO&lt;/P&gt;
&lt;P&gt;-- &lt;STRONG&gt;you'll see now "Sysadmin not found, you are in safe."&lt;BR&gt;&lt;/STRONG&gt;-- Thats is our desired goal - to be in safe. Lets try to set TRUSTWORTHY ON&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;ALTER DATABASE [TestDB] SET TRUSTWORTHY ON&lt;BR&gt;&lt;/EM&gt;&lt;/STRONG&gt;GO&lt;/P&gt;
&lt;P&gt;-- try again the "safe" version&lt;BR&gt;DECLARE @cmd nvarchar(1000)&lt;BR&gt;&lt;STRONG&gt;&lt;EM&gt;SET @cmd = 'DENY BACKUP DATABASE TO [Test]' &lt;BR&gt;EXEC (@cmd) AS USER = 'dbo'&lt;BR&gt;&lt;/EM&gt;&lt;/STRONG&gt;GO&lt;/P&gt;
&lt;P&gt;-- &lt;STRONG&gt;you'll see again "Sysadmin found, security can be broken easily."&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;-- clear objects&lt;BR&gt;USE Master&lt;BR&gt;GO&lt;/P&gt;
&lt;P&gt;DROP DATABASE [TestDB]&lt;BR&gt;GO&lt;/P&gt;
&lt;P&gt;DROP LOGIN [Test]&lt;BR&gt;GO&lt;/P&gt;
&lt;P&gt;--------------------------------------------------------&lt;/P&gt;
&lt;P&gt;As as result of this demo i would recommend you do not use TRUSTWORTHY ON on your databases. Otherwise before making any &lt;BR&gt;changes under 'sa' account in unknown databases please check TRUSTWORTHY setting for the database you are &lt;BR&gt;working with, because simple EXECUTE AS .... (even with cookie) will not&amp;nbsp;protect you&amp;nbsp;from loosing you privileges or &lt;BR&gt;GRANT 'sa' privileges to everybody (as explained in&amp;nbsp;my previous articles). And be always in safe :-).&lt;/P&gt;
&lt;P&gt;--------------------------------------------------------&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1777963" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ikovalenko/archive/tags/SQL+Server+Administration/default.aspx">SQL Server Administration</category><category domain="http://blogs.msdn.com/ikovalenko/archive/tags/SQL+Server+2005+Security/default.aspx">SQL Server 2005 Security</category><category domain="http://blogs.msdn.com/ikovalenko/archive/tags/SQL+Server+2005+Administration/default.aspx">SQL Server 2005 Administration</category></item><item><title>ALTER DATABASE ... ALTER COLLATION (FORCED).</title><link>http://blogs.msdn.com/ikovalenko/archive/2006/12/03/alter-database-alter-collation-forced.aspx</link><pubDate>Sun, 03 Dec 2006 01:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1195859</guid><dc:creator>Igor Kovalenko</dc:creator><slash:comments>34</slash:comments><comments>http://blogs.msdn.com/ikovalenko/comments/1195859.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ikovalenko/commentrss.aspx?PostID=1195859</wfw:commentRss><description>&lt;P&gt;As all of you know Microsoft has made a small problem for all SQL DBA and developers. If you&amp;nbsp;have a&amp;nbsp;database with any of SQL_xxxxx collations (for more details please have a look at the TERTIARY_WEIGHTS() function &lt;A class="" title=here href="http://msdn2.microsoft.com/en-us/library/ms186881.aspx" target=_blank mce_href="http://msdn2.microsoft.com/en-us/library/ms186881.aspx"&gt;here&lt;/A&gt;), you will defenetily have a significant problem with index search and ordering performance if you'll try to upgrade your SQL Server&amp;nbsp;with 2005 version. Some&amp;nbsp;of developers&amp;nbsp;was excited when first time reading this article, really :-). &lt;/P&gt;
&lt;P&gt;Suddenly ALTER DATABASE .. ALTER COLLATION command will change database collation for you, but all objects will stay "as is" with old collation (by design). The only way to change collation for all existing objects is to create a new empty database with new desired collation and copy data into this new database (copy wizzard, for example).&lt;/P&gt;
&lt;P&gt;To sort this issue out i created this small script (in attachment). It was tested both manually and with using&amp;nbsp;VSTS DBPro, and i can confirm that you can change the collation of your database without any problem and manual work.&amp;nbsp;As an input you should provide it with database with one collation, and as output you will have the same database with&amp;nbsp;another (desired) collation. So as for me this is an analoque of ALTER DATABASE ... ALTER COLLATION FORCED command :-).&amp;nbsp;How it works:&lt;/P&gt;
&lt;P&gt;- you should use sqlcmd or SSMS in sqlcmd mode,&lt;/P&gt;
&lt;P&gt;- specify database name as a parameter,&lt;/P&gt;
&lt;P&gt;- specify desired collation as a parameter, &lt;/P&gt;
&lt;P&gt;- run it and wait for results.&lt;/P&gt;
&lt;P&gt;What is not covered by the script: table and index partitioning.&amp;nbsp;I guess that if you are using these advanced options, you&amp;nbsp;are smart enough to&amp;nbsp;change this script to add required feature. Mostly i created this script for the following scenario: (1) detach database from SQL Server 6.5/7.0/200 version; (2) attach it to SQL Server 2005; (3) run this script; (4) use your database asap.&lt;/P&gt;
&lt;P&gt;Some words about performance: than more tables with computed columns (computed column not at the end of the table) you have than more time you will have to wait. 100 Gigabyte db can be easily transformed within one hour on AI64 4way with EVA8000.&lt;/P&gt;
&lt;P&gt;Also&amp;nbsp;it is possible to&amp;nbsp;use this script to&amp;nbsp;study SQL 2005 system views and relationship between them. &lt;/P&gt;
&lt;P&gt;Many thanks for any reply and comments.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;---- January 2008&lt;/P&gt;
&lt;P&gt;Many thanks to WaitForPete, script updated to fix these (and some other) issues.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1195859" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/ikovalenko/attachment/1195859.ashx" length="105974" type="application/octet-stream" /><category domain="http://blogs.msdn.com/ikovalenko/archive/tags/SQL+Server+2005/default.aspx">SQL Server 2005</category><category domain="http://blogs.msdn.com/ikovalenko/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://blogs.msdn.com/ikovalenko/archive/tags/SQL+Server+Administration/default.aspx">SQL Server Administration</category></item></channel></rss>