九零登录

[【未通过】] 【未转正】基于linux的webshell 文件监控脚本

[复制链接]
发表于 2016-7-23 16:02:43 | 显示全部楼层 |阅读模式
linux 基本上集成了python环境,该小脚本基于python的pyinotify 模块 可以监控目标文件夹下的多级目录 如果上传新文件 将文件保存在/tmp目录下 后删除该文件,并记录日志 实时监控增、修改、以及删除操作。
pyinotify库下载地址
https://github.com/seb-m/pyinotify
+-------------------------------------------------------------------------------------+
pyino.py


[Python] 纯文本查看 复制代码
#coding = utf-8
import os
import pyinotify
import logging
# LOG class
class classlog(object):
    """log class"""
    def __init__(self,logfilename="/tmp/log.txt",level="INFO"):
        level = level if level in ['CRITICAL','ERROR','WARNING','INFO','DEBUG','NOTSET'] else 'INFO'
        self.logger = logging.getLogger("classlog")
        self.logger.setLevel(logging.DEBUG)
        Fileformatter = logging.Formatter("%(asctime)s - %(levelname)-8s:%(message)s",
        datefmt='%Y-%m-%d %I:%M:%S %p')
        Streamformatter = logging.Formatter("%(asctime)s %(levelname)s:%(message)s",
        datefmt='%Y-%m-%d %I:%M:%S')# ,filename='example.log')

        Filelog = logging.FileHandler(logfilename)
        Filelog.setFormatter(Fileformatter)
        Filelog.setLevel(logging.DEBUG)

        Streamlog = logging.StreamHandler()
        Streamlog.setFormatter(Streamformatter)
        Streamlog.setLevel(level)

        self.logger.addHandler(Filelog)
        self.logger.addHandler(Streamlog)

    def debug(self,msg):
        self.logger.debug(msg)

    def info(self,msg):
        self.logger.info(msg)

    def warn(self,msg):
        self.logger.warn(msg)

    def error(self,msg):
        self.logger.error(msg)

    def critical(self,msg):
        self.logger.critical(msg)

class pyinotify_status(pyinotify.ProcessEvent):
    def process_IN_CREATE(self,event):
        LOG.info("CREATE file: %s"% os.path.join(event.path,event.name))
        # reg=re.compile(r"\( \)")
        # cpfile=reg.sub('',event.pathname)
        # print cpfile
        os.system("cp %s /tmp/"% event.pathname)
        os.system("rm -rf %s"% event.pathname)
    def process_IN_DELETE(self, event):
        LOG.info("DELETE file: %s" % event.pathname)
    def process_IN_MODIFY(self, event):
        LOG.info("MODIFY file: %s" % event.pathname)


def main():
    wm=pyinotify.WatchManager()
    path=raw_input("please input path:")
    wm.add_watch(path,pyinotify.ALL_EVENTS,rec=True)
    eh=pyinotify_status()
    notifier = pyinotify.Notifier(wm, eh)
    notifier.loop()


if __name__=='__main__':
    LOG = classlog("/tmp/log.txt")
    main()

单选投票, 共有 9 人参与投票
您所在的用户组没有投票权限
发表于 2016-7-23 22:21:47 | 显示全部楼层
自己写的还是大自然的搬运工?
发表于 2016-7-24 10:19:52 | 显示全部楼层
首先我之前应该是投了通过票。

一些想法:

虽然代码不一定完全自写,但是可以确定的是,楼主的代码里有自写成分。只是这代码似乎有点太过简单,并且网上已有大量现成的类似代码,所以我觉得其实我应该投反对票。。

找到的一些类似代码:

http://www.cnblogs.com/hollyspirit/p/3182828.html
http://blog.csdn.net/zhou191954/article/details/8262869
http://www.jbxue.com/python/30342.htm
 楼主| 发表于 2016-7-26 15:27:04 | 显示全部楼层
hundan 发表于 2016-7-24 10:19
首先我之前应该是投了通过票。

一些想法:

帅哥,你是否对此有深入研究,比如我想要获取修改了文件的哪些地方,该怎么获取 是否能取得修改的数据值呢?

发表于 2016-7-26 16:51:23 | 显示全部楼层
本帖最后由 hundan 于 2016-7-26 16:54 编辑
Le0nis 发表于 2016-7-26 15:27
帅哥,你是否对此有深入研究,比如我想要获取修改了文件的哪些地方,该怎么获取 是否能取得修改的数据值 ...

linux不了解,说下我基于windows下的想法。
取得修改值无非两种方式,一种从系统底层监控,获取到每个进程的动作,可以准确获取修改,否则不在系统底层的话就只能通过前后文件比对,获取修改位置,在这种情况下如果你要获取修改的数据,必须完全备份一次。
发表于 2016-7-29 08:12:01 | 显示全部楼层
Le0nis 发表于 2016-7-26 15:27
帅哥,你是否对此有深入研究,比如我想要获取修改了文件的哪些地方,该怎么获取 是否能取得修改的数据值 ...

要是Linux的话,感觉还不如diff好使...或者是git diff.

九零

GMT+8, 2026-10-22 13:24

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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