Showing posts with label security. Show all posts
Showing posts with label security. Show all posts

Friday, 1 June 2018

Some simple secure development standards

These are mainly based around ASP.NET/IIS – but the same principles can be applied everywhere.

These are just a few that I’ve seen highlighted a lot in pen tests of late. 

Nothing is going to make you 100% secure but remember – defence in depth.  Lots of defences adds up.

I may get some others written up some time.

(HTML/URL) Encode everything

Why

This helps prevent Cross Site Scripting (XSS) attacks.  XSS attacks occur when user input (or other data) is displayed on the browser.  The attacker puts javascript in the input and this is then executed by the browser. 

Examples

sending the user a link with a query string which contains a value that is displayed on the target page – look at google and you’ll see the search is in the url and displayed on the browser.  (Reflected XSS).

Putting data in to the system that is displayed on someone elses browser (e.g. facebook page) (Stored XSS).  For a great example of this watch this watch  https://www.youtube.com/watch?v=EYMGAoIx8yk&feature=youtu.be FROM 8:30

How?

Use strongly typed parameterised queries (that’s stored procs or EF).

Why

To mitigate against SQL injection attacks.

Parameterised queries (and stored procs) should treat the input as input – not build sql strings from them so cannot be manipulated as easily.

How?

Use an ORM (Entity Framework) AND DO NOT GENERATE SQL ON THE FLY IN YOUR CODE.

Use strongly typed stored procedures and DO NOT GENERATE SQL ON THE FLY IN YOUR SQL.

Combine this with

  • least privilege (execute on stored procs or data reader/writer on appropriate tables for EF) – note SPROCS allow a more minimal access as an attaker owning the applications connection can still not read whole tables but needs to go through stored procs.
  • A defence mechanism for preventing developers from still generating sql on the fly.
    • e.g. for stored procs review them all.

EF does not particularly encourage sql on the fly but none the less be aware application developers now have a connection available that can do data reader/writer functions.  I have seen this result in a successful attack – not surprisingly on the last (hastily implemented) CR implementation.

Implement some secure configuration/hardening

These are some simple hardening recommendations – may not do much but will not make you less secure!

Why?

  • Your application and server (IIS) etc. uses technologies.  These may have known vulnerabilities.  Advertising the technologies is the same as advertising the weaknesses.  Security by obscurity is not security but giving attackers a list of sites to attack when a vulnerability is published is dumb.
  • Reduce the attack surface area.

How?

•Disclosure of server type / Disclosure of technology

