九零登录

[【通过】] 【已转正】python脚本:扫描jsp类的webshell及其改良

[复制链接]
发表于 2016-7-24 14:24:44 | 显示全部楼层 |阅读模式
前言:楼主是在甲方工作的一名苦逼工程师
前几天有前线的小伙伴说不能在客户(证券类的)环境里装检查工具,让我给写个小脚本跑跑jsp马之类的,想了想可以实现,于是就写了个脚本:
[Python] 纯文本查看 复制代码
#-*- encoding:UTF-8 -*-
import re
import time
from optparse import OptionParser
import  os
from itertools import islice

 
p=re.compile("BASE64Decoder\(\).decodeBuffer\(|void BB|Socket\(\)|request.getInputStream\(\)|jspspy|KJ021320|PW_SESSION_ATTRIBUTE|getRuntime\(\).exec\(.*\)|Runtime.getRuntime\(\)|FileOutputStream\(application|getRealPath\(request.getServletPath\(",re.I)
#p=re.compile("Pwd\s{0,3}\=.{1,10}|BASE64Decoder|JFolder|getInputStream\(|getOutputStream\(|jspspy|KJ021320|PW_SESSION_ATTRIBUTE|getRuntime\(.*\)|FileOutputStream\(.*\)|FileInputStream\(.*\)|getRealPath\(.*\)",re.I)
file_log=os.getcwd()+'/'+time.strftime('%Y%m%d%H%M%S')+'.log'

#文件切片处理
def filelice(filespath):
          with open(filespath) as f:
             while True:
                 next_n_lines = "".join(list(islice(f,20)))
                 if p.findall(next_n_lines):
                     LastCreatedTime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getctime(filespath)))
                     LastModifiedTime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(filespath)))
                     filelog='FilePath:%s\nBadCode:%s\nLast Created Time:%s\nLast Modified Time:%s\n' % (filespath,p.findall(next_n_lines)[0],LastCreatedTime, LastModifiedTime)
                     print filelog+'\n'
                     fo=open(file_log,'a+')
                     fo.write(filelog+'\n')
                     fo.close()
                     break
                 elif not next_n_lines:
                     break



#扫描文件
def scan(file,include_ext):
 global  file_log
 for root, dirs, files in os.walk(file):
     for filespath in files:
         if include_ext=='all':
             filelice(os.path.join(root,filespath))
         elif os.path.splitext(os.path.join(root,filespath))[1][1:].lower() in include_ext:
             filelice(os.path.join(root,filespath))


 
#主函数
def main():
    parser = OptionParser()
    parser.add_option("-p", "--path", action="store",
                           dest="path",
                                           default=False, 
                                           type="string",
                                           help="Scanning Path")
    parser.add_option("-i", "--ifileext", 
                           action="store",
                                           dest="ifileext",
                                           default=False, 
                                           type="string",
                                           help="Including FileExt")
    (options, args) = parser.parse_args()
    if(options.path and options.ifileext):
     path = options.path
     ifileext=options.ifileext
    else:
      return
    print "scanning start..."
    scan(path,ifileext)


 
if __name__=="__main__":
    start =time.clock()
    main()
    end = time.clock()
    print ("The function run time is : %.03f seconds" %(end-start))
使用方法:
用法1.png
测试:
用1.png 用2.png
基于关键字的扫描,肯定有漏网之鱼,但是总比手工去查快些。

后来小伙伴说速度有点慢啊,有些客户的网站文件量非常的大,需要更快的扫描速度。
想想那就弄个多线程就能加快扫描。
[Python] 纯文本查看 复制代码
#-*- encoding:UTF-8 -*-
import re
import time
from optparse import OptionParser
import  os
from itertools import islice
import threading

 
p=re.compile("BASE64Decoder\(\).decodeBuffer\(|void BB|Socket\(\)|new RandomAccessFile\(|request.getInputStream\(\)|jspspy|KJ021320|PW_SESSION_ATTRIBUTE|getRuntime\(\).exec\(.*\)|FileOutputStream\(application|getRealPath\(request.getServletPath\(",re.I)
#p=re.compile("Pwd\s{0,3}\=.{1,10}|BASE64Decoder|JFolder|getInputStream\(|getOutputStream\(|jspspy|KJ021320|PW_SESSION_ATTRIBUTE|getRuntime\(.*\)|FileOutputStream\(.*\)|FileInputStream\(.*\)|getRealPath\(.*\)",re.I)
file_log=os.getcwd()+'/'+time.strftime('%Y%m%d%H%M%S')+'.log'
rlock = threading.Lock()

