'Format raw shellcode to ndisasm output display
"j"  # 0x00000000:    push byte +0x1
"["      # 0x00000002:    pop ebx
TODO: understand syscall numbers, socket call'

@staticmethod
def format_shellcode(buf, mode=32):

    def nasm2shellcode(asmcode):
        if (not asmcode):
            return ''
        shellcode = []
        pattern = re.compile('([0-9A-F]{8})\\s*([^\\s]*)\\s*(.*)')
        matches = pattern.findall(asmcode)
        for line in asmcode.splitlines():
            m = pattern.match(line)
            if m:
                (addr, bytes, code) = m.groups()
                sc = ('"%s"' % to_hexstr(codecs.decode(bytes, 'hex')))
                shellcode += [(sc, ('0x' + addr), code)]
        maxlen = max([len(x[0]) for x in shellcode])
        text = ''
        for (sc, addr, code) in shellcode:
            text += ('%s    #    %s:                %s\n' % (sc.ljust((maxlen + 1)), addr, code))
        return text
    out = execute_external_command(('%s    -b    %d    -' % (config.NDISASM, mode)), buf)
    return nasm2shellcode(out)
