Skip to content

Commit

Permalink
new code examps
Browse files Browse the repository at this point in the history
  • Loading branch information
System Administrator authored and System Administrator committed May 17, 2015
1 parent 77d712f commit fbf285b
Show file tree
Hide file tree
Showing 45 changed files with 74 additions and 82 deletions.
17 changes: 10 additions & 7 deletions skf/markdown/code_examples/asp/1-code_example--File_upload--.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
File upload
-------

**Example:**


using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -23,7 +23,8 @@ File upload
string test = file.FileName;

/*
Here we define a blacklist of different path traversal patterns in order to prevent an attacker to upload files outside of the unintended
Here we define a blacklist of different path traversal patterns in order to prevent an
attacker to upload files outside of the unintended
directory.
*/
string[] evil = new string[] { @"%2e%2e%2f", "../", "%2e", "%5c", "%252e", "%c0%af", "%c1%9c" };
Expand All @@ -37,8 +38,10 @@ File upload

if (match.Success)
{
//this breach has to be repported into the log files
//Log.SetLog(Session['userID'], "Untrusted userinput was detected in the file get contents function in HOME, date, FAIL, HIGH");
/*
this breach has to be repported into the log files
Log.SetLog(Session['userID'], "Untrusted userinput in HOME, date, FAIL, HIGH");
*/

/*
Set counter; if counter hits 3, the user's session must be terminated.
Expand All @@ -55,8 +58,9 @@ File upload

/*
The next step would be checking if the file contains the right extension in order to prevent
a user from uploading files which could be used to harm your system. in this example we check if the last extension
found in the file name is a jpg or a png. whenever an application just regexes for the extension an attacker could
a user from uploading files which could be used to harm your system. in this example
we check if the last extension found in the file name is a jpg or a png. whenever
an application just regexes for the extension an attacker could
bypass the check by uploading an file like: "filename.jpg.php".
*/
string[] StrSpli = test.Split('.');
Expand Down Expand Up @@ -119,7 +123,6 @@ File upload
//If the mimetype is not valid we delete the file from the system.
System.IO.File.Delete(@"C:\Users\Public\xml\"+test+"");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Anti clickjacking headers
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

X-XSS-Protection header
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

X-Content-Type-Options header
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Secure session cookies
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Session cookies HttpOnly
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Identifier-based authorization
-------

Expand Down Expand Up @@ -39,7 +38,8 @@ Identifier-based authorization
Aggregate aggregate = new Aggregate();

//Here we connect to the database by means of a connection string as configured in the web.config
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["users"].ConnectionString);
SqlConnection conn = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["users"].ConnectionString);

//The count integer is set every time the user connects to the databse to process data
public void IdentifierBasedAuthentication(int pageID)
Expand Down Expand Up @@ -80,7 +80,8 @@ Identifier-based authorization
/*
Whenever you are checking whether a user is restricted to review certain data,
the acces restrictions should be proccessed serverside.
The userID could be stored inside a session variable on login, and should be used to retrieve userdata from the database when requested
The userID could be stored inside a session variable on login, and should
be used to retrieve userdata from the database when requested
in order to verify if the user is allowed to look into that data:
*/
string query = string.Format("SELECT * from profile WHERE userID = @userID ");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

SQL query
-------

Expand Down Expand Up @@ -41,7 +40,8 @@ SQL query
//AuditLog Log = new AuditLog();

//Here we connect to the database by means of a connection string as configured in the web.config
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["users"].ConnectionString);
SqlConnection conn = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["users"].ConnectionString);

public void selectStatement()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Crossdomain.xml
-------

Expand All @@ -20,7 +19,8 @@ Crossdomain.xml
Example by twitter's crossdomain.xml:

<?xml version="1.0" encoding="UTF-8"?>
<cross-domain-policy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.adobe.com/xml/schemas/PolicyFile.xsd">
<cross-domain-policy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.adobe.com/xml/schemas/PolicyFile.xsd">
<allow-access-from domain="twitter.com" />
<allow-access-from domain="api.twitter.com" />
<allow-access-from domain="search.twitter.com" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Enforce secure passwords
-------

Expand Down Expand Up @@ -48,9 +47,9 @@ Enforce secure passwords
/*
Also very important is the fact that you have to take into consideration that
Password1! is a valid password according to password standards. This however is not the case since
this password is included in almost every dictionairy attack system. So we have to prevent the user from using these
weak passwords, this we do by defining these bad passwords in a text file and compare the user's password with the
bad passwords defined in the text file.
this password is included in almost every dictionairy attack system. So we have to prevent
the user from using these weak passwords, this we do by defining these bad passwords in a text
file and compare the user's password with the bad passwords defined in the text file.
*/

StreamReader sr = new StreamReader(@"C:\Users\Public\xml\test.txt", true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

CSRF tokens
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Timeout a session
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Disable directory listing
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Charsets
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

HTML output
-------

Expand Down Expand Up @@ -60,7 +59,8 @@ HTML output

inputvalidation validate = new inputvalidation();
string userinput = "when this string is evil the application will block operation!";
if(validate.validateInput(userinput, "nummeric", "Unecpected userinput", "HIGH", 3)== false){ /* Cancel operation of your application */ }
if(validate.validateInput(userinput, "nummeric", "Unecpected userinput", "HIGH", 3)== false)
{ /* Cancel operation of your application */ }


/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Password storage(salting/stretching/hashing)
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Random password/token generation
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Session cookies (domain)
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Content type headers
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Sandboxing
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Audit logs
-------

Expand Down Expand Up @@ -27,7 +26,8 @@ Audit logs
public int blocker { get; set; }

//Here we connect to the database by means of a connection string as configured in the web.config
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["users"].ConnectionString);
SqlConnection conn = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["users"].ConnectionString);

