Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1054034
  • 博文数量: 50
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 2037
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-05 08:03
文章分类
文章存档

2011年(1)

2010年(3)

2009年(17)

2008年(29)

我的朋友

分类:

2008-03-31 20:32:36

;//code by laomms 2007.6

.586
.model flat, stdcall
option casemap :none


include        windows.inc
include        user32.inc
include        kernel32.inc
include        masm32.inc
include comdlg32.inc
include shell32.inc
include comctl32.inc

includelib    user32.lib
includelib    kernel32.lib
includelib    masm32.lib
includelib comdlg32.lib
includelib shell32.lib
includelib comctl32.lib
include bin2coff.inc

IDR_MENU equ 10000
ico equ 2001
IDD_Main equ 1000
IDC_FilePath equ 1002
IDC_Open equ 1003
IDC_Label equ 1007
IDC_OutputName equ 1008
IDC_Save equ 1009
IDC_Start equ 1013
IDM_Context equ 10004
IDM_About equ 10002
IDM_Exit equ 10003

DlgProc     proto        :DWORD,:DWORD,:DWORD,:DWORD
CTEXT MACRO y:VARARG
        LOCAL sym
                CONST segment
                ifidni <y>,<>
                sym db 0
                else
                sym db y,0
                endif
                CONST ends
                exitm <offset sym>
ENDM

.data
ofn OPENFILENAME <>
n        db    0
style        db    0    
szTagetName db 40 dup(0)
LabelName db 40 dup(0)


.data?
ProgPath db 256 dup(?)
AppPath db 1024 dup (?)
BufTgt        dd    ?    
hTagetFile    dd    ?    
hInstance    dd    ?
hDlg dd ?
hMenu dd ?
hSubMenu1 dd ?
hSubMenu2 dd ?
hSubMenu3 dd ?
nRead        dd    ?    
hFile        dd ?            
hHeap        dd ?            
NumberOfBytesRead dd ?            
nNumberOfBytesToRead dd    ?        
nNumberOfBytesToWrite dd ?    
lpBuffer    dd ?            
lpMem        dd ?    

.code
start:
        invoke GetModuleHandle, NULL
        mov hInstance,eax
        invoke DialogBoxParam, hInstance, IDD_Main, NULL, addr DlgProc, NULL
        invoke ExitProcess,eax
DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
        push hWnd
        pop hDlg
    .if uMsg == WM_INITDIALOG
        invoke GetWindowLong,hWnd,GWL_EXSTYLE
        or eax,WS_EX_LAYERED
        invoke    LoadIcon,hInstance,ico
     invoke    SendMessage,hWnd,WM_SETICON,1,eax
                invoke LoadMenu, hInstance, CTEXT("MyMenu")
                mov hMenu , eax
                invoke GetSubMenu, hMenu,0
                mov hSubMenu1, eax
                invoke GetSubMenu, hMenu,1
                mov hSubMenu2, eax
                invoke GetSubMenu, hMenu,2
                mov hSubMenu3, eax
    .elseif uMsg==WM_COMMAND
        mov eax,wParam
        .if ax==IDC_Open
            call GetFilePath
            invoke SetDlgItemText,hWnd,IDC_FilePath,addr AppPath
        .elseif ax==IDC_Start
            call Covert
        .elseif ax==IDC_Save
         call SaveAs
        .elseif ax==IDM_Context            
            invoke MessageBox,NULL,CTEXT("这个小工具可以将任何文件转成16进制数据格式以供调用。") ,CTEXT("说明"),MB_OK
        .elseif ax==IDM_About
         invoke MessageBox,NULL,CTEXT("-BIN2COFF- ",13,"-== laomms 2007.7 ==--") ,CTEXT("关于"),MB_OK
        .elseif ax==IDM_Exit
         invoke    EndDialog,hWnd,0
        .endif
        .elseif uMsg==WM_CONTEXTMENU
            mov eax, lParam         
                and eax, 0ffffh
                mov ebx, lParam
                shr ebx, 16
                invoke TrackPopupMenu, hSubMenu1, TPM_LEFTALIGN, eax, ebx, 0, hWnd, 0
                invoke TrackPopupMenu, hSubMenu2, TPM_LEFTALIGN, eax, ebx, 0, hWnd, 0
                invoke TrackPopupMenu, hSubMenu3, TPM_LEFTALIGN, eax, ebx, 0, hWnd, 0
    .elseif    uMsg== WM_CLOSE
        invoke EndDialog,hWnd,0
    .endif
    xor eax,eax
    ret
