19.09.2017 Views

the-web-application-hackers-handbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

720 Chapter 19 n Finding Vulnerabilities in Source Code<br />

as a mapping from string names to object values, which can be accessed using<br />

<strong>the</strong> APIs listed in Table 19-5.<br />

Table 19-5: APIs Used to Interact with <strong>the</strong> User’s Session on <strong>the</strong> ASP.NET Platform<br />

API<br />

Add<br />

Item<br />

Keys<br />

DESCRIPTION<br />

Adds a new item to <strong>the</strong> session collection.<br />

Gets or sets <strong>the</strong> value of a named item in <strong>the</strong> collection.<br />

Return <strong>the</strong> names of all items in <strong>the</strong> collection.<br />

GetEnumerator<br />

CopyTo<br />

Copies <strong>the</strong> collection of values to an array.<br />

Potentially Dangerous APIs<br />

This section describes some common ASP.NET APIs that can introduce security<br />

vulnerabilities if used in an unsafe manner.<br />

File Access<br />

System.IO.File is <strong>the</strong> main class used to access files in ASP.NET. All of its<br />

relevant methods are static, and it has no public constructor.<br />

The 37 methods of this class all take a filename as a parameter. Path traversal<br />

vulnerabilities may exist in every instance where user-controllable data is passed<br />

in without checking for dot-dot-slash sequences. For example, <strong>the</strong> following<br />

code opens a file in <strong>the</strong> root of <strong>the</strong> C:\ drive on Windows:<br />

string userinput = “..\\boot.ini”;<br />

FileStream fs = File.Open(“C:\\temp\\” + userinput,<br />

FileMode.OpenOrCreate);<br />

The following classes are most commonly used to read and write file<br />

contents:<br />

n System.IO.FileStream<br />

n System.IO.StreamReader<br />

n System.IO.StreamWriter<br />

They have various constructors that take a file path as a parameter. These<br />

may introduce path traversal vulnerabilities if user-controllable data is passed.<br />

For example:<br />

string userinput = “..\\foo.txt”;<br />

FileStream fs = new FileStream(“F:\\tmp\\” + userinput,<br />

FileMode.OpenOrCreate);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!