def _defaultLineParser(self, line):
line = line.strip()
if line.startswith("/**"):
return None
if self._lineParser:
line = self._lineParser(line)
return line if line else None
def __iter__(self):
return self
def __next__(self):
line = self._file.readline()
if line == '':
self._file.close()
raise StopIteration()
else:
line = self._defaultLineParser(line)
return line if line else next(self)
class Dict(dict):
def init(self, kwargs):
super(Dict, self).init(kwargs)
def __getattr__(self, key):
try:
return self[key]
except KeyError:
raise AttributeError("object dose not have attribute '{0}'".format(key))
def __setattr__(self, key, value):
self[key] = value
class YamlConf(object):
'''
Yaml配置文件加载器
'''
def new(cls, path):
try:
with open(path,"r",encoding='utf-8') as _file:
result = yaml.load(_file)
except IOError:
raise PenError("Loading yaml file '{0}' failed, read file failed".format(path))
except yaml.YAMLError as error:
raise PenError("Loading yaml file '{0}' failed, yaml error, reason: '{1}'".format(path,str(error)))
except Exception as error:
raise PenError("Loading yaml file '{0}' failed, reason: {1}".format(path,str(error)))
!/usr/bin/env python
-- coding:utf-8 --
''' Pentestdb, a database for penetration test. Copyright (c) 2015 alpha1e0 '''
import os import sys import logging import types import re import urllib.parse import urllib.request, urllib.parse, urllib.error
from lxml import etree import yaml
class PenError(Exception): def init(self, errorMsg): self.errorMsg = errorMsg
class DictError(PenError): def str(self): return str(" ".join(["Dict error", self.errorMsg]))
def exceptionHook(etype, evalue, trackback): if isinstance(evalue, KeyboardInterrupt): print("User force exit.") exit() else: sys.excepthook(etype, evalue, trackback)
def addSlashes(line): d = {'"':'\"', "'":"\'", "\0":"\\0", "\":"\\"} return "".join([d.get(x,x) for x in line])
def stripSlashes(line): r = line.replace('\"', '"') r = r.replace("\'", "'") r = r.replace("\\0", "\0") r = r.replace("\\", "\") return r
class WordList(object): ''' 字典文件迭代器 ''' def init(self, fileName, lineParser=None): self._fileName = fileName self._lineParser = lineParser try: self._file = open(self._fileName, 'r') except IOError: raise PenError("Loading wordlist file '{0}' failed".format(fileName))
class Dict(dict): def init(self, kwargs): super(Dict, self).init(kwargs)
class YamlConf(object): ''' Yaml配置文件加载器 ''' def new(cls, path): try: with open(path,"r",encoding='utf-8') as _file: result = yaml.load(_file) except IOError: raise PenError("Loading yaml file '{0}' failed, read file failed".format(path)) except yaml.YAMLError as error: raise PenError("Loading yaml file '{0}' failed, yaml error, reason: '{1}'".format(path,str(error))) except Exception as error: raise PenError("Loading yaml file '{0}' failed, reason: {1}".format(path,str(error)))
class Output(object): ''' 终端输出功能 该类用于输出信息到控制台和文件 ''' _RED = '\033[31m' _BLUE = '\033[34m' _YELLOW = '\033[33m' _GREEN = '\033[32m' _EOF = '\033[0m'
class Log(object): ''' Log class support:critical, error, warning, info, debug, notset Params: logname: specify the logname toConsole: whether outputing to console tofile: whether to logging to file ''' def new(cls, logname=None, toConsole=True, tofile="pen"): logname = logname if logname else "pen"
class URL(object): ''' URL处理 ''' urlPattern = re.compile(r"^((?:http(?:s)?\://)?(?:[-0-9a-zA-Z]+.)+(?:[-0-9a-zA-Z_]+)(?:\:\d+)?)[^:]$") _ipPattern = re.compile(r"^(?:http(s)?\://)?(\d+.){3}(\d+)(?:\:\d+)?.")