Skip to content

Commit

Permalink
Merge pull request #36 from ebranca/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
hephex committed Oct 18, 2014
2 parents ed37e4f + e6c7146 commit ac5a531
Show file tree
Hide file tree
Showing 36 changed files with 2,303 additions and 415 deletions.
137 changes: 0 additions & 137 deletions demo/binary_search/pescan.py

This file was deleted.

101 changes: 71 additions & 30 deletions pysec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,74 @@
#
# -*- coding: ascii -*-
"""PySec is a set of tools for secure application development under Linux"""
from pysec import (alg,
core,
entropy,
expr,
io,
lang,
kv,
load,
log,
net,
stats,
utils,
xsplit)


_OPEN_MODES = {
'r': io.fd.FO_READ,
'w': io.fd.FO_WRITE,
'a': io.fd.FO_APPEND
}


def open(path, mode='r'):
mode = _OPEN_MODES.get(str(mode), None)
if mode is None:
raise ValueError(lang.WRONG_OPEN_MODE % mode)
return io.fd.File.open(path, mode)


xrange = utils.xrange
import sys

from pysec import config
from pysec import core
from pysec.io import fd
from pysec import lang
from pysec import log
from pysec import tb
from pysec import utils


REMOVE_BUILTINS = 'eval', 'execfile'

Object = core.Object
Error = core.Error


def set_builtins():
_OPEN_MODES = {
'r': fd.FO_READEX,
'w': fd.FO_WRITE,
'a': fd.FO_APPEND
}
def open(path, mode='r'):
_mode = _OPEN_MODES.get(str(mode), None)
if _mode is None:
raise ValueError(lang.WRONG_OPEN_MODE % mode)
return fd.File.open(path, _mode)
BUILTINS = {
# 'dict': core.Dict,
'file': open,
'input': raw_input,
'list': core.List,
'object': core.Object,
'open': open,
# 'set': core.Set,
# 'str': core.String,
# 'tuple': core.Tuple,
'xrange': utils.xrange,
'range': utils.range
}
def not_implemented(name):
def _not_implemented(*arg, **kwds):
raise NotImplementedError("builtin %r was disabled by PySec" % name)
return _not_implemented
builtins = {}
for key, value in __builtins__.iteritems():
builtins[key] = BUILTINS.get(key, None) or \
(not_implemented(key) if key in REMOVE_BUILTINS else __builtins__[key])
__builtins__.clear()
__builtins__.update(builtins)


def init(name=__name__, fields=None, timer=None, emitter=log.emit_simple, save_actions=None, save_errors=None, hook_tb=tb.long_tb):
if isinstance(name, tuple):
name, code = name
code = log.register_action(str(name), int(code))
else:
code = log.register_action(name)
log.start_root_log(code, fields, timer)
if emitter:
log.add_global_emit(emitter)
sys.path = []
set_builtins()
tb.set_excepthook(hook_tb)
#
if save_errors:
log.save_errors(save_errors)
if save_actions:
log.save_actions(save_actions)

107 changes: 101 additions & 6 deletions pysec/binary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/python -OOBRStt
""""""
from pysec.core import is_int, is_str, is_dict


SPECIAL_CHARS = '\\', '*', '?', '!', '[', ']', '{', '}', '-', ',', '#', '@'

ALPHA = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
Expand All @@ -12,14 +15,99 @@
NOT_PRINTABLE = tuple(chr(ch) for ch in xrange(0x00, 0x20))
VISIBLE = tuple(chr(ch) for ch in xrange(0x21, 0x7F))
NOT_VISIBLE = tuple(chr(ch) for ch in xrange(0x00, 0x21))
ASCII_CHAR = tuple(chr(ch) for ch in xrange(0x00, 0x7F))
ASCII_CHAR = tuple(chr(ch) for ch in xrange(0x00, 0x80))

CTRL_CHAR = ''.join(chr(ch) for ch in xrange(0x00, 0x1F)) + '\x7F'

