Skip to content

Commit c12a0c4

Browse files
committed
Add windows.system + ALPC + RPC + winproxy demo to README.md
1 parent 5ee74d5 commit c12a0c4

2 files changed

Lines changed: 124 additions & 2 deletions

File tree

README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ You can also make some operation on threads (suspend/resume/wait/get(or set) con
3535
>>> import windows
3636
>>> windows.current_process.bitness
3737
32
38+
>>> windows.current_process.token.integrity
39+
SECURITY_MANDATORY_MEDIUM_RID(0x2000L)
3840
>>> calc = [p for p in windows.system.processes if p.name == "calc.exe"][0]
3941
>>> calc
4042
<WinProcess "calc.exe" pid 6960 at 0x37391f0>
@@ -62,6 +64,34 @@ WindowsError: <WinProcess "calc.exe" pid 6960 (DEAD) at 0x37391f0> died during e
6264
6961L
6365
```
6466

67+
### System information
68+
69+
Information about the Windows computer running the script are available through the `windows.system` object.
70+
71+
```python
72+
>>> windows.system
73+
<windows.winobject.system.System object at 0x03FEED10>
74+
>>> windows.system.bitness
75+
64
76+
>>> windows.system.computer_name
77+
'DESKTOP-VKUGISR'
78+
>>> windows.system.product_type
79+
VER_NT_WORKSTATION(0x1L)
80+
>>> windows.system.version
81+
(10, 0)
82+
>>> windows.system.version_name
83+
'Windows 10'
84+
>>> windows.system.build_number
85+
'10.0.15063.608'
86+
87+
# windows.system also contains dynamic lists about processes / threads / handles / ...
88+
>>> windows.system.handles[-2:]
89+
[<Handle value=<0x5cc> in process pid=14360>, <Handle value=<0x28e4> in process pid=14360>]
90+
>>> windows.system.processes[:2]
91+
[<WinProcess "[System Process]" pid 0 at 0x433f7d0>, <WinProcess "System" pid 4 at 0x433fd30>]
92+
>>> windows.system.logicaldrives[0]
93+
<LogicalDrive "C:\" (DRIVE_FIXED)>
94+
```
6595

6696
### IAT Hook
6797

@@ -74,6 +104,49 @@ So the features is present (See [online documentation][ONLINE_IATHOOK] about IAT
74104
A wrapper around some Windows functions. Arguments name and order are the same,
75105
but some have default values and the functions raise exception on call error (I don't like 'if' around all my call).
76106

107+
```python
108+
>>> import windows
109+
>>> help(windows.winproxy.VirtualAlloc)
110+
# Help on function VirtualAlloc in module windows.winproxy:
111+
# VirtualAlloc(lpAddress=0, dwSize=NeededParameter, flAllocationType=MEM_COMMIT(0x1000L), flProtect=PAGE_EXECUTE_READWRITE(0x40L))
112+
# Errcheck:
113+
# raise Kernel32Error if result is 0
114+
115+
# Positional arguments
116+
>>> windows.winproxy.VirtualAlloc(0, 0x1000)
117+
34537472
118+
119+
# Keyword arguments
120+
>>> windows.winproxy.VirtualAlloc(dwSize=0x1000)
121+
34603008
122+
123+
# NeededParameter must be provided
124+
>>> windows.winproxy.VirtualAlloc()
125+
"""
126+
Traceback (most recent call last):
127+
File "<stdin>", line 1, in <module>
128+
File "windows\winproxy.py", line 264, in VirtualAlloc
129+
return VirtualAlloc.ctypes_function(lpAddress, dwSize, flAllocationType, flProtect)
130+
File "windows\winproxy.py", line 130, in perform_call
131+
raise TypeError("{0}: Missing Mandatory parameter <{1}>".format(self.func_name, param_name))
132+
TypeError: VirtualAlloc: Missing Mandatory parameter <dwSize>
133+
"""
134+
135+
# Error raises exception
136+
>>> windows.winproxy.VirtualAlloc(dwSize=0xffffffff)
137+
"""
138+
Traceback (most recent call last):
139+
File "<stdin>", line 1, in <module>
140+
File "windows\winproxy.py", line 264, in VirtualAlloc
141+
return VirtualAlloc.ctypes_function(lpAddress, dwSize, flAllocationType, flProtect)
142+
File "windows\winproxy.py", line 133, in perform_call
143+
return self._cprototyped(*args)
144+
File "windows\winproxy.py", line 59, in kernel32_error_check
145+
raise Kernel32Error(func_name)
146+
windows.winproxy.Kernel32Error: VirtualAlloc: [Error 8] Not enough storage is available to process this command.
147+
"""
148+
```
149+
77150

78151
### Native execution
79152

@@ -158,6 +231,55 @@ KeyValue(name='MYQWORD', value=123456789987654321L, type=11)
158231
[KeyValue(name='MYQWORD', value=123456789987654321L, type=11), KeyValue(name='VALUE', value=u'a_value_for_my_key', type=1)]
159232
```
160233

234+
### ALPC-RPC
235+
236+
#### ALPC
237+
238+
Classes around **A**dvanced **L**ocal **P**rocedure **C**all (**ALPC**) syscalls allows to simply
239+
write client and server able to send **ALPC** messages.
240+
241+
```python
242+
>>> import windows.alpc
243+
# Test server juste reply to each message with "REQUEST '{msg_data}' RECEIVED"
244+
>>> client = windows.alpc.AlpcClient(r"\RPC Control\PythonForWindowsTESTPORT")
245+
>>> response = client.send_receive("Hello world !")
246+
>>> response
247+
<windows.alpc.AlpcMessage object at 0x04C0D5D0>
248+
>>> response.data
249+
"REQUEST 'Hello world !' RECEIVED"
250+
```
251+
252+
Full client/server code for this example is available is the [ALPC samples][ONLINE_SAMPLE_ALPC] along with a more complex example.
253+
254+
255+
#### RPC
256+
257+
An RPC-Client based using **ALPC** communication is also integred
258+
259+
```python
260+
# Server (port ALPC '\RPC Control\HelloRpc') offers:
261+
# Interface '41414141-4242-4343-4444-45464748494a' version 1.0
262+
# Method 1 -> int Add(int a, int b) -> return a + b
263+
# This Test server is a real RPC Server using rpcrt4.dll and compiled with VS2015.
264+
265+
>>> import windows.rpc
266+
>>> from windows.rpc import ndr
267+
>>> client = windows.rpc.RPCClient(r"\RPC Control\HelloRpc")
268+
>>> client
269+
<windows.rpc.client.RPCClient object at 0x0411E130>
270+
>>> iid = client.bind("41414141-4242-4343-4444-45464748494a")
271+
>>> ndr_params = ndr.make_parameters([ndr.NdrLong] * 2)
272+
# NDR pack + Make RPC call to method 1.
273+
>>> resp = client.call(iid, 1, ndr_params.pack([41414141, 1010101]))
274+
# Unpack the NDR response
275+
>>> result = ndr.NdrLong.unpack(ndr.NdrStream(resp))
276+
>>> result
277+
42424242
278+
```
279+
280+
A sample with the **U**ser **A**ccount **C**ontrol (**UAC**) and one with `lsasrv.dll` are available in the [RPC samples][ONLINE_SAMPLE_RPC].
281+
282+
161283
### Debugger
162284

163285
PythonForWindows provides a standard debugger to debug other processes.
@@ -271,4 +393,6 @@ The local debugger handles
271393
[SAMPLE_DIR]: https://github.com/hakril/PythonForWindows/tree/master/samples
272394
[ONLINE_DOC]: http://hakril.github.io/PythonForWindows/
273395
[ONLINE_SAMPLE]: http://hakril.github.io/PythonForWindows/build/html/sample.html
396+
[ONLINE_SAMPLE_ALPC]: http://hakril.github.io/PythonForWindows/build/html/sample.html#windows-alpc
397+
[ONLINE_SAMPLE_RPC]: http://hakril.github.io/PythonForWindows/build/html/sample.html#windows-rpc
274398
[ONLINE_IATHOOK]: http://hakril.github.io/PythonForWindows/build/html/iat_hook.html

TODO

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ TODO:
3131

3232
- Some test/doc on windows.system.handles
3333

34-
- registry
35-
- test !
3634

3735
- wintrust doc:
3836
add : https://blogs.msdn.microsoft.com/winsdk/2016/01/05/why-cryptcatadmincalchashfromfilehandle-fails-with-a-seemingly-unexpected-error-code/

0 commit comments

Comments
 (0)