DlgProc endp
GetFilePath proc
                mov ofn.lStructSize, sizeof ofn
                push hDlg
                pop ofn.hwndOwner
                push hInstance
                pop ofn.hInstance
                lea eax, offset AppPath
                mov ofn.lpstrFile, eax
                mov ofn.nMaxFile, offset AppPath
                mov ofn.Flags, OFN_FILEMUSTEXIST + OFN_PATHMUSTEXIST
                mov ofn.lpstrFilter, CTEXT("所有文件",0,"*.*",0 , 0)
                mov ofn.lpstrTitle, CTEXT("请打开一个PE可执行文件…")
                invoke GetOpenFileName, addr ofn
                .if !eax
                        invoke MessageBox,hDlg,CTEXT("打开程序错误!"),CTEXT("提示"),MB_OK
                .endif
                ret
GetFilePath endp
SaveAs        proc
LOCAL @stOF:OPENFILENAME

        invoke    RtlZeroMemory,addr @stOF,sizeof @stOF
        mov    @stOF.lStructSize,sizeof @stOF
        push    hDlg
        pop    @stOF.hwndOwner
        mov    @stOF.lpstrFilter,CTEXT("MS COFF文件 (*.obj)",0,'*.obj',0)
        mov    @stOF.lpstrFile,offset szTagetName
        mov    @stOF.nMaxFile,MAX_PATH
        mov    @stOF.Flags,OFN_PATHMUSTEXIST
        mov    @stOF.lpstrDefExt,CTEXT('obj',0)
        mov    @stOF.lpstrTitle,CTEXT("保存为文本文件")
        invoke    GetSaveFileName,addr @stOF
        .if    eax
            ;invoke     CreateFile,addr szTagetName,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
         ;.if eax==INVALID_HANDLE_VALUE
         ; invoke MessageBox,hDlg,CTEXT("创建错误"),CTEXT("提示"),MB_OK
         ;     ret
         ;.endif
            ;mov    hTagetFile,eax
            invoke SetDlgItemText,hDlg,IDC_OutputName,addr szTagetName
        .else
            invoke MessageBox,NULL,CTEXT("设置路径错误!!!"),CTEXT("提示"),MB_OK            
        .endif
        mov    eax,FALSE
        ret