CTRL2ABBR = {
'\x00': 'NUL',
'\x01': 'SOH',
'\x02': 'STX',
'\x03': 'ETX',
'\x04': 'EOT',
'\x05': 'ENQ',
'\x06': 'ACK',
'\x07': 'BEL',
'\x08': 'BS',
'\x09': 'HT',
'\x0A': 'LF',
'\x0B': 'VT',
'\x0C': 'FF',
'\x0D': 'CR',
'\x0E': 'SO',
'\x0F': 'SI',
'\x10': 'DLE',
'\x11': 'DC1',
'\x12': 'DC2',
'\x13': 'DC3',
'\x14': 'DC4',
'\x15': 'NAK',
'\x16': 'SYN',
'\x17': 'ETB',
'\x18': 'CAN',
'\x19': 'EM',
'\x1A': 'SUB',
'\x1B': 'ESC',
'\x1C': 'FS',
'\x1D': 'GS',
'\x1E': 'RS',
'\x1F': 'US',
'\x7F': 'DEL',
}

CTRL2CARETNOTATION = {
'\x00': '^@',
'\x01': '^A',
'\x02': '^B',
'\x03': '^C',
'\x04': '^D',
'\x05': '^E',
'\x06': '^F',
'\x07': '^G',
'\x08': '^H',
'\x09': '^I',
'\x0A': '^J',
'\x0B': '^K',
'\x0C': '^L',
'\x0D': '^M',
'\x0E': '^N',
'\x0F': '^O',
'\x10': '^P',
'\x11': '^Q',
'\x12': '^R',
'\x13': '^S',
'\x14': '^T',
'\x15': '^U',
'\x16': '^V',
'\x17': '^W',
'\x18': '^X',
'\x19': '^Y',
'\x1A': '^Z',
'\x1B': '^[',
'\x1C': '^\\',
'\x1D': '^]',
'\x1E': '^^',
'\x1F': '^_',
'\x7F': '^?',
}

CTRL2CESCAPE = {
'\x00': r'\0',
'\x07': r'\a',
'\x08': r'\b',
'\x09': r'\t',
'\x0A': r'\n',
'\x0B': r'\v',
'\x0C': r'\f',
'\x0D': r'\r',
'\x1B': r'\e',
}

BACKSLASH_ORD = ord('\\')


MASK_NONE_CHAR = 0
MASK_ALL_CHAR = 2 ** 257 - 1
MASK_ALL_CHAR = object()
MASK_PRINT = reduce(lambda a, b: a | b, (1 << ord(ch) for ch in PRINTABLE))
MASK_NOT_PRINT = (2 ** 0x20) - 1
MASK_ALPHNUM = reduce(lambda a, b: a | b, (1 << ord(ch) for ch in ALPHANUMERIC))
Expand Down Expand Up @@ -101,6 +189,8 @@ def byte_search(text, pattern, offset=0):
p += 1
else:
p = 0
elif pc is MASK_ALL_CHAR:
p += 1
elif isinstance(pc, (int, long)):
if pc & (1 << (ord(tc) + 1)):
p += 1
Expand Down Expand Up @@ -144,7 +234,7 @@ def ancestors(self):


def byte_msearch(text, patterns, offset=0):
if isinstance(patterns, dict):
if is_dict(patterns):
patterns = patterns.iteritems()
else:
patterns = ((p, None) for p in patterns)
Expand All @@ -170,15 +260,19 @@ def byte_msearch(text, patterns, offset=0):
while actual_trees:
tree = actual_trees.pop()
if tree.eop:
yield t, [node.token for node in reversed(tuple(node.ancestors())[:-1])], tree.name
yield t, [node.token for node in reversed(tuple(tree.ancestors())[:-1])], tree.name
tree.eop = 0
for pc, node in tree.items():
if isinstance(pc, str):
if is_str(pc):
pc = str(pc)
if pc == tc:
next_trees.add(node)
else:
next_trees.add(root)
elif isinstance(pc, (int, long)):
elif pc is MASK_ALL_CHAR:
next_trees.add(node)
elif is_int(pc):
pc = int(pc)
if pc & (1 << (ord(tc) + 1)):
next_trees.add(node)
else:
Expand All @@ -187,3 +281,4 @@ def byte_msearch(text, patterns, offset=0):
raise Exception("unknown token: %r" % pc)
actual_trees = next_trees
t += 1

Loading

0 comments on commit ac5a531

Please sign in to comment.