Detailed description of ASP.net server intrusion methods

Source: Internet
Author: User

A friend asked me about the ASP.net server intrusion method on QQ. I will tell you some common intrusion methods on ASP.net.

1. commonly used standard component in ASP: FileSystemObject, which provides powerful file system access capabilities for ASP, you can read, write, delete, and rename any directories and files on the server's hard disk. The FSO object comes from the script running library scrrun. dll provided by Microsoft. Use the following code to create a FSO object in ASP: Set fso = CreateObject ("Scripting. fileSystemObject ") we use the attributes and methods contained in the fso object, such as Drive, Drives, Folder, Floders, File, Files, and other operations on the server disk, directory, and File. This powerful file system access capability brings serious security problems to ASP shared space providers, many ASP space administrators delete this component or rename it to prevent users from using this standard component. Deleting a component or renaming a component is indeed a simple and effective method, but it does not allow users to use its powerful functions. There is also a beautiful solution on the network, which allows users to use the FileSystemObject component without affecting the security of the server, that is, each user is set to an independent server user and a single directory operation permission. However, this method is problematic. Because ASP and ASP. NET have similar problems in this regard, we will add details in the corresponding solutions section of ASP. NET. In ASP. NET, we found that this problem still exists and becomes more difficult to solve. This is because. NET, and ASP. NET has a new function, this component does not need to use regsvr32 for registration as ASP does, you just need to upload the Dll class library file to the bin directory to use it directly. This function is designed for ASP development. NET brings a lot of convenience, but it makes the solution that we delete or rename this dll in ASP useless, so it becomes more complicated to prevent this problem. Before discussing the solution, let's take a look at how to implement the above dangerous functions.

2. File System

The other classes that use the System. IO namespace are:

System. IO. Directory: provides classes for creating, moving, and enumerating static methods through directories and subdirectories

System. IO. File: provides a class for creating, copying, deleting, moving, and opening files.

System. IO. FileInfo: class that provides instance methods for creating, copying, deleting, moving, and opening files

System. IO. StreamReader: implements a TextReader to read characters from the byte stream with a specific encoding.

The specific usage of the attributes and methods of each class we use will be described in the program as code annotations.

The System. IO namespace is in mscorlib. dll provided by. net framework. You need to reference this Dll to this handler before using VS. Net programming.

All programs we write use the Codebehind method, that is, each aspx program has a corresponding aspx. in the cs program and aspx program, only Code related to page display is written, and all logic implementation code is placed in the corresponding aspx. in the cs file, the display and logic can be better separated. Since our purpose is not to discuss Codebehind technology, we will not discuss it more.

In this article, we will only introduce the usage of several main classes and their key methods. For details about the program, please refer to the included source code.

Program 1: listdrivers. aspx program that displays the current information of the server and the names of all logical drives

Main Method 1: We use the GetSysInf () method to obtain information about the current environment and platform of the server.

// Method for obtaining system information, which is in the listdrivers. aspx. cs File

Public void GetSysInf (){

// Obtain the operating system type

QDrives = Environment. OSVersion. ToString ();

// Obtain the system folder

QSystemDir = Environment. SystemDirectory. ToString ();

/* Obtain the physical memory volume mapped to the process context. You can learn about ASP through this memory ING volume. the number of system physical memory required by the. NET Program during operation helps to better plan our entire application. Because the physical memory volume is measured in bytes, we divide this value by 1024, you can obtain the physical memory size in KB */

QMo = (Environment. WorkingSet/1024). ToString ();

// Obtain the fully qualified path of the current directory (that is, the directory from which the process starts)

QCurDir = Environment. CurrentDirectory. ToString ();

// Obtain the network of the host

Domain Name

QDomName = Environment. UserDomainName. ToString ();

// Obtain the number of milliseconds after the system is started

QTick = Environment. TickCount;

// Calculate the number of minutes after the system is started

QTick/= 60000;

// Obtain the machine name

QMachine = Environment. MachineName;

// Obtain the user name for running the current process

QUser = Environment. UserName;

/* Retrieve the name of the logical drive in the format of "<drive letter>: \" on this computer and return a string array, which is the key to the next step */

AchDrives = Directory. GetLogicalDrives ();

// Obtain the dimension of the string array and determine the number of logical drives.

NNumOfDrives = achDrives. Length;

} The system information does not need to be operated. We simply use asp: Label to display them. The number of logical drives varies on different servers. Therefore, the name of the logical drive is stored in an indefinite array, and the name of the logical drive is also the basis for browsing directories and files, therefore, we use the data grid DataGrid to display and process it. Code for displaying and processing the DataGrid of the logical drive name (the code is in the listdrivers. aspx file ):

