一个自定义扫描web程序源代码的工具

本文介绍了一种使用Python正则表达式扫描并替换文件内容的方法,包括基础的替换和更复杂的多条件替换功能。此外,作者还提及了对当前开源代码检测工具的不满,并计划开发一个Webshell管理系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import sys
import getopt
import os
import re

def replace(dir,regex):
    p = re.compile(regex)
    WalkDir(dir,p)

def replacemore(dir,regex,regex2):
    p = re.compile(regex)
    p2 = re.compile(regex2)
    WalkDirmore(dir,p,p2)


def WalkDir( dir, regex,dir_callback = None, file_callback = None ):   
    for root, dirs, files in os.walk(dir):   
        for f in files:   
            file_path = os.path.join(root, f)   
            checkfile(file_path,regex)

def WalkDirmore( dir, regex,regex2):   
    for root, dirs, files in os.walk(dir):   
        for f in files:   
            file_path = os.path.join(root, f)   
            checkfilemore(file_path,regex,regex2)
            
def checkfilemore(file,regex,regex2):
    f1 = open(file, mode = 'r')    
    line = f1.readline()
    linen = 1
    key=0    
    while line:
        if regex.match(line):
            key=1
        if  regex2.match(line)and key==1 :
            print file+" : "+str(linen)
        linen +=1
        line = f1.readline()    
    f1.close() 
    
def checkfile(file,regex):    
    f1 = open(file, mode = 'r')    
    line = f1.readline()
    linen = 1  

      
    while line:
        if  regex.match(line) :
            print file+" : "+str(linen)
        linen +=1
        line = f1.readline()    
    f1.close() 
    
#replace("F:\phpcms",".*new\s\w*\(\$.*?")
#replace("F:\phpcms",".*get\(.*?")
replace("F:\phpcms",".*if\(intval.*?")

 写得很水,就是可以扫描web目录下面的文件内容,用正则匹配下,大家可以自由发挥。。比如说.*mysql_query.*==

这只是个模子。

 

    用用了好多开源的代码检测工具,swaats,rats,pixy,yasca,appcodescan,等等,都感觉有点不如意,所以打算慢慢发展一个,明天写一个webshell管理器。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值