This question is similar to Exploitable PHP Functions.
Tainted data comes from the user, or more specifically an attacker. When a tainted variable reaches a sink function, then you have a vulnerability. For instance a function that executes a sql query is a sink, and GET/POST variables are sources of taint.
What are all of the sink functions in C#? I am looking for functions that introduce a vulnerability or software weakness. I am particularly interested in Remote Code Execution vulnerabilities. Are there whole classes/libraries that contain nasty functionally that a hacker would like to influence? How do people accidentally make dangerous C# code?
IMO: The nr 1 exploitable functions, are innocent looking, but very dangerously when used without thought.
In ASP.Net
Response.Write
or the shortcut:In ADO.Net:
string
+
operator:var sql = "SELECT * FROM table WHERE name = '" + searchTermFromUser + "'"
Any piece of data you get from the user (or any other external source) and pass to another system or another user is a potential exploit.
If you get a string from the user and display it to another user without using HtmlEncode it's a potential exploit.
If you get a string from the user and use it to construct SQL it's a potential SQL injection.
If you get a string from the user and use it to contract a file name for Process.Start or Assembly.Load it's a remote execution vulnerability
You get the point, the danger comes from using unsanitized data, if you never pass user input to external system without sanitizing it (example: HtmlEncode) or using injection-safe interfaces (example: SQL parameters) you are relatively safe - the minute you forget to sanitize something the most innocent-looking method can become a security vulnerability.
Note: cookies, html headers and anything else that passes over a network is also data from the user, in most cases even data in your database is data from the user.
Aside from the obvious
Process.Start()
already mentioned, I can see a couple of ways of potential indirect exploitation.CreateProcess()
and whatnot.Assembly.Load()
and other such overloads. If a compromised assembly made it to the system and loaded.That's all that comes to mind right now.
Anything that uses regular expressions (particularly the RegularExpressionValidator). To see this, run a RegularExpressionValidator with the regex
^(\d+)+$
and give it 30 digits and an alpha character to validate against.Some posts:
This is called a Regular Expression Denial of Service attack and it can bring a website to its knees.
On the web based side of things, C# (and more generally, ASP.NET) is commonly vulnerable to the following (items listed by OWASP Top 10 2013). I realise you were mainly interested in sink functions, of which I cover some, however you did ask how people accidentally make dangerous C# code so hopefully I've provided some insight here.
A1-Injection
SQL Injection
Generating queries by string concatenation.
This can often be solved by parameterised queries, but if you are using an
IN
condition it currently isn't possible without string concatenation.LDAP Injection
Code such as
can make the application vulnerable. More information here.
OS Command Injection
This code is vulnerable to command injection because the second parameter to
Process.Start
can have extra commands passed to it using the&
character to batch multiple commandse.g.
foldername && ipconfig
A2-Broken Authentication and Session Management
Sign Out
The default Forms Authentication SignOut method does not update anything server side, allowing a captured auth token to be continued to be used.
Using Session State for Authentication
A session fixation vulnerability could be present if a user has used session state for authentication.
A3-Cross-Site Scripting (XSS)
Response.Write
(and the shortcut<%= =>
) are vulnerable by default, unless the developer has remembered to HTML encode the output. The more recent shortcut<%:
HTML encodes by default, although some developers may use this to insert values into JavaScript where they can still be escaped by an attacker. Even using the modern Razor engine it is difficult to get this right:ASP.NET by default enables Request Validation, which will block any input from cookies, the query string and from POST data that could potentially be malicious (e.g. HTML tags). This appears to cope well with input coming through the particular app, but if there is content in the database that is inserted from other sources like from an app written using other technologies, then it is possible that malicious script code could still be output. Another weakness is where data is inserted within an attribute value. e.g.
This can be exploited without triggering Request Validation:
If
alt
isthen this renders
In old versions of .NET it was a bit of a mine-field for a developer to ensure that their output was correctly encoded using some of the default web controls.
e.g. not vulnerable:
vulnerable:
A4-Insecure Direct Object References
MVC model binding can allow parameters added to POST data to be mapped onto the a data model. This can happen unintentionally as the developer hasn't realised that a malicious user may amend parameters in this way. The
Bind
attribute can be used to prevent this.A5-Security Misconfiguration
There are many configuration options that can weaken the security of an application. For example setting
customErrors
toOn
or enabling trace.Scanners such as ASafaWeb can check for this common misconfigurations.
A6-Sensitive Data Exposure
Default Hashing
The default password hashing methods in ASP.NET are sometimes not the best.
A7-Missing Function Level Access Control
Failure to Restrict URL Access
In integrated pipeline mode .NET can see every request and handles can authorise each request, even to non .NET resources (e.g.
.js
and images). However, if the application i running in classic mode, .NET only sees requests to files such as.aspx
so other files may be accidentally unsecured. See this answer for more detail on the differences.e.g.
www.example.com/images/private_photograph_user1.jpg
is more likely to be vulnerable in an application that runs in classic mode, although there are workarounds.A8-Cross-Site Request Forgery (CSRF)
Although the legacy web forms applications are usually more secure against CSRF due to requiring the attacker to forge the View State and Event Validation values, newer MVC applications could be vulnerable unless the developer has manually implemented anti forgery tokens. Note I am not saying that web forms is not vulnerable, just that it is more difficult that simply passing on a few basic parameters - there are fixes though, such as integrating the user key into the View State value.
A10-Unvalidated - Redirects and Forwards
Adding code such as
will make your site vulnerable. The attack could be initiated by sending a phishing email to a user containing a link. If the user is vigilant they may have double checked the domain of the URL before clicking. However, as the domain will match your domain which the user trusts, they will click the link unaware that the page will redirect the user to the attacker's domain.
Validation should take place on
Url
to ensure that it is either a relative, allowed URL or an absolute URL to one of your own allowed domains and pages. You may want to check someone isn't redirecting your users to/Logout.aspx
for example. Although there may be nothing stopping an attacker from directly linking tohttp://www.example.com/Logout.aspx
, they could use the redirect to hide the URL so it is harder for a user to understand which page is being accessed (http://www.example.com/Redirect.aspx?Url=%2f%4c%6f%67%6f%75%74%2e%61%73%70%78
).Others
The other OWASP categories are:
of which I can't think of any to mind that are specific to C#/ASP.NET. I'll update my answer if I think of any (if you think they are relevant to your question).
Process.Start
is the first one to come to mind.I am sure that
WindowsIdentity
and much ofSystem.Security
can also be used for evil.Of course, there are SQL injection attacks, but I don't think that's what you mean (though remote execution can occur through SQL Server).