<Asp: DataGrid id = "DriversGrid" runat = "server" AutoGenerateColumns = "false">

<Columns>

<Asp: BoundColumn HeaderText = "ID" DataField = "ID"/>

<Asp: BoundColumn HeaderText = "disk name" DataField = "Drivers"/>

<Asp: HyperLinkColumn

HeaderText = "details"

DataNavigateUrlField = "Drivers" DataNavigateUrlFormatString = "listdir. aspx? Dir = {0 }"

DataTextField = "Detail"

Target = "_ new"/>

</Columns>

</Asp: DataGrid> the first two BoundColumn columns show the sequence number and actual logical drive name. The third column must be noted, before entering the display directory and file of each logical drive, we need to pass the name of the selected logical drive to the file of the display directory. Therefore, a special hyperlink line HyperLinkColumn is required, we set DataNavigateUrlField to the URL field of the hyperlink to be bound to the HyperLinkColumn in the data source, where it is the name of the logical drive. Then, set DataNavigateUrlFormatString to the display format of the hyperlink URL in the HyperLinkColumn when the URL data is bound to a field in the data source, that is, the next level of processing page to be linked. Here it is listdir. aspx? Dir = {name of the logical drive you clicked on}

1. commonly used standard component in ASP: FileSystemObject, which provides powerful file system access capabilities for ASP, you can read, write, delete, and rename any directories and files on the server's hard disk. The FSO object comes from the script running library scrrun. dll provided by Microsoft. Use the following code to create a FSO object in ASP: Set fso = CreateObject ("Scripting. fileSystemObject ") we use the attributes and methods contained in the fso object, such as Drive, Drives, Folder, Floders, File, Files, and other operations on the server disk, directory, and File. This powerful file system access capability brings serious security problems to ASP shared space providers, many ASP space administrators delete this component or rename it to prevent users from using this standard component. Deleting a component or renaming a component is indeed a simple and effective method, but it does not allow users to use its powerful functions. There is also a beautiful solution on the network, which allows users to use the FileSystemObject component without affecting the security of the server, that is, each user is set to an independent server user and a single directory operation permission. However, this method is problematic. Because ASP and ASP. NET have similar problems in this regard, we will add details in the corresponding solutions section of ASP. NET. In ASP. NET, we found that this problem still exists and becomes more difficult to solve. This is because. NET, and ASP. NET has a new function, this component does not need to use regsvr32 for registration as ASP does, you just need to upload the Dll class library file to the bin directory to use it directly. This function is designed for ASP development. NET brings a lot of convenience, but it makes the solution that we delete or rename this dll in ASP useless, so it becomes more complicated to prevent this problem. Before discussing the solution, let's take a look at how to implement the above dangerous functions.

2. File System

The other classes that use the System. IO namespace are:

System. IO. Directory: provides classes for creating, moving, and enumerating static methods through directories and subdirectories

System. IO. File: provides a class for creating, copying, deleting, moving, and opening files.

System. IO. FileInfo: class that provides instance methods for creating, copying, deleting, moving, and opening files

System. IO. StreamReader: implements a TextReader to read characters from the byte stream with a specific encoding.

