返回列表 回复 发帖

[原创] 带头开源 - CsrWalker_VB(源码)

[原创] 带头开源 - CsrWalker_VB(源码)

'版权声明: suanzi , iceboy(因为是ICY同学教我做的,也写上他名字)
'直接把ProcessView里的一个枚举进程模块copy出来 随便写的代码 很乱 不过我加了比较详细的注释了
'代码中我加了一处手脚 原因.. 有垃圾.. 垃圾在哪?
'提示一下 某行代码的传址传值我修改了 会的人一调试就知
'带上广告,附上我的ProcessView
  1. Option Explicit
  2. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
  3. Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
  4. Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
  5. Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
  6. Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hprocess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
  7. Private Declare Function CsrGetProcessId Lib "ntdll" () As Long '这是csrss的Pid

  8. Private Type CSR_PROCESS_PARTIAL
  9.     UniqueProcess As Long
  10.     UniqueThread As Long
  11.     Flink As Long
  12.     Blink As Long
  13. End Type


  14. Public Function EnumByCsrWalk() As Long()
  15.     Dim hprocess As Long, hModule As Long, addr As Long
  16.     Dim buff(0 To &H2F) As Byte, AddrCRP As Long, addr2 As Long
  17.     Dim cpp As CSR_PROCESS_PARTIAL, FirstFlink As Long
  18.     Dim Pids() As Long, I As Long

  19.     hprocess = IszOpenProcess(&H400 Or &H10, CsrGetProcessId()) '打开csrss需要SE_DEBUG
  20.     hModule = LoadLibrary("csrsrv.dll")
  21.     addr = GetProcAddress(hModule, "CsrLockProcessByClientId")  'CsrLockProcessByClientId的地址

  22.     CopyMemory buff(0), ByVal CLng(addr + &H8), &H30
  23.    
  24.     For I = 0 To UBound(buff) - 1   '搜索并定位CsrRootProcess
  25.         If buff(I) = &H8B And buff(I + 1) = &H35 Then  'CsrRootProcess在XP反汇编得的特征码是8B 35
  26.             CopyMemory AddrCRP, ByVal buff(I + 2), 4
  27.         End If
  28.     Next

  29.     ReadProcessMemory hprocess, ByVal AddrCRP, addr2, 4, ByVal 0& '读取AddrCRP指向的地址,这个地址指向一个结构
  30.     ReadProcessMemory hprocess, ByVal addr2, cpp, 16, ByVal 0&  '这里已经读取第1个了
  31.     FirstFlink = cpp.Flink '记录一下第1个 因为这是一个双向链表
  32.     ReDim Pids(0)
  33.     Do
  34.         ReadProcessMemory hprocess, ByVal cpp.Flink - 8, cpp, 16, ByVal 0& '下一个是Flink - 8
  35.         AddItemToArray cpp.UniqueProcess, Pids
  36.     Loop While cpp.Flink <> FirstFlink
  37.    
  38.     If (UBound(Pids) = 0) Then MsgBox "错误发生在_EnumByCsrWalk"
  39.     IcyClose hprocess
  40.     FreeLibrary hModule

  41.     EnumByCsrWalk = Pids
  42. End Function
复制代码
[ 本帖最后由 suanzi 于 2008-7-27 14:16 编辑 ]
附件: 您所在的用户组无法下载或查看附件
2

评分次数

  • dolphins

  • 炉子

cpp 这个变量名有意思

--

注释应该改一下:'记录下第一个, 因为这是一个循环链表'


[ 本帖最后由 icesboy 于 2008-7-27 14:44 编辑 ]
在 suanzi 同学的带头作用下, 丢一点儿代码:

