SQL Server Security

FDN » SQL Server » SQL Server Security

SQL Server Security

SQL Server 2000 implements security at three levels: server logins, database users, and object permissions. This guide covers authentication, authorization, and hardening.

Authentication Modes

ModeDescriptionWhen to Use
Windows AuthenticationUses AD/Windows accounts. No separate password.Recommended for intranet apps
Mixed ModeAllows both Windows and SQL Server logins.When non-Windows clients need access

Creating Logins and Users

-- Create a Windows login
EXEC sp_grantlogin 'CORP\jsmith'

-- Create a SQL Server login
EXEC sp_addlogin 'appuser', 'Str0ngP@ss!', 'FlamenetDB'

-- Map login to a database user
USE FlamenetDB
EXEC sp_grantdbaccess 'CORP\jsmith', 'jsmith'

-- Add user to a database role
EXEC sp_addrolemember 'db_datareader', 'jsmith'
EXEC sp_addrolemember 'db_datawriter', 'jsmith'

Fixed Database Roles

RolePermissions
db_ownerFull control of the database
db_datareaderSELECT on all tables and views
db_datawriterINSERT, UPDATE, DELETE on all tables
db_ddladminCREATE, ALTER, DROP objects
db_securityadminManage roles and permissions
db_denydatareaderCannot SELECT any data
db_denydatawriterCannot modify any data

Object-Level Permissions

-- Grant SELECT on a specific table
GRANT SELECT ON Employees TO jsmith

-- Grant EXECUTE on a stored procedure
GRANT EXECUTE ON usp_GetEmployeesByDept TO webapp_role

-- Revoke permissions
REVOKE INSERT ON Employees FROM jsmith

-- Deny (overrides any grant)
DENY DELETE ON Employees TO jsmith

Hardening Checklist

  • Use Windows Authentication mode when possible
  • Set a strong sa password (even if not using Mixed Mode)
  • Remove the BUILTIN\Administrators login from sysadmin role
  • Disable xp_cmdshell unless required
  • Drop sample databases (pubs, Northwind) on production servers
  • Apply SQL Server 2000 SP4 and all post-SP4 security patches
  • Restrict SQL Server port (1433) access with firewalls
  • Hide the SQL Server instance to prevent enumeration
« Developer Network ‹ Stored Procedures Best Practices ADO Connection Strings ›