1+ import os
12import unittest
2- from ptrlib .cpu import CPU
33from logging import getLogger , FATAL
4+ from ptrlib .cpu import CPU
5+
6+ _is_windows = os .name == 'nt'
47
58# TODO: Test AT&T syntax
69
7- const_gcc = '.byte 0; .word 1; .long 2; .quad 3; .asciz "/bin/sh"; .ascii "/bin/sh"'
8- const_gcc_bytes = \
10+ CONST_GCC = '.byte 0; .word 1; .long 2; .quad 3; .asciz "/bin/sh"; .ascii "/bin/sh"'
11+ CONST_GCC_BYTES = \
912 b'\x00 \x01 \x00 \x02 \x00 \x00 \x00 \x03 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' \
1013 b'/bin/sh\0 /bin/sh'
1114
12- const_nasm = 'db 0; dw 1; dd 2; dq 3; db "/bin/sh", 0; db "/bin/sh"'
13- const_nasm_bytes = \
15+ CONST_NASM = 'db 0; dw 1; dd 2; dq 3; db "/bin/sh", 0; db "/bin/sh"'
16+ CONST_NASM_BYTES = \
1417 b'\x00 \x01 \x00 \x02 \x00 \x00 \x00 \x03 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' \
1518 b'/bin/sh\0 /bin/sh'
1619
17- shellcode_32 = """
20+ SHELLCODE_32 = """
1821 push 0x30
1922 pop eax
2023 xor al, 0x30 // omit eax, ecx
2730 popad
2831 dec edx /* edx = 0xffffffff */
2932"""
30- shellcode_32_bytes = b'j0X40PPPPQPaJ'
33+ SHELLCODE_32_BYTES = b'j0X40PPPPQPaJ'
3134
32- shellcode_64 = """
35+ SHELLCODE_64 = """
3336 movabs rax, qword ptr gs:[0x188] // Prcb.CurrentThread
3437 mov rax, qword ptr [rax + 0xb8] // ApcState.Process
3538 mov rcx, rax
6366 swapgs
6467 sysretq
6568"""
66- shellcode_64_bytes = \
69+ SHELLCODE_64_BYTES = \
6770 b"\x65 \x48 \xa1 \x88 \x01 \x00 \x00 \x00 \x00 \x00 \x00 " \
6871 b"\x48 \x8b \x80 \xb8 \x00 \x00 \x00 " \
6972 b"\x48 \x89 \xc1 " \
9295 b"\x48 \x0f \x07 "
9396
9497class TestIntelAssembler (unittest .TestCase ):
98+ """Test Intel assembler
99+ """
95100 def setUp (self ):
96101 getLogger ("ptrlib" ).setLevel (FATAL )
97102
98103 def test_32bit_gcc (self ):
99104 """Test assembler for Intel 32-bit GCC
100105 """
106+ if _is_windows :
107+ return # Skip windows
108+
101109 cpu = CPU ('intel' , 32 )
102110 cpu .assembler = 'gcc'
103111
@@ -116,10 +124,10 @@ def test_32bit_gcc(self):
116124 self .assertEqual (cpu .assemble ('call far [esp]' ), b'\xff \x94 $\x06 \xff \x00 \x00 ' )
117125
118126 # Test long code
119- self .assertEqual (cpu .assemble (shellcode_32 ), shellcode_32_bytes )
127+ self .assertEqual (cpu .assemble (SHELLCODE_32 ), SHELLCODE_32_BYTES )
120128
121129 # Test constant
122- self .assertEqual (cpu .assemble (const_gcc ), const_gcc_bytes )
130+ self .assertEqual (cpu .assemble (CONST_GCC ), CONST_GCC_BYTES )
123131
124132 # Test exception
125133 with self .assertRaises (OSError ):
@@ -130,6 +138,9 @@ def test_32bit_gcc(self):
130138 def test_64bit_gcc (self ):
131139 """Test assembler for Intel 64-bit GCC
132140 """
141+ if _is_windows :
142+ return # Skip windows
143+
133144 cpu = CPU ('intel' , 64 )
134145 cpu .assembler = 'gcc'
135146
@@ -148,10 +159,10 @@ def test_64bit_gcc(self):
148159 self .assertEqual (cpu .assemble ('call far [rsp]' ), b'\xff \x94 $\x06 \xff \x00 \x00 ' )
149160
150161 # Test long code
151- self .assertEqual (cpu .assemble (shellcode_64 ), shellcode_64_bytes )
162+ self .assertEqual (cpu .assemble (SHELLCODE_64 ), SHELLCODE_64_BYTES )
152163
153164 # Test constant
154- self .assertEqual (cpu .assemble (const_gcc ), const_gcc_bytes )
165+ self .assertEqual (cpu .assemble (CONST_GCC ), CONST_GCC_BYTES )
155166
156167 # Test exception
157168 with self .assertRaises (OSError ):
@@ -179,10 +190,10 @@ def test_32bit_keystone(self):
179190 self .assertEqual (cpu .assemble ('A: /* infinite call */ call A' ), b'\xe8 \xfb \xff \xff \xff ' )
180191
181192 # Test long code
182- self .assertEqual (cpu .assemble (shellcode_32 ), shellcode_32_bytes )
193+ self .assertEqual (cpu .assemble (SHELLCODE_32 ), SHELLCODE_32_BYTES )
183194
184195 # Test constant
185- self .assertEqual (cpu .assemble (const_gcc ), const_gcc_bytes )
196+ self .assertEqual (cpu .assemble (CONST_GCC ), CONST_GCC_BYTES )
186197
187198 # Test exception
188199 with self .assertRaises (OSError ):
@@ -211,7 +222,7 @@ def test_64bit_keystone(self):
211222 self .assertEqual (cpu .assemble ('A: /* infinite call */ call A' ), b'\xe8 \xfb \xff \xff \xff ' )
212223
213224 # Test constant
214- self .assertEqual (cpu .assemble (const_gcc ), const_gcc_bytes )
225+ self .assertEqual (cpu .assemble (CONST_GCC ), CONST_GCC_BYTES )
215226
216227 # Test exception
217228 with self .assertRaises (OSError ):
@@ -222,6 +233,9 @@ def test_64bit_keystone(self):
222233 def test_32bit_nasm (self ):
223234 """Test assembler for Intel 32-bit nasm
224235 """
236+ if _is_windows :
237+ return # Skip windows
238+
225239 cpu = CPU ('intel' , 32 )
226240 cpu .assembler = 'nasm'
227241
@@ -238,10 +252,10 @@ def test_32bit_nasm(self):
238252 self .assertEqual (cpu .assemble ('A: /* infinite call */ call A' ), b'\xe8 \xfb \xff \xff \xff ' )
239253
240254 # Test long code
241- self .assertEqual (cpu .assemble (shellcode_32 ), shellcode_32_bytes )
255+ self .assertEqual (cpu .assemble (SHELLCODE_32 ), SHELLCODE_32_BYTES )
242256
243257 # Test constant
244- self .assertEqual (cpu .assemble (const_nasm ), const_nasm_bytes )
258+ self .assertEqual (cpu .assemble (CONST_NASM ), CONST_NASM_BYTES )
245259
246260 # Test exception
247261 with self .assertRaises (OSError ):
@@ -252,6 +266,9 @@ def test_32bit_nasm(self):
252266 def test_64bit_nasm (self ):
253267 """Test assembler for Intel 64-bit nasm
254268 """
269+ if _is_windows :
270+ return # Skip windows
271+
255272 cpu = CPU ('intel' , 64 )
256273 cpu .assembler = 'nasm'
257274
@@ -269,7 +286,7 @@ def test_64bit_nasm(self):
269286 self .assertEqual (cpu .assemble ('A: /* infinite call */ call A' ), b'\xe8 \xfb \xff \xff \xff ' )
270287
271288 # Test constant
272- self .assertEqual (cpu .assemble (const_nasm ), const_nasm_bytes )
289+ self .assertEqual (cpu .assemble (CONST_NASM ), CONST_NASM_BYTES )
273290
274291 # Test exception
275292 with self .assertRaises (OSError ):
0 commit comments