-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoc_iocp.cr
55 lines (43 loc) · 1.53 KB
/
poc_iocp.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require "c/io"
require "c/fcntl"
require "c/fileapi"
require "c/sys/utime"
require "c/sys/stat"
require "c/winbase"
require "c/iocp"
require "./lib_windbg"
r = Random.new
comp_key = r.next_u
# puts "Completion Key: #{comp_key}"
io_port = LibC.CreateIoCompletionPort(LibC::INVALID_HANDLE_VALUE, nil, 0, 0)
path = "test.txt"
win_path = path.check_no_null_byte.to_utf16.to_unsafe
test_file_hnd = LibC.CreateFileW(
win_path,
LibC::GENERIC_READ | LibC::GENERIC_WRITE,
LibC::FILE_SHARE_READ,
nil,
LibC::CREATE_ALWAYS,
LibC::FILE_ATTRIBUTE_NORMAL | LibC::FILE_FLAG_OVERLAPPED,
LibC::HANDLE.null
)
fake_data = r.base64((2048) ** 2).to_slice
puts WinError.new(LibC.GetLastError)
if LibC.CreateIoCompletionPort(test_file_hnd, io_port, comp_key, 0) != LibC::INVALID_HANDLE_VALUE
3.times do
ol = AsyncOperation.new
fake_data = r.base64((1024) ** 2).to_slice
puts LibC.WriteFile(test_file_hnd, fake_data, fake_data.bytesize, out written, pointerof(ol))
puts WinError.new(LibC.GetLastError)
end
entries = Slice.new(3, LibC::OVERLAPPED_ENTRY.new)
# get_status = LibC.GetQueuedCompletionStatus(io_port, out bytes_trnsfred, out completed, pointerof(ol), LibC::INFINITE)
get_status = LibC.GetQueuedCompletionStatusEx(io_port, entries, 3, out removed, LibC::INFINITE, false)
puts entries[1].lpOverlapped.value
puts "GetQueuedCompletionStatus : #{get_status}"
puts WinError.new(LibC.GetLastError)
puts "Removed Entries: #{removed}"
end
if LibC.CloseHandle(test_file_hnd) == 0
raise RuntimeError.from_winerror("CloseHandle")
end