Task: Machine code







Python: import ctypes
import os
from ctypes import c_ubyte, c_int

code = bytes([0x8b, 0x44, 0x24, 0x04, 0x03, 0x44, 0x24, 0x08, 0xc3])

code_size = len(code)

if (os.name == 'posix'):
    import mmap
    executable_map = mmap.mmap(-1, code_size, mmap.MAP_PRIVATE | mmap.MAP_ANON, mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC)
    
    executable_map.write(code)
    
    
    func_address = ctypes.addressof(c_ubyte.from_buffer(executable_map))
elif (os.name == 'nt'):
    
    code_buffer = ctypes.create_string_buffer(code)
    PAGE_EXECUTE_READWRITE = 0x40  
    MEM_COMMIT = 0x1000
    executable_buffer_address = ctypes.windll.kernel32.VirtualAlloc(0, code_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE)
    if (executable_buffer_address == 0):
        print('Warning: Failed to enable code execution, call will likely cause a protection fault.')
        func_address = ctypes.addressof(code_buffer)
    else:
        ctypes.memmove(executable_buffer_address, code_buffer, code_size)
        func_address = executable_buffer_address
else:
    
    code_buffer = ctypes.create_string_buffer(code)
    func_address = ctypes.addressof(code_buffer)

prototype = ctypes.CFUNCTYPE(c_int, c_ubyte, c_ubyte) 
func = prototype(func_address)                        
res = func(7,12)
print(res)



COBOL:        >>SOURCE FORMAT IS FIXED
       IDENTIFICATION DIVISION.
       PROGRAM-ID. MC.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
           01 INSTRUCTIONS.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'55'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'48'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'89'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'E5'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'89'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'7D'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'FC'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'89'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'75'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'F8'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'8B'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'75'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'FC'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'03'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'75'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'F8'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'89'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'75'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'F4'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'8B'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'45'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'F4'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'5D'.
              03 USAGE BINARY-CHAR UNSIGNED VALUE H'C3'.
           01 MMAP.
              03 MMAP-ADDR   USAGE POINTER VALUE NULL.
              03 MMAP-LEN    USAGE BINARY-LONG UNSIGNED VALUE 24.
              03 MMAP-PROT   USAGE BINARY-INT VALUE H'0007'.
              03 MMAP-FLAGS  USAGE BINARY-INT VALUE H'1002'.
              03 MMAP-FD     USAGE BINARY-INT VALUE -1.
              03 MMAP-OFFSET USAGE BINARY-LONG VALUE 0.
           03 CODE-PTR USAGE PROCEDURE-POINTER.
           01 ARG-A USAGE BINARY-INT VALUE 7.
           01 ARG-B USAGE BINARY-INT VALUE 12.
           01 RESULT USAGE BINARY-INT.
       LINKAGE SECTION.
           01 MACHINE-CODE PIC X(24).
       PROCEDURE DIVISION.
       MAIN SECTION.
           PERFORM SET-UP.
           CALL CODE-PTR USING
              BY VALUE ARG-A
              BY VALUE ARG-B
              RETURNING RESULT.
           DISPLAY RESULT.
           PERFORM TEAR-DOWN.
           STOP RUN.

       SET-UP SECTION.
           CALL 'mmap' USING
              BY VALUE MMAP-ADDR
              BY VALUE MMAP-LEN
              BY VALUE MMAP-PROT
              BY VALUE MMAP-FLAGS
              BY VALUE MMAP-FD
              BY VALUE MMAP-OFFSET
              RETURNING CODE-PTR.
           SET ADDRESS OF MACHINE-CODE TO CODE-PTR.
           MOVE INSTRUCTIONS TO MACHINE-CODE.

       TEAR-DOWN SECTION.
           SET ADDRESS OF MACHINE-CODE TO NULL.
           CALL 'munmap' USING
              BY VALUE CODE-PTR
              BY VALUE MMAP-LEN.