Пример #1
0
def main(args=sys.argv):
    def usage():
        global MAIN_FUNCTIONS
        print('Welcome to pwny!')
        print()
        print('Available apps:')
        longest_app_name = max(len(app) for app in MAIN_FUNCTIONS)
        for app, config in MAIN_FUNCTIONS.items():
            f = config['callable']
            fmt = ' - %%-%ds   %%s' % longest_app_name
            print(fmt % (app, f.__doc__.strip().split('\n')[0]))
        print()
        sys.exit(1)

    # Import everything so all main functions are registered.
    import pwny

    global MAIN_FUNCTIONS

    app = os.path.basename(args[0])
    app_args = args[1:]

    if app not in MAIN_FUNCTIONS:
        if len(args) < 2 or app_args[0] not in MAIN_FUNCTIONS:
            usage()
        prog = '%s %s' % (app, app_args[0])
        app, app_args = app_args[0], app_args[1:]
    else:
        prog = app

    f = MAIN_FUNCTIONS[app]['callable']
    parser = argparse.ArgumentParser(
        prog=prog,
        description=f.__doc__,
    )
    parser.add_argument('-f',
                        '--format',
                        dest='format',
                        choices=['raw', 'hex', 'py', 'sh', 'b64'],
                        default='raw',
                        help='set output format')
    parser.add_argument(
        '-n',
        '--no-newline',
        dest='no_newline',
        action='store_const',
        const=True,
        help='inhibit newline after output (off when run from tty)')

    output = f(parser, app, app_args)

    if output is not None:
        if six.PY3:
            output_bytes = os.fsencode(output)
        else:
            output_bytes = output

        args = parser.parse_args(app_args)
        if args.no_newline or not sys.stdout.isatty():
            end = ''
        else:
            end = '\n'

        if args.format == 'raw':
            if isinstance(output, six.binary_type):
                writer = io.open(sys.stdout.fileno(), mode='wb', closefd=False)
                writer.write(output)
                writer.write(end if not six.PY3 else os.fsencode(end))
                writer.close()
            else:
                print(output, end=end)
        elif args.format == 'hex':
            print(pwny.enhex(output_bytes), end=end)
        elif args.format == 'py':
            print(repr(output), end=end)
        elif args.format == 'sh':
            r = repr(output)
            if r.startswith('b\''):
                r = r[1:]
            print('$' + r, end=end)
        elif args.format == 'b64':
            print(pwny.enb64(output_bytes), end=end)
Пример #2
0
def test_enhex():
    assert pwny.enhex(b'ABCD') == '41424344'
Пример #3
0
def main():
    def usage():
        global MAIN_FUNCTIONS
        print('Welcome to pwny!')
        print()
        print('Available apps:')
        longest_app_name = max(len(app) for app in MAIN_FUNCTIONS)
        for app, config in MAIN_FUNCTIONS.items():
            f = config['callable']
            fmt = ' - %%-%ds   %%s' % longest_app_name
            print(fmt % (app, f.__doc__.strip().split('\n')[0]))
        print()
        sys.exit(1)

    # Import everything so all main functions are registered.
    import pwny

    global MAIN_FUNCTIONS

    app = os.path.basename(sys.argv[0])
    app_args = sys.argv[1:]

    if app not in MAIN_FUNCTIONS:
        if len(sys.argv) < 2 or app_args[0] not in MAIN_FUNCTIONS:
            usage()
        prog = '%s %s' % (app, app_args[0])
        app, app_args = app_args[0], app_args[1:]
    else:
        prog = app

    f = MAIN_FUNCTIONS[app]['callable']
    parser = argparse.ArgumentParser(
        prog=prog,
        description=f.__doc__,
    )
    parser.add_argument(
        '-f', '--format',
        dest='format',
        choices=['raw', 'hex', 'py', 'sh', 'b64'],
        default='raw',
        help='set output format'
    )
    parser.add_argument(
        '-n', '--no-newline',
        dest='no_newline',
        action='store_const',
        const=True,
        help='inhibit newline after output'
    )

    output = f(parser, app, app_args)

    if output is not None:
        if six.PY3:
            output_bytes = os.fsencode(output)
        else:
            output_bytes = output

        args = parser.parse_args(app_args)
        if args.no_newline:
            end = ''
        else:
            end = '\n'

        if args.format == 'raw':
            if isinstance(output, six.binary_type):
                writer = io.open(sys.stdout.fileno(), mode='wb', closefd=False)
                writer.write(output_bytes)
                writer.close()
            else:
                print(output, end=end)
        elif args.format == 'hex':
            print(pwny.enhex(output_bytes), end=end)
        elif args.format == 'py':
            print(repr(output), end=end)
        elif args.format == 'sh':
            r = repr(output)
            if r.startswith('b\''):
                r = r[1:]
            print('$' + r, end=end)
        elif args.format == 'b64':
            print(pwny.enb64(output_bytes), end=end)