Knowledge learned when reading Sqlmap source code-check the operating environment

Source: Internet
Author: User

Recently read the source of Sqlmap, Mengmengdongdong in the page about learning some knowledge (say to yourself to listen to: This shows that the so-called can solve all the problems encountered in the Python level, can only show that you meet the simple needs of .... ), the usual, write here, a memo, and two consolidate

First of all, Sqlmap first step

1 def Main (): 2     """ 3     Main function of Sqlmap when running by command line. 4     """ 5 6     Try : 7         checkenvironment ()8        setpaths (Modulepath ())9         Banner ()

The first step is to check the environment, enter the checkenvironment view, find

1 defcheckenvironment ():2     Try:3 Os.path.isdir (Modulepath ())4     exceptUnicodeencodeerror:5ErrMsg ="your system does not properly handle non-ascii paths."6ErrMsg + ="Please move the Sqlmap ' s directory"7 logger.critical (errmsg)8         RaiseSystemexit

This place has a modulepath () aroused my interest, because the point in to see what he is a what, unexpectedly did not see understand ...

1 defModulepath ():2     """3 This would get us the program's directory, even if we are frozen4 using Py2exe5     """6 7     Try: One_ = sys.executableifWearefrozen ()Else __file__ A     exceptNameerror: -_ =inspect.getsourcefile (Modulepath) -  the     returnGetunicode (Os.path.dirname (Os.path.realpath (_)), -Encoding=sys.getfilesystemencoding ()orunicode_encoding)
  _ = sys.executable   If Wearefrozen ()  else  __file__  

This sentence logically parses, ' _ ' this variable, If Wearefrozen () ==true, then it is equal to sys.executable,sys.executable point in view, it is Python.exe location, but from the code, this section of the
should be obtained Sqlmap directory, Instead of being the interpreter directory, then else gets the __file__, which all knows, gets the execution directory, relative to the path execution gets the relative directory, absolute path execution gets the absolute directory, think about it, can you say that
Wearefrozen never be true?

Let's go in and see what this wearefrozen is.
 1  def   Wearefrozen ():  2   3   Returns whether we are frozen via Py2exe.  4   This would affect how we find out where we are locat Ed.  5   reference:http://www.py2exe.org/index.cgi/ Whereami  6   7  8  return  hasattr (SYS, Span style= "COLOR: #800000" > " frozen  " )

The knowledge is limited, do not see what this means ... Then go to the comment inside the URL and look at it. is Http://www.py2exe.org/index.cgi/WhereAmI this. English proficiency is limited, can only read a general meaning, about to say

If SYS contains the frozen attribute, it proves that you are running a file that is packaged as an EXE, and if not, you execute script. Oh? Is that right? Write a script to verify the following:

1 #-*-coding:utf-8-*-2 # ====================================================3 #@ Creator:Hainan.Zhang4 #@ date:2017-7-215 #Test6 # ====================================================7 ImportSYS8 Printsys.executable9 PrintHasattr (SYS,"Frozen")

We use Pyinstaller to package this. py file as exe

Then this line, Pyinstaller run at the command line, will be in the command to add new build and Dist folder, to go dist inside to find this test.exe.

Indeed, the original package into EXE files, then sys.executable will be shown as the execution exe path, SYS will be more than the frozen property. We then look at Modulepath, and once the nameerrorappears, then the except statement

, _ is assigned to Inspect.getsourcefile (Modulepath), the action point of Getsourcefile to see the note is very obvious, but also to return this code in the road.

At the end of the Modulepath, the return statement calls its own getunicode function, converting the obtained path strength to the desired encoding encoding=sys.getfilesystemencoding () or unicode_encoding

If the file specifies the encoding format, then the encoding format for the specified format, if not, converted to Unicode, there is a book called What to improve the Python program 59 methods, it is written that we want to implement the code conversion function, has solved the coding problem in our project, This getunicode can be used as an example. Prevent the influence of Chinese, Japanese and other road strength.

Sqlmap is worthy of God-level tools, the source of this small section, let me harvest a lot, insist on reading. I will share it with you when I have the feeling.

Knowledge learned when reading Sqlmap source code-check the operating environment

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.