The specific usage of the attributes and methods of each class we use will be described in the program as code annotations.

The System. IO namespace is in mscorlib. dll provided by. net framework. You need to reference this Dll to this handler before using VS. Net programming.

All programs we write use the Codebehind method, that is, each aspx program has a corresponding aspx. in the cs program and aspx program, only Code related to page display is written, and all logic implementation code is placed in the corresponding aspx. in the cs file, the display and logic can be better separated. Since our purpose is not to discuss Codebehind technology, we will not discuss it more.

In this article, we will only introduce the usage of several main classes and their key methods. For details about the program, please refer to the included source code.

Program 1: listdrivers. aspx program that displays the current information of the server and the names of all logical drives

Main Method 1: We use the GetSysInf () method to obtain information about the current environment and platform of the server.

// Method for obtaining system information, which is in the listdrivers. aspx. cs File

Public void GetSysInf (){

// Obtain the operating system type

QDrives = Environment. OSVersion. ToString ();

// Obtain the system folder

QSystemDir = Environment. SystemDirectory. ToString ();

/* Obtain the physical memory volume mapped to the process context. You can learn about ASP through this memory ING volume. the number of system physical memory required by the. NET Program during operation helps to better plan our entire application. Because the physical memory volume is measured in bytes, we divide this value by 1024, you can obtain the physical memory size in KB */

QMo = (Environment. WorkingSet/1024). ToString ();

// Obtain the fully qualified path of the current directory (that is, the directory from which the process starts)

QCurDir = Environment. CurrentDirectory. ToString ();

// Obtain the network of the host

Tail"

HeaderText = "details"

Target = "_ new"

/>

In the two DataGrid (DirGrid, FileGrid), we set two HyperLinkColumn columns to navigate to different processing pages.

We use a delete button column in both the DataGrid:

<Asp: ButtonColumn HeaderText = "delete"

Text = "delete"

CommandName = "Delete"

/>

The columns added, updated, and deleted are the default template columns of the DataGrid. Therefore, you can use the DataGrid property generator to automatically add this column in Vs.net.

Get the code of the parameter passed on the previous page:

Because the parameters passed on the previous page need to be used to determine the Directory and file name in the method for generating data sources below, the following code is used in the Page_Load method of the page:

StrDir2List = Request. QueryString ["dir"];

The strDir2List string is the name or file name of the uploaded directory.

Because we use two DateGrid, We need to bind the data twice. There are two different data source generation methods.

How to generate a directory data grid (DirGrid) Data source:

// This method returns a data view DataView in the form of a set to initialize the DataGrid of the subdirectory.

ICollection createperformancedir (){

DtDir = new DataTable ();

DataRow dr;

// Add a new data column to the DataTable, with four columns in total

DtDir. Columns. Add (new DataColumn ("DirID", typeof (Int32 )));

DtDir. Columns. Add (new DataColumn ("DirName", typeof (string )));

DtDir. Columns. Add (new DataColumn ("DelDir", typeof (string )));

DtDir. Columns. Add (new DataColumn ("DirDetail", typeof (string )));

// Obtain the string array of all sub-directory names in the directory based on the input parameters (directory name)

String [] distriries = Directory. GetDirectories (strDir2List );

// Use the foreach loop to traverse arrays of unknown length

Foreach (string DirName in dimo-ries ){

Dr = dtDir. NewRow ();

Dr [0] = I; // serial number

Dr [1] = DirName; // folder name

Dr [3] = "delete ";

Dr [3] = "View Details ";

DtDir. Rows. Add (dr );

I ++;

}

DataView dvDir = new DataView (dtDir );

// Return the Data View

Return dvDir;

}

How to generate a FileGrid Data source:

// This method returns a data view in the form of a set, DataView, used to initialize the object's DataGrid

ICollection createperformancefile (){

DtFile = new DataTable ();

DataRow dr;

DtFile. Columns. Add (new DataColumn ("FileID", typeof (Int32 )));

DtFile. Columns. Add (new DataColumn ("FileName", typeof (string )));

DtFile. Columns. Add (new DataColumn ("DelFile", typeof (string )));

DtFile. Columns. Add (new DataColumn ("FileDetail", typeof (string )));

// Obtain the string array of all file names in the directory based on the input parameters (directory name)

String [] FileEntries = Directory. GetFiles (strDir2List );

Foreach (string FileName in FileEntries ){

Dr = dtFile. NewRow ();

Dr [0] = I;

Dr [1] = FileName;

Dr [2] = "delete ";

Dr [3] = "View Details ";

DtFile. Rows. Add (dr );

I ++;

}

DvFile = new DataView (dtFile );

Return dvFile;

}

We programmed the two DataSource to bind the data of the two DataGrid in the Page_Load method of the page to display the data in the DataTable on the DataGrid of the aspx page.

Data Binding code: // defines and binds the data source of the sub-directory data list DirGrid.

DirGrid. DataSource = createperformancedir ();

DirGrid. DataBind ();

// Define the data source and bind the file data list FileGrid

FileGrid. DataSource = createperformancefile ();

FileGrid. DataBind ();

Through the main method described above, we have displayed a list Of all subdirectories and files in a logical drive or directory, in addition, you can further browse sub-directories or view the attributes and contents of files based on the displayed results. The browsing subdirectory is still through the listdir. aspx program. There is no sub-directory level requirement and there is no directory depth limit.

Main methods and code for deleting subdirectories and files:

When deleting a subdirectory, we need to use the Directory. Delete (string, bool) method. There are two methods:

1. public static void Delete (string );

Deletes an empty directory from a specified path.

2. public static void Delete (string, boolean );

Delete the specified directory and (if indicated) delete any sub-directories in the directory. If boolean is set to true, delete all sub-directories and files in the directory, otherwise, set boolean to false.

Here we use the second method. If you select Delete, all subdirectories and files under this directory will be deleted.

Note: all methods of the Directory class are static and can be called without the need for an instance with Directory.

/* Delete sub-directories. This method is. NET automatically added. Note that the DataGridCommandEventArgs e is the ButtonColumn event of CommandName = "Delete" in DirGrid. With this event, we can see that the ButtonColumn button column of the row is clicked, then determine the name of the subdirectory to be deleted */

Private void DirGrid_DeleteCommand (object source, System. Web. UI. WebControls. DataGridCommandEventArgs e ){

/* Define a cell, e. all items in the row where this event occurs, e. item. cells [1] is the content of the second cell of the entire row. In this DataGrid, It is the name of the subdirectory.

*/

TableCell ItemCell = e. Item. Cells [1];

// Obtain the name string of this subdirectory

String item = ItemCell. Text;

// Delete this subdirectory

Directory. Delete (item, true );

// Bind data after deletion to update the data list

DirGrid. DataBind ();

}

When deleting an object, we need to use File. Delete (string path );

Note: all methods of the File class are static and can be called without a directory instance.

Private void FileGrid_DeleteCommand (object source,

System. Web. UI. WebControls. DataGridCommandEventArgs e ){

TableCell ItemCell = e. Item. Cells [1];

// Obtain the string of this file name

String item = ItemCell. Text;

// Delete this file

File. Delete (item );

// Bind data after deletion to update the data list

DirGrid. DataBind ();

}

Through the above main method, we implement the function of deleting a sub-directory or file on the page. This function must be used with caution during testing. Once deleted, it cannot be restored through the conventional method. Other methods, such as directory or file renaming and content modification, can add corresponding functions on the basis of this program, and the implementation method is also very simple. You can add related functions to expand them into a Web-based Server File Management System. We can also see the danger of this program. A file system of a server that does not take preventive measures for this security risk is exposed to users who use this program.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.