Php prevents webshell processing functions

Source: Internet
Author: User


Import OS
Import sys
Import re
Import time
Def listdir (dirs, liston = '0 '):
Flog = open (OS. getcwd () + "/check_php tutorial _ shell. log", "a + ")
If not OS. path. isdir (dirs ):
Print "directory % s is not exist" % (dirs)
Return
Lists = OS. listdir (dirs)
For list in lists:
Filepath = OS. path. join (dirs, list)
If OS. path. isdir (filepath ):
If liston = '1 ':
Listdir (filepath, '1 ')
Elif OS. path. isfile (filepath ):
Filename = OS. path. basename (filepath)
If re. search (r ".(? : Php | inc | html ?) $ ", Filename, re. ignorecase ):
I = 0
Iname = 0
F = open (filepath)
While f:
File_contents = f. readline ()
If not file_contents:
Break
I + = 1
Match = re. search (r '''(? P <function> B (? : Include | require )(? : _ Once )? B) s *(? S * ["'] (? P <filename> [^;] * (? <!. (? : Php | inc) ["'])? S *; ''', file_contents, re. ignorecase | re. multiline)
If match:
Function = match. group ("function ")
Filename = match. group ("filename ")
If iname = 0:
Info = 'n' [% s]: n' % (filepath)
Else:
Info =''
Info + = 't| -- [% s]-[% s] line [% d] n' % (function, filename, I)
Flog. write (info)
Print info
Iname + = 1
Match = re. search (r' B (? P <function> eval | proc_open | popen | shell_exec | exec | passthru | system | assert | fwrite | create_function) bs * (', file_contents, re. ignorecase | re. multiline)
If match:
Function = match. group ("function ")
If iname = 0:
Info = 'n' [% s]: n' % (filepath)
Else:
Info =''
Info + = 't| -- [% s] line [% d] n' % (function, I)
Flog. write (info)
Print info
Iname + = 1
Match = re. search (R' (^ | (? <=;) S *'(? P <shell> [^ '] +)'s *;', file_contents, re. ignorecase)
If match:
Shell = match. group ("shell ")
If iname = 0:
Info = 'n' [% s]: n' % (filepath)
Else:
Info =''
Info + = 't| -- [''] command is [% s] in line [% d] n' % (shell, I)
Flog. write (info)
Print info
Iname + = 1
Match = re. search (R '(? P <shell >$ _(? : Pos | ge | reques) t) s * [[^] +] s * (', file_contents, re. ignorecase)
If match:
Shell = match. group ("shell ")
If iname = 0:
Info = 'n' [% s]: n' % (filepath)
Else:
Info =''
Info + = 't| -- [''] command is [% s] in line [% d] n' % (shell, I)
Flog. write (info)
Print info
Iname + = 1
F. close ()
Flog. close ()
If '_ main _' = _ name __:
Argvnum = len (sys. argv)
Liston = '0'
If argvnum = 1:
Action = OS. path. basename (sys. argv [0])
Print "command is like: n % s d: wwwroot 1 -- recurse subfolders" % (action, action)
Quit ()
Elif argvnum = 2:
Path = OS. path. realpath (sys. argv [1])
Listdir (path, liston)
Else:
Liston = sys. argv [2]
Path = OS. path. realpath (sys. argv [1])
Listdir (path, liston)
Flog = open (OS. getcwd () + "/check_php_shell.log", "a + ")
Isotimeformat = '% y-% m-% d % x'
Now_time = time. strftime (isotimeformat, time. localtime ())
Flog. write ("n ---------------------- % s checked --------------------- n" % (now_time ))
Flog. close ()

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.