程序段前缀PSP格式如下:
偏移 字节数 说 明
0000 02 中断20H
0002 02 以节计算的内存大小(利用这个可看出是否感染引导型病毒)
0004 01 保留
0005 05 至DOS的长调用
000A 02 INT 22H 入口 IP
000C 02 INT 22H 入口 CS
000E 02 INT 23H 入口 IP
0010 02 INT 23H 入口 CS
0012 02 INT 24H 入口 IP
0014 02 INT 24H 入口 CS
0016 02 父进程的PSP段值(可测知是否被跟踪)
0018 14 存放20个SOFT号
002C 02 环境块段地址(从中可获知执行的程序名)
002E 04 存放用户栈地址指针
0032 1E 保留
0050 03 DOS调用 ( INT 21H / RETF )
0053 02 保留
0055 07 扩展的FCB头
005C 10 格式化的FCB1
006C 10 格式化的FCB2
007C 04 保留
0080 80 命令行参数长度(不包含总为最后的0D)及参数
也是程序运行期间缺省的DTA
现在, 我要利用18H偏移以及80H偏移, 将命令行参数写入一个文件, 完整代码如下:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; filename: psp4.asm ;
; function: write the command line param to a file ;
; usage: psp4.exe hello, welcome ;
; then you will get a file psp4.txt with content: ;
; hello, welcome ;
; author: thinker ;
; e-mail: cnhnyu@gmail.com ;
; qq: 94483026 ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
data segment
szbuf db 128 dup(24h)
filename db 'psp4.txt',0
data ends
stack segment stack
db 200h dup(0)
stack ends
code segment
assume cs:code, ds:data, ss:stack
main proc
mov ax, data
mov ds, ax
; open or create a file
mov ax, 3c00h
xor cx, cx
lea dx, filename
int 21h
jb Over
mov bx, ax ; store the file handle to bx
mov si, 18h ; psp SOFT offset
mov al, es:[si+bx]
mov es:[si+1], al ; change the standard SOFT as file handle SOFT
xor cx, cx
xor si, si
mov cl, es:[80h] ; get the command line length
dec cl
; copy the command line from psp[82h] to szbuf
L:
mov al, es:[82h+si]
mov [si], al
inc si
loop L
; write the szbuf content to the standard output
; but we changed the standard output SOFT to a file handle SOFT
; so in fact, it writes to the file
mov dx, offset szbuf
mov ah, 9
int 21h
mov al, 1
mov es:[si+1], al
mov ah, 3eh
int 21h
Over:
mov ax, 4c00h
int 21h
main endp
code ends
end main
程序利用psp的14H偏移, 修改原有的标准输出的句柄, 把它改成自己创建文件的句柄, 然后再利用psp的80H偏移,获取命令行参数以及长度, 然后把获取的命令行参数内容拷贝到缓冲区, 最后利用dos的21h号中断的4ch号功能, 将内容写入文件。
本来dos的21号中断的4ch号功能是往标准输出写入内容的,这样我们就能在屏幕上看到, 可是这里, 我修改了标准输出的原有句柄为我建立的文件句柄, 于是这个中断就把内容写入了我建立的文件,从而实现了输出的重定向。
psp的功能是相当强大的, 利用它, 可以写出不少有用的程序。 |