psnull2\m_dos_device_trie.bas


  1. Option Explicit

  2. Private Const DataMask As Integer = 31744
  3. Private Const PointerMask As Integer = 1023

  4. Dim Trie(0 To 65535) As Integer, BlockCount As Long

  5. Private Sub ClearTrie()
  6.     BlockCount = 1
  7.     IcyZeroMemory VarPtr(Trie(0)), 64
  8. End Sub

  9. Private Function AddTrie(ByVal Parent As Integer, ByVal Index As Byte, ByVal Data As Byte) As Integer
  10.     Dim ParentOffset As Long, SubBlock As Integer
  11.     If Parent < BlockCount Then
  12.         Index = Index And CByte(31)
  13.         Data = Data And CByte(31)
  14.         ParentOffset = CLng(Parent) * 32 + CLng(Index)
  15.         If Data = 0 Then
  16.             SubBlock = Trie(ParentOffset) And PointerMask
  17.             If SubBlock = 0 Then
  18.                 IcyZeroMemory VarPtr(Trie(BlockCount * 32)), 64
  19.                 SubBlock = BlockCount
  20.                 Trie(ParentOffset) = Trie(ParentOffset) Or SubBlock
  21.                 BlockCount = BlockCount + 1
  22.                 If BlockCount >= 2048 Then
  23.                     MsgBox "Sorry, there are too many devices on your computer.", vbCritical
  24.                     End
  25.                 End If
  26.             End If
  27.             AddTrie = SubBlock
  28.         Else
  29.             Trie(ParentOffset) = Trie(ParentOffset) Or (Data * 1024)
  30.         End If
  31.     End If
  32. End Function

  33. Private Function QueryTrie(ByVal Parent As Integer, ByVal Index As Byte) As Integer
  34.     If Parent < BlockCount Then QueryTrie = Trie(CLng(Parent) * 32 + CLng(Index And 31))
  35. End Function

  36. Private Function SearchTrie(ByRef Buffer() As Byte) As Long
  37.     Dim pBlock As Integer, i As Long
  38.     Do While Buffer(i) <> 0
  39.         pBlock = QueryTrie(pBlock, Buffer(i))
  40.         If i > 1 Then
  41.             If (pBlock And DataMask) <> 0 Then
  42.                 Buffer(i - 2) = (pBlock \ 1024) Or 64
  43.                 Buffer(i - 1) = 58
  44.                 Buffer(i) = 92
  45.                 SearchTrie = i - 1
  46.                 Exit Function
  47.             End If
  48.         End If
  49.         i = i + 1
  50.         If i > UBound(Buffer) Then Exit Do
  51.     Loop
  52. End Function

  53. Public Function NtPathNameToDosName(ByVal NtName As String) As String
  54.     Dim NtNameAnsi() As Byte, i As Long
  55.     If NtName = Empty Then Exit Function
  56.     NtNameAnsi = StrConv(NtName, vbFromUnicode)
  57.     i = SearchTrie(NtNameAnsi)
  58.     If i = 0 Then
  59.         NtPathNameToDosName = NtName
  60.     Else
  61.         NtPathNameToDosName = Mid(StrConv(NtNameAnsi, vbUnicode), i)
  62.     End If
  63. End Function

  64. Public Sub RefreshDosDeviceNames()
  65.     Dim DriveInfo(0 To 8) As Long, DosName(0 To 2) As Byte, NtName(0 To 255) As Byte
  66.     Dim pDosName As Long, pNtName As Long, pBlock As Integer, i As Long
  67.     ClearTrie
  68.     IcyQueryInformationProcess -1, &H17, VarPtr(DriveInfo(0)), 36, 0
  69.     DosName(0) = 65
  70.     DosName(1) = 58
  71.     pDosName = VarPtr(DosName(0))
  72.     pNtName = VarPtr(NtName(0))
  73.     Do While DriveInfo(0) > 0
  74.         If (DriveInfo(0) And 1) = 1 Then
  75.             QueryDosDeviceA pDosName, pNtName, 1024
  76.             i = 0
  77.             pBlock = 0
  78.             Do While NtName(i) <> 0
  79.                 pBlock = AddTrie(pBlock, NtName(i), 0)
  80.                 i = i + 1
  81.             Loop
  82.             AddTrie pBlock, 92, DosName(0)
  83.         End If
  84.         DriveInfo(0) = DriveInfo(0) \ 2
  85.         DosName(0) = DosName(0) + 1
  86.     Loop
  87. End Sub
复制代码


[ 本帖最后由 icesboy 于 2008-7-27 14:53 编辑 ]
1

评分次数

  • yzsyf1996

叫圆圈链表吧 ^-^
dim cpp as CSR_PROCESS_PARTIAL
....懒人的变量名
此人已挂
看贴看回复的人就是好人
把楼顶的代码 code = replace(code, "ByVal buff(I + 2)", "buff(I + 2)")
因为这里是直接传址

over,over ...
1

评分次数

  • icesboy

iceboy同学发下声明可以吗?

回复 #6 329510010 的帖子

- -
全都发了你还要声明
我在想, 肯定有人想要利用楼主的代码, 来证明自己能检测到 phide_ex 了
哎~能详细点吗 太菜了 不会用
全部代码贴出来了 还要多详细
此人已挂
返回列表