SaveAs        endp
Covert proc
                invoke GetDlgItemText,hDlg,IDC_FilePath,addr AppPath,sizeof AppPath
                .if eax==0
                    invoke MessageBox,hDlg,CTEXT("请选择源文件!"),CTEXT("提示"),MB_OK
                    ret
                .endif
                invoke GetDlgItemText,hDlg,IDC_Label,addr LabelName,sizeof LabelName
                .if eax==0
                    invoke MessageBox,hDlg,CTEXT("还未设置标签名称!"),CTEXT("提示"),MB_OK
                    ret
                .endif
                push offset LabelName
                call GetLabel
                mov ebx, eax
        invoke     CreateFile,addr AppPath,GENERIC_READ,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
        .if eax==INVALID_HANDLE_VALUE
            invoke MessageBox,hDlg,CTEXT("打开错误!"),CTEXT("提示"),MB_OK
            ret
        .endif
        mov    hFile,eax
        invoke     GetFileSize,eax,0
        .if eax==0
            invoke MessageBox,hDlg,CTEXT("源文件内容为空!"),CTEXT("提示"),MB_OK
            ret
        .endif
        mov    nNumberOfBytesToRead, eax
        lea    ecx, [eax+3Ch]
        mov    nNumberOfBytesToWrite, ecx
        lea    ecx, [ecx+ebx+17h]
        push    ecx        ; dwBytes
        call    GetProcessHeap

        mov    hHeap, eax
        push    8        ; dwFlags
        push    eax        ; hHeap
        call    HeapAlloc
        .if eax==0
            invoke MessageBox,hDlg,CTEXT("不能申请内存!"),CTEXT("提示"),MB_OK
            ret
        .endif
        mov    edi, eax
        mov    lpBuffer, eax
        mov    lpMem, eax
        add    eax, 3Ch
        invoke     ReadFile,hFile,eax,nNumberOfBytesToRead,offset NumberOfBytesRead,0
        .if eax==INVALID_HANDLE_VALUE
            invoke MessageBox,hDlg,CTEXT("读取文件错误!"),CTEXT("提示"),MB_OK
            ret
        .endif
        invoke CloseHandle,hFile
        invoke     CreateFile,addr szTagetName,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
        .if eax==INVALID_HANDLE_VALUE
            invoke MessageBox,hDlg,CTEXT("创建错误"),CTEXT("提示"),MB_OK
            ret
        .endif
        mov    hTagetFile,eax
        mov    word ptr [edi],    14Ch
        mov    word ptr [edi+2], 1
        mov    dword ptr [edi+4], 0
        mov    eax, nNumberOfBytesToWrite
        mov    [edi+8], eax
        mov    dword ptr [edi+0Ch], 1
        mov    word ptr [edi+10h], 0
        mov    word ptr [edi+12h], 184h
        add    edi, 14h
        mov    dword ptr [edi], 7461642Eh
        mov    dword ptr [edi+4], 61h
        mov    dword ptr [edi+8], 0
        mov    dword ptr [edi+0Ch], 0
        mov    eax, nNumberOfBytesToRead
        mov    [edi+10h], eax
        mov    dword ptr [edi+14h], 3Ch
        mov    dword ptr [edi+18h], 0
        mov    dword ptr [edi+1Ch], 0
        mov    word ptr [edi+20h], 0
        mov    word ptr [edi+22h], 0
        mov    dword ptr [edi+24h], 0C0300040h
        add    edi, 28h
        invoke     WriteFile,hTagetFile,lpBuffer,nNumberOfBytesToWrite,offset NumberOfBytesRead,0
        .if eax==INVALID_HANDLE_VALUE
            invoke MessageBox,hDlg,CTEXT("写入错误!"),CTEXT("提示"),MB_OK
            ret
        .endif
                
        add    edi, nNumberOfBytesToRead
        mov    lpBuffer, edi
        cmp    ebx, 8
        jbe    @1

        mov    dword ptr [edi], 0
        mov    dword ptr [edi+4], 4
        jmp    done

@1:                
        push    ebx
        push    edi
        push    offset LabelName
        call    search


done:                
        mov    dword ptr [edi+8], 0
        mov    word ptr [edi+0Ch], 1
        mov    word ptr [edi+0Eh], 0
        mov    byte ptr [edi+10h], 2
        mov    byte ptr [edi+11h], 0
        add    edi, 12h
        mov    eax, ebx
        cmp    ebx, 8
        jbe    @2

        add    eax, 5
        mov    [edi], eax
        add    edi, 4
        push    eax
        push    ebx
        push    edi
        push    offset LabelName ; "_pEXE"
        call    search
        pop    eax
        jmp    @WriteFile
@2:                
        mov    eax, 4
        mov    [edi], eax