* For azure - (https://azure.microsoft.com/en-us/blog/removing-standard-server-headers-on-windows-azure-web-sites/) – this should work in IIS too – but depends on later frameworks.

For IIS - https://www.saotn.org/remove-iis-server-version-http-response-header/

<configuration> <system.webServer> <security> <requestFiltering> <verbs allowUnlisted="false" > <add verb="GET" allowed="true" />
    
<add verb="POST" allowed="true" /></verbs>
  
</requestFiltering>
 
</security>
</system.webServer>
</configuration>

See - https://docs.microsoft.com/en-us/iis/manage/configuring-security/use-request-filtering

Use least privilege database access (or any other access!)

Why?

Defence in depth – if someone owns your web server or another defence (e.g. sql injection) fails – limits the damage can be done.

How

  • Don’t use dbowner/sa
  • Do use separate account for applications with privilege to execute stored procs (GRANT EXEC ON proc TO user) or (if using EF) data reader/writer as required on specific tables.
  • Do this from the beginning in all your environments so you don’t hit all the issues late in your dev cycle.

Configure your security headers and cookies

See - https://securityheaders.com/

Why?

Modern browsers are adding additional protection against vulnerabilities – xss, disclosure.

How?

See https://securityheaders.com/

If your are https (and you should be)

Enable hsts

<system.webServer>

<httpProtocol>

<customHeaders>

<add name="Strict-Transport-Security" value="max-age=31536000"/>

</customHeaders>

</httpProtocol>

</system.webServer>

Set requires SSL on all your cookies - <httpCookies requireSSL="true" />

(and your forms auth config too if you have it).

X-Frame-Options – do you want your pages to be embedded in another site.  Allowing this facilitates an attacker making a fake site.

X-XSS-Protection – tells the browser to turn on xss filters.

X-Content-Type-Options – stops content type sniffing

Content Security Policy – allows you to list where your page downloads from so you can protect from downloading malicious code.

Referrer Policy – limits the “Referrer” information in http requests.  For example, if you download fonts from google do you want google to be able to track people on your site through the referrer header.  Remember you may letting a third party know the IP (personal information under GDPR) the activity on your site.




Friday, 18 November 2016

Sprint 4–put it live.

Not quite as easy as deploying the sql as connecting locally I’ve been using my own credentials.  For an azure connection I need to:

  1. Create a database role and grant exec on the stored procs to that role.
  2. Create a new database login
  3. Put the database login in the role.

Why don’t I just use the “sa” password.

Well – least privilege – a good security practice.

Monday, 19 October 2015

Watch out for certificate expiration in database mirroring endpoints.

When database mirroring is used between two servers that are not in a domain, as can often be the case when using hosting services from another vendor, certificates are used to secure endpoints.

The MSDN article - Example: Setting Up Database Mirroring Using Certificates (Transact-SQL) explains how to do this.

What it does not mention is that SQL server generated certificates only last a year by default.  A year later all your mirroring starts to fail!

The following event log error helped me diagnose the situation -

“…Connection handshake failed. The certificate used by this endpoint was not found: Certificate expired. Use DBCC CHECKDB in master database to verify the metadata integrity of the endpoints. State 85…”

To rectify you have to generate new certificate(s), copy them across the mirrored servers,  and change the endpoint setting to use the new certificates.

This is explained here -http://www.sql-server-performance.com/faq/database_mirror_certificate_expired_p1.aspx

Note –

1) I had to specify a start date when specifying an end date else I got a warning.

2) I used the following command to change the certificate -

ALTER ENDPOINT Endpoint_Mirroring FOR DATABASE_MIRRORING (AUTHENTICATION = CERTIFICATE cert_Mirror_01)

I then had to restart the mirroring process again for all databases as I’d disable the mirroring.

So – if your creating certificates for mirroring endpoints – give them a nice big expiration date!

Thursday, 19 February 2009

Securing Reporting Services Web Services.

 

Many web based applications use reporting services to facilitate report production. We have used them extensively in many of our internal and external applications. The main drivers for deployment have been:
  • to produce PDF and other format documents
  • they are part of SQL Server
  • they have a very simple and effective deployment model
Although placing reporting services behind a second level of firewalls will secure it from external attack a mechanism is required to authenticate the client – otherwise internal users will be able to run any reports. To this end we would normally create a domain or local NT account and grant that account read access to the applications. These credentials can then be used to authenticate when using web services to execute reports.
However, when using the Microsoft ReportViewer web control it is not at first clear how to set the network credentials to call reporting services.
In order to set the credentials you must first create a class that implements the IReportServerCredentials interface as follows –
[Serializable]
    public class RsCredentials : IReportServerCredentials
    {
        #region IReportServerCredentials Members
 
        public bool GetFormsCredentials(out Cookie authCookie, out string userName, out string password, out string authority)
        {
            authCookie = new Cookie();
            userName = password = authority = null;
            return false;
        }
 
        public System.Security.Principal.WindowsIdentity ImpersonationUser
        {
            get { return null; }
        }
 
        public ICredentials NetworkCredentials
        {
            get
            {
                if (ConfigurationManager.AppSettings["rptUser"] != null)
                {
                    return new NetworkCredential(
                            ConfigurationManager.AppSettings["rptUser"],
                            ConfigurationManager.AppSettings["rptPassword"]);
                }
                else
                {
                    return CredentialCache.DefaultNetworkCredentials;
                }
            }
          
        }
 
        #endregion
    }
 
And set the report viewer report server to use these credentials.
 
m_rptViewer.ServerReport.ReportServerCredentials = new RsCredentials();
 
In this particular implementation we look for a username and password in the web.config or we use the current Network Credentials of the process.
The current network credentials can be used if you are running if your asp.net worker process is running with account credentials that have access to the reports. Most internet setups do not usually use or allow domain authentication in the web servers so in practice this mechanism is rarely used.