public void SetLog(string session, string message, string state, string threat)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Single input validation controll
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Aggregate user controlls
-------

Expand All @@ -7,10 +6,10 @@ Aggregate user controlls

/*
In order to enforce Aggregate access control protection the best method would be to
define your rules by means of a database structure rather than sessions or log's.
define your rules by means of a database structure rather than sessions or logs.
This is due to the fact that if the user drops his session the rating would start
al over again.

TABLE users
---------------------------------------------------------------------------------
| userID | userName | password | privilegeID | access | AggregateControl |
Expand All @@ -32,8 +31,6 @@ Aggregate user controlls
----------------------------------
| 3 | read |
----------------------------------


*/
using System;
Expand Down Expand Up @@ -96,7 +93,8 @@ Aggregate user controlls

using (SqlCommand command = conn.CreateCommand())
{
//We update the aggregate table in the database in order to keep track of the number of connections the user made
//We update the aggregate table in the database in order to
//keep track of the number of connections the user made
count += controll;
command.CommandText = "UPDATE users SET aggregate = @count WHERE userID = @userID";
Expand All @@ -111,7 +109,8 @@ Aggregate user controlls
Everytime the user accesses the database we keep track of the number of times he
connected. Whenever the user passes a reasonable number he should be rejected
since he could be an attacker scraping your table contents and stealing company information
You could a CRON job or stored procedure in your system in order to clean the Aggregate column within certain timeframes
You could a CRON job or stored procedure in your system in order to
clean the Aggregate column within certain timeframes
*/
HttpContext.Current.Response.Write(controll);
if (controll > 5000)
Expand All @@ -120,14 +119,20 @@ Aggregate user controlls
{

//this breach has to be repported into the log files
Log.SetLog(Session['userID'], "User account was locked out due to aggregate user controll system", date, FAIL, HIGH");
Log.SetLog(Session['userID'], "User account was locked out due to aggregate user control system", date, FAIL, HIGH");

//Whenever te reasonable number of connections the user made was surpassed we destroy all the sessions to deny the user any further access to the system
/*
Whenever te reasonable number of connections the user made was surpassed we destroy all the
sessions to deny the user any further access to the system
*
HttpContext.Current.Session["authenticateUser"] = "";
HttpContext.Current.Session.Abandon();
HttpContext.Current.Response.Redirect("/login", true);

//Than we set his access level on his account to FALSE in order to prevent him from logging in again til you did your forensics on the log files
/*
Than we set his access level on his account to FALSE in order to prevent
him from logging in again til you did your forensics on the log files
*/
string access = "FALSE";
command.CommandText = "UPDATE users SET access = @access WHERE userID = @userID";
command.Parameters.AddWithValue("@access", access);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Session hijacking / Session fixation
-------

Expand Down Expand Up @@ -125,12 +124,14 @@ Session hijacking / Session fixation
AuditLog Log = new AuditLog();

//Here we connect to the database by means of a connection string as configured in the web.config
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["users"].ConnectionString);
SqlConnection conn = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["users"].ConnectionString);

//The count integer is set every time the user connects to the databse to process data
public void checkSession()
{
if ((System.Web.HttpContext.Current.Session["authenticateUser"] != "isLoggedin") || (System.Web.HttpContext.Current.Session["authenticateUser"] == ""))
if ((System.Web.HttpContext.Current.Session["authenticateUser"] != "isLoggedin") ||
(System.Web.HttpContext.Current.Session["authenticateUser"] == ""))
{
HttpContext.Current.Response.Redirect("/login", true);
}
Expand All @@ -151,12 +152,16 @@ Session hijacking / Session fixation
session = oReader["sessiom"].ToString();
ipadress = oReader["ipadress"].ToString();

if ((System.Web.HttpContext.Current.Session["ASPsessionID"].ToString() != session) && (ipadress != HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]))
if ((System.Web.HttpContext.Current.Session["ASPsessionID"].ToString() != session) &&
(ipadress != HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]))
{
//We log the muliple users on the system
Log.SetLog(Session['userID'], "Mulitple users with same session id detected", date, FAIL, MOD");

//We redirect the user to a page which alerts him as well as gives him the option to destroy the mulitple sessions if he does not trust them
/*
We redirect the user to a page which alerts him as well as gives him the option to destroy the
mulitple sessions if he does not trust them
*/
HttpContext.Current.Response.Redirect("/Home/multipleUsers", true);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Open forwards & redirects
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

XML injection prevention
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Path traversal
-------

Expand All @@ -11,12 +10,10 @@ Path traversal
using System.Text.RegularExpressions;
using System.IO;


namespace MvcApplication1.Controllers
{
public class rewrite
{
//include classes
auditLogs Log = new auditLogs();
inputvalidation validate = new inputvalidation();
whitelist listme = new whitelist();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Enforce sequential step order (Wizzard)
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

White-listing
White-listing
-------

**Example:**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Encoding
-------

Expand Down

0 comments on commit fbf285b

Please sign in to comment.