Update of /cvsroot/wisp/wisp/users/dig
In directory sc8-pr-cvs1:/tmp/cvs-serv17466
Added Files:
make-pe-exe.py
Log Message:
added make-pe-exe.py
--- NEW FILE: make-pe-exe.py ---
#! /usr/bin/python
#### make-pe-exe.py - create a PE-format EXE file
#
# Copyleft © 2003 by Andres Soolo (di...@us...)
# This file is licensed under the GNU GPL v2. If you
# don't know what that means, please do read the GPL.
#
#### @(#) $Id: make-pe-exe.py,v 1.1 2003/03/09 14:20:59 digg Exp $
from linkie import Linkie
import time
# This file is *very* incomplete
def make_mz_prefix (message = 'OS too broken'):
if message.find('$') != -1:
raise 'MZ message may not contain dollar signs', message
if message.find('\r') == -1: # if no CRs, insert them
message = '\r\n'.join(message.split('\n'))
b = Linkie('<')
b.set_origin(0x100)
b.emit_string('MZ') # 'MZ'
b.emit_wyde_sum(['#mz/bytes-in-last-block'])
b.emit_wyde_sum(['#mz/blocks-in-file'])
b.emit_wyde(0) # no relocation
b.emit_wyde(0) # empty MZ header--load it all into memory
b.emit_wyde(0x40) # low memory limit in paragraphs
b.emit_wyde(0x40) # high memory limit in paragraphs
b.emit_wyde(-0x10) # initial SS
b.emit_wyde(0x100) # initial SP in PSP
b.emit_wyde(0) # no checksum
b.emit_wyde_sum(['&mz/_start']) # initial IP
b.emit_wyde(-0x10) # initial CS
b.emit_wyde(0) # relocation table offset
b.emit_wyde(0) # not an overlay
b.place_symbol('&mz/_start')
b.emit_string('\x8C\xC8\x8E\xD8') # mov %ax, %cs; mov %ds, mov %ax
b.emit_string('\xB4\x09') # mov %ah, 0x09
b.emit_byte(0xBA); b.emit_wyde_sum(['&mz/message']) # mov %dx, message
b.emit_string('\xCD\x21') # int 0x21
b.emit_string('\xB8\xFF\x4C\xCD\x21') # mov %ax, 0x4CFF; int 0x21
message += '$' # Dollar the Terminator
# a pointer to the PE signature must appear at the offset 0x003C
room = 0x003C - b.filesz()
if message[0] == '!': room += 1
if len(message) <= room:
if message[0] != '!':
b.place_symbol('&mz/message')
b.emit_string(message)
else:
b.place_symbol('&mz/message', b.filesz() - 1)
b.emit_string(message[1:])
b.emit_string('\x00' * (0x003C - b.filesz()))
b.emit_tetra_sum(['mz/pe-offset'])
else:
b.emit_string('\x00' * (0x003C - b.filesz()))
b.emit_tetra_sum(['mz/pe-offset'])
b.place_symbol('&mz/message')
b.emit_string(message)
b.place_symbol('#mz/bytes-in-last-block', b.filesz() % 0x200)
b.place_symbol('#mz/blocks-in-file', (b.filesz() + 0x1FF) / 0x200)
b.link(b.get_symbol_dict())
return b
p = make_mz_prefix('OS too broken')
p.get_file().tofile(open('pehello1.exe', 'w'))
p = make_mz_prefix('OS too broken:(')
p.get_file().tofile(open('pehello2.exe', 'w'))
p = make_mz_prefix('OS too broken :(')
p.get_file().tofile(open('pehello3.exe', 'w'))
p = make_mz_prefix('! OS too broken!')
p.get_file().tofile(open('pehello4.exe', 'w'))
p = make_mz_prefix('! OS too broken !')
p.get_file().tofile(open('pehello5.exe', 'w'))
p = make_mz_prefix('A very long message\nspanning several lines.')
p.get_file().tofile(open('pehello6.exe', 'w'))
p = make_mz_prefix('Many lines spanning several blocks in total.\n' * 40)
p.get_file().tofile(open('pehello7.exe', 'w'))
|