'Search a cyclic pattern in registers and memory
Set "pattern" option for basic/extended pattern type
Usage:
MYNAME'

def pattern_search(self, *arg):

    def nearby_offset(v):
        for offset in range((-128), 128, 4):
            pos = cyclic_pattern_offset((v + offset))
            if (pos is not None):
                return (pos, offset)
        return None
    if (not self._is_running()):
        return
    reg_result = {}
    regs = peda.getregs()
    for (r, v) in regs.items():
        if (len(to_hex(v)) < 8):
            continue
        res = nearby_offset(v)
        if res:
            reg_result[r] = res
    if reg_result:
        msg('Registers    contain    pattern    buffer:', 'red')
        for (r, (p, o)) in reg_result.items():
            msg(('%s+%d    found    at    offset:    %d' % (r.upper(), o, p)))
    else:
        msg('No    register    contains    pattern    buffer')
    reg_result = {}
    for (r, v) in regs.items():
        if (not peda.is_address(v)):
            continue
        chain = peda.examine_mem_reference(v)
        (v, t, vn) = chain[(-1)]
        if (not vn):
            continue
        o = cyclic_pattern_offset(vn.strip("'").strip('"')[:4])
        if (o is not None):
            reg_result[r] = (len(chain), (len(vn) - 2), o)
    if reg_result:
        msg('Registers    point    to    pattern    buffer:', 'yellow')
        for (r, (d, l, o)) in reg_result.items():
            msg(('[%s]    %s    offset    %d    -    size    ~%d' % (r.upper(), ('-->' * d), o, l)))
    else:
        msg('No    register    points    to    pattern    buffer')
    maps = peda.get_vmmap()
    search_result = []
    for (start, end, perm, name) in maps:
        if ('w' not in perm):
            continue
        res = cyclic_pattern_search(peda.dumpmem(start, end))
        for (a, l, o) in res:
            a += start
            search_result += [(a, l, o)]
    sp = peda.getreg('sp')
    if search_result:
        msg('Pattern    buffer    found    at:', 'green')
        for (a, l, o) in search_result:
            ranges = peda.get_vmrange(a)
            text = ('%s    :    offset    %4d    -    size    %4d' % (to_address(a), o, l))
            if (ranges[3] == '[stack]'):
                text += ('    ($sp    +    %s    [%d    dwords])' % (to_hex((a - sp)), ((a - sp) // 4)))
            else:
                text += ('    (%s)' % ranges[3])
            msg(text)
    else:
        msg('Pattern    buffer    not    found    in    memory')
    ref_result = []
    for (a, l, o) in search_result:
        res = peda.searchmem_by_range('all', ('0x%x' % a))
        ref_result += [(x[0], a) for x in res]
    if (len(ref_result) > 0):
        msg('References    to    pattern    buffer    found    at:', 'blue')
        for (a, v) in ref_result:
            ranges = peda.get_vmrange(a)
            text = ('%s    :    %s' % (to_address(a), to_address(v)))
            if (ranges[3] == '[stack]'):
                text += ('    ($sp    +    %s    [%d    dwords])' % (to_hex((a - sp)), ((a - sp) // 4)))
            else:
                text += ('    (%s)' % ranges[3])
            msg(text)
    else:
        msg('Reference    to    pattern    buffer    not    found    in    memory')
    return
