Example #1
0
 def from_source(cls, text, arch, base=0x1000):
     text = source.remove_comments(text)
     try:
         blob = assembler.assemble_source(arch, text, base=base)
     except keystone.KsError as error:
         logger.error(
             "Failed to assemble instruction: '{}' at: 0x{:04x}".format(
                 text, base))
         raise error
     return cls.from_bytes(blob, arch, base=base)
Example #2
0
def to_windows_service(arch, payload, service_name='Crimson Forge', writable=False):
	source_path = relpath('data', 'stubs', arch.name.lower(), 'service_wrapper.jnj.asm')
	with open(source_path, 'r') as file_h:
		text = file_h.read()
	text = assembler.render_source(arch, text, variables={
		'payload': payload,
		'permissions': (PAGE_EXECUTE_READWRITE if writable else PAGE_EXECUTE_READ),
		'service_name': service_name
	})
	return assembler.assemble_source(arch, text)
Example #3
0
def main():
    parser = argparse.ArgumentParser(
        'crimson-forge',
        description="Crimson Forge Assembler v{0}".format(
            crimson_forge.__version__),
        conflict_handler='resolve',
        formatter_class=argparse.RawTextHelpFormatter,
        fromfile_prefix_chars='@')
    parser.add_argument('-a',
                        '--arch',
                        dest='arch',
                        default='x86',
                        metavar='value',
                        choices=architectures.keys(),
                        help='the architecture')
    parser.add_argument('-r',
                        '--render',
                        action='store_true',
                        default=False,
                        help='render the source as a template')
    parser.add_argument('input',
                        type=argparse.FileType('r'),
                        help='the input file')
    cli.add_output_arguments(parser, required=True)

    args = parser.parse_args()
    printer = utilities

    arch = architectures[args.arch]

    text = args.input.read()
    if args.render:
        text = assembler.render_source(arch, text)

    match = re.search(r'(^|\s)jmp\.i\d+(\s|$)', text, re.IGNORECASE)
    if match:
        printer.print_warning(
            'sized jmp instruction detected (results may not assemble):')
        lineno = text[:match.span()[0]].count('\n')
        print(" -> {: >4}: {}".format(lineno + 1, text.split('\n')[lineno]))

    try:
        assembled = assembler.assemble_source(arch, text)
    except keystone.KsError as error:
        printer.print_error("Error: {}".format(error.message))
        asm_count = error.get_asm_count()
        if asm_count is not None:
            lines = text.split('\n')
            start = max(0, asm_count - 3)
            end = min(len(lines), asm_count + 3)
            for lineno, line in enumerate(lines[start:end], start):
                print(" {: >2} {: >4}: {}".format(
                    ('->' if lineno == asm_count else ''), lineno, line))
        return
    cli.handle_output(args, printer, arch, assembled)
Example #4
0
 def _permutation_bytes_replacements(self):
     # if replacing instructions, operate at the source level to use labels
     src_code = self.permutation_source(replacements=True)
     try:
         blob = assembler.assemble_source(self.arch,
                                          src_code,
                                          base=self.address)
     except keystone.KsError as error:
         logger.error('Failed to assemble source, error: ' + error.message)
         return None
     return blob
Example #5
0
 def from_source(cls, text, arch, base=0x1000):
     blob = assembler.assemble_source(arch, text, base=base)
     return cls(blob, arch, base=base)