資源簡介
文件監(jiān)控
代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
#use:?python?file_check.py?./
import?os
import?hashlib
import?shutil
import?ntpath
import?time
CWD?=?os.getcwd()
FILE_MD5_DICT?=?{}??????#?文件MD5字典
ORIGIN_FILE_LIST?=?[]
#?特殊文件路徑字符串
Special_path_str?=?‘drops_JWI96TY7ZKNMQPDRUOSG0FLH41A3C5EXVB82‘
bakstring?=?‘bak_EAR1IBM0JT9HZ75WU4Y3Q8KLPCX26NDFOGVS‘
logstring?=?‘log_WMY4RVTLAJFB28960SC3KZX7EUP1IHOQN5GD‘
webshellstring?=?‘webshell_WMY4RVTLAJFB28960SC3KZX7EUP1IHOQN5GD‘
difffile?=?‘diff_UMTGPJO17F82K35Z0LEDA6QB9WH4IYRXVSCN‘
Special_string?=?‘drops_log‘??#?免死金牌
UNICODE_ENCODING?=?“utf-8“
INVALID_UNICODE_CHAR_FORMAT?=?r“\?%02x“
#?文件路徑字典
spec_base_path?=?os.path.realpath(os.path.join(CWD?Special_path_str))
Special_path?=?{
????‘bak‘?:?os.path.realpath(os.path.join(spec_base_path?bakstring))
????‘log‘?:?os.path.realpath(os.path.join(spec_base_path?logstring))
????‘webshell‘?:?os.path.realpath(os.path.join(spec_base_path?webshellstring))
????‘difffile‘?:?os.path.realpath(os.path.join(spec_base_path?difffile))
}
def?isListLike(value):
????return?isinstance(value?(list?tuple?set))
#?獲取Unicode編碼
def?getUnicode(value?encoding=None?noneToNull=False):
????if?noneToNull?and?value?is?None:
????????return?NULL
????if?isListLike(value):
????????value?=?list(getUnicode(_?encoding?noneToNull)?for?_?in?value)
????????return?value
????if?isinstance(value?unicode):
????????return?value
????elif?isinstance(value?basestring):
????????while?True:
????????????try:
????????????????return?unicode(value?encoding?or?UNICODE_ENCODING)
????????????except?UnicodeDecodeError?ex:
????????????????try:
????????????????????return?unicode(value?UNICODE_ENCODING)
????????????????except:
????????????????????value?=?value[:ex.start]?+?““.join(INVALID_UNICODE_CHAR_FORMAT?%?ord(_)?for?_?in?value[ex.start:ex.end])?+?value[ex.end:]
????else:
????????try:
????????????return?unicode(value)
????????except?UnicodeDecodeError:
????????????return?unicode(str(value)?errors=“ignore“)
#?目錄創(chuàng)建
def?mkdir_p(path):
????import?errno
????try:
????????os.makedirs(path)
????except?OSError?as?exc:
????????if?exc.errno?==?errno.EEXIST?and?os.path.isdir(path):
????????????pass
????????else:?raise
#?獲取當(dāng)前所有文件路徑
def?getfilelist(cwd):
????filelist?=?[]
????for?rootsubdirs?files?in?os.walk(cwd):
????????for?filepath?in?files:
????????????originalfile?=?os.path.join(root?filepath)
????????????if?Special_path_str?not?in?originalfile:
????????????????filelist.append(originalfile)
????return?filelist
#?計算機(jī)文件MD5值
def?calcMD5(filepath):
????try:
????????with?open(filepath‘rb‘)?as?f:
????????????md5obj?=?hashlib.md5()
????????????md5obj.update(f.read())
????????????hash?=?md5obj.hexdigest()
????????????return?hash
????except?Exception?e:
????????print?u‘[!]?getmd5_error?:?‘?+?getUnicode(filepath)
????????print?getUnicode(e)
????????try:
????????????ORIGIN_FILE_LIST.remove(filepath)
????????????FILE_MD5_DICT.pop(filepath?None)
????????except?KeyError?e:
????????????pass
#?獲取所有文件MD5
def?getfilemd5dict(filelist?=
- 上一篇:python爬蟲爬取微博熱搜
- 下一篇:a*算法的python版
評論
共有 條評論