@WriteFile:                
        add    eax, 12h
                invoke     WriteFile,hTagetFile,lpBuffer,eax,offset NumberOfBytesRead,0
        .if eax==INVALID_HANDLE_VALUE
            invoke MessageBox,hDlg,CTEXT("写入错误!"),CTEXT("提示"),MB_OK
            ret
        .endif
                invoke CloseHandle,hTagetFile
                invoke HeapFree,hHeap,0,lpMem
        ret
Covert endp
search     proc near        

arg_0        = dword    ptr 8
arg_4        = dword    ptr 0Ch
arg_8        = dword    ptr 10h

        push    ebp
        mov    ebp, esp
        push    esi
        push    edi
        cld
        mov    esi, [ebp+arg_0]
        mov    edi, [ebp+arg_4]
        mov    ecx, [ebp+arg_8]
        shr    ecx, 2
        rep movsd
        mov    ecx, [ebp+arg_8]
        and    ecx, 3
        rep movsb
        pop    edi
        pop    esi
        leave
        retn    0Ch

search     endp
GetLabel    proc near        

arg_0        = dword    ptr 4

        mov    eax, [esp+arg_0]
        lea    esp, [esp+0]
        add    eax, 0


@@1:                
        mov    edx, [eax]
        add    eax, 4
        test    edx, 0FFh
        jz    @@4

        test    edx, 0FF00h
        jz    @@3

        test    edx, 0FF0000h
        jz    @@2

        test    edx, 0FF000000h
        jnz    @@1

        sub    eax, 1
        sub    eax, [esp+arg_0]
        retn    4

@@2:                
        sub    eax, 2
        sub    eax, [esp+arg_0]
        retn    4

@@3:                
        sub    eax, 3
        sub    eax, [esp+arg_0]
        retn    4

@@4:                
        sub    eax, 4
        sub    eax, [esp+arg_0]
        retn    4

GetLabel    endp

end start

#define IDR_MENU 10000
#define ico 2001
#define IDD_Main 1000
#define IDC_GRP1 1001
#define IDC_FilePath 1002
#define IDC_Open 1003
#define IDC_STC2 1006
#define IDC_OutputName 1008
#define IDC_Start 1013
#define IDC_STC1 1004
#define IDC_STC3 1005
#define IDC_Label 1007
#define IDC_Save 1009
#define IDM_Context 10004
#define IDM_About 10002
#define IDM_Exit 10003
ico ICON DISCARDABLE "ico.ico"
1 MANIFEST DISCARDABLE "XPStyle.manifest"
IDD_Main DIALOGEX 6,5,208,64
CAPTION "Bin2Coff"
FONT 8,"MS Sans Serif",0,0
STYLE 0x10CE0800
BEGIN
  CONTROL "",IDC_GRP1,"Button",0x50000007,2,1,202,61
  CONTROL "",IDC_FilePath,"Edit",0x50010080,36,14,114,11,0x00000200
  CONTROL "浏 览",IDC_Open,"Button",0x50018000,158,14,38,11
  CONTROL "保存:",IDC_STC2,"Static",0x50000000,10,31,24,9
  CONTROL "",IDC_OutputName,"Edit",0x50010080,36,29,114,11,0x00000200
  CONTROL "转换",IDC_Start,"Button",0x50018000,158,44,38,11
  CONTROL "打开:",IDC_STC1,"Static",0x50000000,10,14,22,11
  CONTROL "标签:",IDC_STC3,"Static",0x50000000,10,46,22,13
  CONTROL "",IDC_Label,"Edit",0x50010000,36,44,114,11,0x00000200
  CONTROL "保存",IDC_Save,"Button",0x50018000,158,29,38,11
END
MyMenu MENUEX
BEGIN
  POPUP "File"
  BEGIN
    MENUITEM "说明",IDM_Context
    MENUITEM "关于",IDM_About
    MENUITEM "退出",IDM_Exit
  END
END

阅读(1471) | 评论(0) | 转发(0) |
0

上一篇:InlineHook in asm

下一篇:数据格式转换工具

给主人留下些什么吧!~~