#文件切片处理
def Filelice(filepath):
        with open(filepath) as f:
             while True:
                 next_n_lines = "".join(list(islice(f,40)))
                 if p.findall(next_n_lines):
                     LastCreatedTime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getctime(filepath)))
                     LastModifiedTime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(filepath)))
                     filelog='FilePath:%s\nBadCode:%s\nLast Created Time:%s\nLast Modified Time:%s\n' % (filepath,p.findall(next_n_lines)[0],LastCreatedTime, LastModifiedTime)
                     print filelog+'\n'
                     fo=open(file_log,'a+')
                     fo.write(filelog+'\n')
                     fo.close()
                     break
                 elif not next_n_lines:
                     break


#扫描文件
def scan(file,include_ext):
 global  file_log
 global rlock
 for root, dirs, files in os.walk(file):
     rlock.acquire()
     for filespath in files:
         rlock.acquire()
         if include_ext=='all' or os.path.splitext(os.path.join(root,filespath))[1][1:].lower() in include_ext :
             filelice(os.path.join(root,filespath))
         rlock.release()



 
#主函数
def main():
    parser = OptionParser()
    parser.add_option("-p", "--path", action="store",
                           dest="path",
                                           default=False,
                                           type="string",
                                           help="Scanning Path")
    parser.add_option("-i", "--ifileext",
                           action="store",
                                           dest="ifileext",
                                           default=False,
                                           type="string",
                                           help="Including FileExt")
    parser.add_option("-t", "--threadNum",
                           action="store",
                                           dest="threadNum",
                                           default=5,
                                           type="int",
                                           help="Threading Num")
    (options, args) = parser.parse_args()
    if(options.path and options.ifileext and options.threadNum):
        path = options.path
        ifileext=options.ifileext
        threadNum=options.threadNum
    else:
        return
    print "scanning start..."
    filelist=[]
    for root, dirs, files in os.walk(path):
        for filespath in files:
            if ifileext=='all' or os.path.splitext(filespath)[1][1:].lower() in ifileext.split(','):
                filelist.append(os.path.join(root,filespath))
    for i in  range(0,len(filelist),threadNum):
        threads = []
        for j in range(len(filelist[i:i+threadNum])):
            threads.append(threading.Thread(target=Filelice,args=(filelist[i:i+threadNum][j],)))
        for th in threads:
            th.start()
        for th in threads:
            th.join()




 
if __name__=="__main__":
    start =time.clock()
    main()
    end = time.clock()
    print ("The function run time is : %.03f seconds" %(end-start))
用法:
用法2.png
测试:
用3.png 用4.png
能快很多。

自我介绍一下:
我也是一名90后,大学里修的计算机科学与技术专业,没有接触过信息安全,后面毕业了才第一次接触,当时就下定决心走这条路。
一路下来,主要还是靠自学,遇到不懂的就百度,但是学到的东西都是面向公司业务的,有很多方面接触不到。
所以希望加入一个平台能和大家多交流,互通有无。
新手上路,本来有点不敢投稿,但是看到本次开放注册门槛降低了,就厚着脸皮申请加入90sec。
望组织收留

==========================
超过10位正式成员同意,转正成功。
                                            管理05
单选投票, 共有 16 人参与投票

投票已经结束

100.00% (16)
0.00% (0)
您所在的用户组没有投票权限

评分

参与人数 1酒票 +5 收起 理由
管理05 + 5 欢迎加入90!

查看全部评分

 楼主| 发表于 2016-7-24 19:27:50 | 显示全部楼层
如果那里编辑有问题的,请告知我,我立马改
发表于 2016-7-24 22:27:57 | 显示全部楼层
工程师  666
发表于 2016-7-25 01:12:08 | 显示全部楼层
通过应该没问题了,我看没反对票,挺好的,
 楼主| 发表于 2016-7-25 08:56:10 | 显示全部楼层

哈哈,随便都是工程师啦
 楼主| 发表于 2016-7-25 09:06:43 | 显示全部楼层
80uncle 发表于 2016-7-25 01:12
通过应该没问题了,我看没反对票,挺好的,

谢谢支持
发表于 2016-7-25 10:39:51 | 显示全部楼层
zbjbxf 发表于 2016-7-25 08:56
哈哈,随便都是工程师啦

投你一票  ,0·0
 楼主| 发表于 2016-7-25 11:28:00 | 显示全部楼层
Silence7 发表于 2016-7-25 10:39
投你一票  ,0·0

感谢表格支持

九零

GMT+8, 2026-10-22 14:57

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表