↓ Skip to main content

C#

Unit Testing a Private Method

·1 min
With Visual Studio keep changing the way you create unit tests I thought I’d document down how to test a private method the easy way. Here is a simple HomeController and we want to test that the GetPageSize method return 20 in a unit test.

URL Activity Tampering and how to prevent it in MVC

·13 mins
No web applications are developed without concerning the security issues. But it is not sure whether the security measures are implemented correctly and the applications are well protected. We could see a lot of web applications query string parameters in plain string which are easy to manipulate and way to escalate restricted resources. Even the URL parameters are encrypted there is still a chance of a bruteforce attack by manipulating the encrypted hash and it yields when the database is large where the chance of matching a random hash is high. So it is clear the importance to protect the URL in the web application is necessary for all programming platforms. Here, this is discussed with reference to ASP.NET MVC and the utility class is presented.

Clean Content to help searching

·3 mins
One of the issue we all faced with is when a user searches for content, we are allowing the end user to be able to search for anything and this could result in unnecessary searching or cause the backend to perform searches which could result in DOS attacks via SQL.

URL Validator

·1 min
I needed to check to see if a URL that the user was entering was a valid Url, does it contain HTTP:// or HTTPS:// I ended up writing a small ValidationAttribute to add to the collection I am now building up, here is the code:

Removing and Cleaning search content to prevent DoS attacks

·2 mins
There are many ways to search a system, what we have to be careful of is making sure that the search does not cause a DoS (Denial of Service) attack. So following on from my article Denial Of Service (DoS) attacks via SQL Wildcards should be prevented here is a method to remove and clean up the search to prevent such an attack.

MVC way to check the File Extension

·4 mins
Following on from my article about checking for the file signature to ensure that the file is who it says it is I decide to take it further and produce a Data Validation attribute, I found that the Microsoft.Web.Mvc.dll library already has one ,but it only checks for the file names, which is generally okay.

File Upload does not check content

·1 min
Is it possible to upload files of any type to the server through web application’s functionality? Only file extension restrictions were could be enforced and the server may not perform any content checking (apart from possible anti-virus checks).

SSL Vulnerable to BEAST attack

·3 mins
A vulnerability exists in SSL 3.0 and TLS 1.0 that could allow information disclosure if an attacker intercepts encrypted traffic served from an affected system. The weakness is down to insufficiently randomised data being used for the Initialisation Vectors (IV) within the CBC-mode encryption algorithms. The exploit can be performed through multiple injection points, both native to the browser’s functionality.

Verbose Error Messages

·3 mins
The software generates an error message that includes sensitive information about its environment, users, or associated data. Extended Description The sensitive information may be valuable information on its own (such as a password), or it may be useful for launching other, more deadly attacks. If an attack fails, an attacker may use error information provided by the server to launch another more focused attack. For example, an attempt to exploit a path traversal weakness might yield the full pathname of the installed application. In turn, this could be used to select the proper number of “..” sequences to navigate to the targeted file. An attack using SQL injection might not initially succeed, but an error message could reveal the malformed query, which would expose query logic and possibly even passwords or other sensitive information used within the query.

Environment issue – Untrusted Certificate

·1 min
Certificate is singed by an unrecognised certificate authority. If a browser receives a self-signed certificate, it pops up a warning, and the burden falls to the user to confirm the identity. Pushing this decision to the user is ultimately what opens up the possibility of a man-in-the-middle (MITM) attack. The security issue is not with self-signed certificates, but with the way users interact with them in the browser.

Denial Of Service (DoS) attacks via SQL Wildcards should be prevented

·3 mins
SQL Wildcard attacks force the underlying database to carry out CPU-intensive queries by using several wildcards. This vulnerability generally exists in search functionalities of web applications. Successful exploitation of this attack will cause Denial of Service (DoS).

HTTP Header Disclosure

·2 mins
Vulnerability overview/description Due to unsanitized user input it is possible to inject arbitrary HTTP header values in certain HTTP responses of the Satellite Server. This can be exploited, for example, to perform session fixation and malicious redirection attacks via the Set-Cookie and the Refresh headers. Moreover, the Satellite Server caches these HTTP responses with the injected HTTP header resulting in all further requests to the same resource being served with the poisoned HTTP response, while these objects remain in cache.

PenTest where to start

·2 mins
I would like to secure up an MVC application, and one way of insuring a secure application is to run through pentesting, Penetration Testing, but what is pentesting? Wikipedia A penetration test, occasionally pentest, is a method of evaluating computer and network security by simulating an attack on a computer system or network from external and internal threats. The process involves an active analysis of the system for any potential vulnerabilities that could result from poor or improper system configuration, both known and unknown hardware or software flaws, or operational weaknesses in process or technical countermeasures. This analysis is carried out from the position of a potential attacker and can involve active exploitation of security vulnerabilities.

Elmah or Microsoft Enterprise Library for Exception logging

·4 mins
 Enterprise Library Logging Application Block The Enterprise Library Logging Application Block simplifies the implementation of common logging functions. You can use the Logging Application Block to write information to a variety of locations:

Singleton Pattern the right way

·1 min
The singleton pattern is used in almost all modern day programming languages, so why do I keep finding it written incorrectly in so many applications, so lets start with the right way public sealed class SimpleNoLockLazy { static readonly SimpleNoLockLazy instance = new SimpleNoLockLazy(); // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static SimpleNoLockLazy() { } SimpleNoLockLazy() { } public static SimpleNoLockLazy Instance { get { return instance; } } }</pre> So why this implementation? The reason for using a Singleton is for performance in a multi-threaded environment and this pattern is not only simple, but it is the fastest. I have attached a benchmark application, which is based on Jon Skeet’s benchmark, but testing using a Parallel processes.

Encrypting Web.config for the website

·1 min
Security is always high for installation to customer so is it not about time you encripted the connection string in your web.config file? This is how it looks like before encrypting: <connectionStrings> <add name="Pubs" connectionString="Server=localhost;Integrated Security=True;Database=Pubs" providerName="System.Data.SqlClient" /> <add name="Northwind" connectionString="Server=localhost;Integrated Security=True;Database=Northwind" providerName="System.Data.SqlClient" /> </connectionStrings> We can encrypt any section of your Web.config file on-the-fly and programatically. If you have full access to your Web server, you can encrypt your connection strings with this single command-line located in the in the %windows%\Microsoft.NET\Framework\versionNumber folder:

Don’t and Do’s in C#

·5 mins
For a while now I have been reviewing developers code and performing code reviews and I have noticed some common mistakes are being repeated time and time again. These are mostly the mistakes, which once you point them, are quite easy to remember. However, if a developer is not aware of them, they can cause many problems with the efficiency and quality of the developed software. So what are these common mistakes and why are they wrong?