pwntools 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +88 -11
  3. data/Rakefile +5 -1
  4. data/lib/pwn.rb +9 -7
  5. data/lib/pwnlib/abi.rb +60 -0
  6. data/lib/pwnlib/asm.rb +146 -0
  7. data/lib/pwnlib/constants/constant.rb +16 -2
  8. data/lib/pwnlib/constants/constants.rb +35 -19
  9. data/lib/pwnlib/constants/linux/amd64.rb +30 -1
  10. data/lib/pwnlib/context.rb +25 -17
  11. data/lib/pwnlib/dynelf.rb +117 -54
  12. data/lib/pwnlib/elf/elf.rb +267 -0
  13. data/lib/pwnlib/ext/helper.rb +4 -4
  14. data/lib/pwnlib/logger.rb +87 -0
  15. data/lib/pwnlib/memleak.rb +58 -29
  16. data/lib/pwnlib/pwn.rb +19 -8
  17. data/lib/pwnlib/reg_sort.rb +102 -108
  18. data/lib/pwnlib/shellcraft/generators/amd64/common/common.rb +14 -0
  19. data/lib/pwnlib/shellcraft/generators/amd64/common/infloop.rb +17 -0
  20. data/lib/pwnlib/shellcraft/generators/amd64/common/memcpy.rb +31 -0
  21. data/lib/pwnlib/shellcraft/generators/amd64/common/mov.rb +127 -0
  22. data/lib/pwnlib/shellcraft/generators/amd64/common/nop.rb +16 -0
  23. data/lib/pwnlib/shellcraft/generators/amd64/common/popad.rb +27 -0
  24. data/lib/pwnlib/shellcraft/generators/amd64/common/pushstr.rb +64 -0
  25. data/lib/pwnlib/shellcraft/generators/amd64/common/pushstr_array.rb +19 -0
  26. data/lib/pwnlib/shellcraft/generators/amd64/common/ret.rb +32 -0
  27. data/lib/pwnlib/shellcraft/generators/amd64/common/setregs.rb +19 -0
  28. data/lib/pwnlib/shellcraft/generators/amd64/linux/execve.rb +21 -0
  29. data/lib/pwnlib/shellcraft/generators/amd64/linux/linux.rb +14 -0
  30. data/lib/pwnlib/shellcraft/generators/amd64/linux/ls.rb +19 -0
  31. data/lib/pwnlib/shellcraft/generators/amd64/linux/sh.rb +19 -0
  32. data/lib/pwnlib/shellcraft/generators/amd64/linux/syscall.rb +21 -0
  33. data/lib/pwnlib/shellcraft/generators/helper.rb +106 -0
  34. data/lib/pwnlib/shellcraft/generators/i386/common/common.rb +14 -0
  35. data/lib/pwnlib/shellcraft/generators/i386/common/infloop.rb +17 -0
  36. data/lib/pwnlib/shellcraft/generators/i386/common/mov.rb +90 -0
  37. data/lib/pwnlib/shellcraft/generators/i386/common/nop.rb +16 -0
  38. data/lib/pwnlib/shellcraft/generators/i386/common/pushstr.rb +39 -0
  39. data/lib/pwnlib/shellcraft/generators/i386/common/pushstr_array.rb +19 -0
  40. data/lib/pwnlib/shellcraft/generators/i386/common/setregs.rb +19 -0
  41. data/lib/pwnlib/shellcraft/generators/i386/linux/execve.rb +19 -0
  42. data/lib/pwnlib/shellcraft/generators/i386/linux/linux.rb +14 -0
  43. data/lib/pwnlib/shellcraft/generators/i386/linux/ls.rb +19 -0
  44. data/lib/pwnlib/shellcraft/generators/i386/linux/sh.rb +19 -0
  45. data/lib/pwnlib/shellcraft/generators/i386/linux/syscall.rb +19 -0
  46. data/lib/pwnlib/shellcraft/generators/x86/common/common.rb +26 -0
  47. data/lib/pwnlib/shellcraft/generators/x86/common/infloop.rb +22 -0
  48. data/lib/pwnlib/shellcraft/generators/x86/common/mov.rb +15 -0
  49. data/lib/pwnlib/shellcraft/generators/x86/common/pushstr.rb +15 -0
  50. data/lib/pwnlib/shellcraft/generators/x86/common/pushstr_array.rb +85 -0
  51. data/lib/pwnlib/shellcraft/generators/x86/common/setregs.rb +82 -0
  52. data/lib/pwnlib/shellcraft/generators/x86/linux/execve.rb +69 -0
  53. data/lib/pwnlib/shellcraft/generators/x86/linux/linux.rb +14 -0
  54. data/lib/pwnlib/shellcraft/generators/x86/linux/ls.rb +66 -0
  55. data/lib/pwnlib/shellcraft/generators/x86/linux/sh.rb +52 -0
  56. data/lib/pwnlib/shellcraft/generators/x86/linux/syscall.rb +52 -0
  57. data/lib/pwnlib/shellcraft/registers.rb +145 -0
  58. data/lib/pwnlib/shellcraft/shellcraft.rb +67 -0
  59. data/lib/pwnlib/timer.rb +60 -0
  60. data/lib/pwnlib/tubes/buffer.rb +96 -0
  61. data/lib/pwnlib/tubes/sock.rb +95 -0
  62. data/lib/pwnlib/tubes/tube.rb +270 -0
  63. data/lib/pwnlib/util/cyclic.rb +95 -94
  64. data/lib/pwnlib/util/fiddling.rb +256 -220
  65. data/lib/pwnlib/util/getdents.rb +83 -0
  66. data/lib/pwnlib/util/hexdump.rb +109 -108
  67. data/lib/pwnlib/util/lists.rb +55 -0
  68. data/lib/pwnlib/util/packing.rb +226 -228
  69. data/lib/pwnlib/util/ruby.rb +18 -0
  70. data/lib/pwnlib/version.rb +2 -1
  71. data/test/abi_test.rb +21 -0
  72. data/test/asm_test.rb +104 -0
  73. data/test/constants/constant_test.rb +1 -0
  74. data/test/constants/constants_test.rb +4 -2
  75. data/test/context_test.rb +1 -0
  76. data/test/data/echo.rb +20 -0
  77. data/test/data/elfs/Makefile +22 -0
  78. data/test/data/elfs/amd64.frelro.elf +0 -0
  79. data/test/data/elfs/amd64.frelro.pie.elf +0 -0
  80. data/test/data/elfs/amd64.nrelro.elf +0 -0
  81. data/test/data/elfs/amd64.prelro.elf +0 -0
  82. data/test/data/elfs/i386.frelro.pie.elf +0 -0
  83. data/test/data/elfs/i386.prelro.elf +0 -0
  84. data/test/data/elfs/source.cpp +19 -0
  85. data/test/data/flag +1 -0
  86. data/test/data/lib32/ld.so.2 +0 -0
  87. data/test/data/lib32/libc.so.6 +0 -0
  88. data/test/data/lib64/ld.so.2 +0 -0
  89. data/test/data/lib64/libc.so.6 +0 -0
  90. data/test/dynelf_test.rb +59 -24
  91. data/test/elf/elf_test.rb +120 -0
  92. data/test/ext_test.rb +3 -2
  93. data/test/files/use_pwnlib.rb +1 -1
  94. data/test/logger_test.rb +61 -0
  95. data/test/memleak_test.rb +4 -33
  96. data/test/reg_sort_test.rb +3 -1
  97. data/test/shellcraft/infloop_test.rb +26 -0
  98. data/test/shellcraft/linux/ls_test.rb +108 -0
  99. data/test/shellcraft/linux/sh_test.rb +119 -0
  100. data/test/shellcraft/linux/syscalls/execve_test.rb +136 -0
  101. data/test/shellcraft/linux/syscalls/syscall_test.rb +83 -0
  102. data/test/shellcraft/memcpy_test.rb +35 -0
  103. data/test/shellcraft/mov_test.rb +98 -0
  104. data/test/shellcraft/nop_test.rb +26 -0
  105. data/test/shellcraft/popad_test.rb +29 -0
  106. data/test/shellcraft/pushstr_array_test.rb +91 -0
  107. data/test/shellcraft/pushstr_test.rb +108 -0
  108. data/test/shellcraft/registers_test.rb +32 -0
  109. data/test/shellcraft/ret_test.rb +30 -0
  110. data/test/shellcraft/setregs_test.rb +62 -0
  111. data/test/shellcraft/shellcraft_test.rb +28 -0
  112. data/test/test_helper.rb +12 -1
  113. data/test/timer_test.rb +23 -0
  114. data/test/tubes/buffer_test.rb +45 -0
  115. data/test/tubes/sock_test.rb +68 -0
  116. data/test/tubes/tube_test.rb +241 -0
  117. data/test/util/cyclic_test.rb +2 -1
  118. data/test/util/fiddling_test.rb +2 -1
  119. data/test/util/getdents_test.rb +32 -0
  120. data/test/util/hexdump_test.rb +7 -9
  121. data/test/util/lists_test.rb +21 -0
  122. data/test/util/packing_test.rb +4 -3
  123. metadata +215 -25
@@ -0,0 +1,83 @@
1
+ # encoding: ASCII-8BIT
2
+
3
+ require 'test_helper'
4
+
5
+ require 'pwnlib/context'
6
+ require 'pwnlib/shellcraft/shellcraft'
7
+
8
+ class SyscallTest < MiniTest::Test
9
+ include ::Pwnlib::Context
10
+
11
+ def setup
12
+ @shellcraft = ::Pwnlib::Shellcraft::Shellcraft.instance
13
+ end
14
+
15
+ def test_amd64
16
+ context.local(arch: 'amd64') do
17
+ assert_equal(<<-'EOS', @shellcraft.syscall('SYS_execve', 1, 'rsp', 2, 0))
18
+ /* call execve(1, "rsp", 2, 0) */
19
+ push 0x3b /* (SYS_execve) */
20
+ pop rax
21
+ push 1
22
+ pop rdi
23
+ mov rsi, rsp
24
+ push 2
25
+ pop rdx
26
+ xor r10d, r10d /* 0 */
27
+ syscall
28
+ EOS
29
+ assert_equal(<<-'EOS', @shellcraft.syscall)
30
+ /* call syscall() */
31
+ syscall
32
+ EOS
33
+ assert_equal(<<-'EOS', @shellcraft.syscall('rax', 'rdi', 'rsi'))
34
+ /* call syscall("rax", "rdi", "rsi") */
35
+ /* setregs noop */
36
+ syscall
37
+ EOS
38
+ assert_equal(<<-'EOS', @shellcraft.syscall('rbp', nil, nil, 1))
39
+ /* call syscall("rbp", ?, ?, 1) */
40
+ mov rax, rbp
41
+ push 1
42
+ pop rdx
43
+ syscall
44
+ EOS
45
+ mmap = @shellcraft.syscall('SYS_mmap', 0, 4096,
46
+ 'PROT_READ | PROT_WRITE | PROT_EXEC',
47
+ 'MAP_PRIVATE | MAP_ANONYMOUS', -1, 0)
48
+ assert_equal(<<-'EOS', mmap)
49
+ /* call mmap(0, 4096, "PROT_READ | PROT_WRITE | PROT_EXEC", "MAP_PRIVATE | MAP_ANONYMOUS", -1, 0) */
50
+ push 9 /* (SYS_mmap) */
51
+ pop rax
52
+ xor edi, edi /* 0 */
53
+ mov esi, 0x1010101
54
+ xor esi, 0x1011101 /* 0x1000 == 0x1010101 ^ 0x1011101 */
55
+ push 7 /* (PROT_READ | PROT_WRITE | PROT_EXEC) */
56
+ pop rdx
57
+ push 0x22 /* (MAP_PRIVATE | MAP_ANONYMOUS) */
58
+ pop r10
59
+ push -1
60
+ pop r8
61
+ xor r9d, r9d /* 0 */
62
+ syscall
63
+ EOS
64
+ end
65
+ end
66
+
67
+ def test_i386
68
+ context.local(arch: 'i386') do
69
+ assert_equal(<<-'EOS', @shellcraft.syscall('ebp', nil, nil, 1))
70
+ /* call syscall("ebp", ?, ?, 1) */
71
+ mov eax, ebp
72
+ push 1
73
+ pop edx
74
+ int 0x80
75
+ EOS
76
+ assert_equal(<<-'EOS', @shellcraft.syscall('eax', 'ebx', 'ecx'))
77
+ /* call syscall("eax", "ebx", "ecx") */
78
+ /* setregs noop */
79
+ int 0x80
80
+ EOS
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: ASCII-8BIT
2
+
3
+ require 'test_helper'
4
+
5
+ require 'pwnlib/context'
6
+ require 'pwnlib/shellcraft/shellcraft'
7
+
8
+ class MemcpyTest < MiniTest::Test
9
+ include ::Pwnlib::Context
10
+
11
+ def setup
12
+ @shellcraft = ::Pwnlib::Shellcraft::Shellcraft.instance
13
+ end
14
+
15
+ def test_amd64
16
+ context.local(arch: 'amd64') do
17
+ assert_equal(<<-'EOS', @shellcraft.memcpy('rdi', 'rbx', 255))
18
+ /* memcpy("rdi", "rbx", 0xff) */
19
+ cld
20
+ mov rsi, rbx
21
+ xor ecx, ecx
22
+ mov cl, 0xff
23
+ rep movsb
24
+ EOS
25
+ assert_equal(<<-'EOS', @shellcraft.memcpy('rdi', 'rbx', 255))
26
+ /* memcpy("rdi", "rbx", 0xff) */
27
+ cld
28
+ mov rsi, rbx
29
+ xor ecx, ecx
30
+ mov cl, 0xff
31
+ rep movsb
32
+ EOS
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,98 @@
1
+ # encoding: ASCII-8BIT
2
+
3
+ require 'test_helper'
4
+
5
+ require 'pwnlib/context'
6
+ require 'pwnlib/shellcraft/shellcraft'
7
+
8
+ class MovTest < MiniTest::Test
9
+ include ::Pwnlib::Context
10
+
11
+ def setup
12
+ @shellcraft = ::Pwnlib::Shellcraft::Shellcraft.instance
13
+ end
14
+
15
+ def test_amd64
16
+ context.local(arch: 'amd64') do
17
+ assert_equal(" xor eax, eax /* 0 */\n", @shellcraft.mov('rax', 0))
18
+ assert_equal(" /* moving rax into rax, but this is a no-op */\n", @shellcraft.mov('rax', 'rax'))
19
+ assert_equal(" push 9 /* mov eax, '\\n' */\n pop rax\n inc eax\n", @shellcraft.mov('rax', 10))
20
+ assert_equal(" xor eax, eax\n mov al, 0xc0\n", @shellcraft.mov('rax', 0xc0))
21
+ assert_equal(" xor eax, eax\n mov ax, 0xc0c0\n", @shellcraft.mov('rax', 0xc0c0))
22
+ assert_equal(" xor ebx, ebx\n mov bh, 0x100 >> 8\n", @shellcraft.mov('ebx', 0x100))
23
+ assert_equal(<<-'EOS', @shellcraft.mov('rdi', 0x100))
24
+ mov edi, 0x1010201
25
+ xor edi, 0x1010301 /* 0x100 == 0x1010201 ^ 0x1010301 */
26
+ EOS
27
+ assert_equal(" mov r15d, 0xffffffff\n", @shellcraft.mov('r15', 0xffffffff))
28
+ assert_equal(" push -1\n pop rsi\n", @shellcraft.mov('rsi', -1))
29
+ assert_equal(" mov esi, -1\n", @shellcraft.mov('rsi', -1, stack_allowed: false))
30
+ assert_equal(" movzx edi, ax\n", @shellcraft.mov('rdi', 'ax'))
31
+ assert_equal(" mov rdx, rbx\n", @shellcraft.mov('rdx', 'rbx'))
32
+ assert_equal(" xor eax, eax /* (SYS_read) */\n", @shellcraft.mov('rax', 'SYS_read'))
33
+ assert_equal(" push 1 /* (SYS_write) */\n pop rax\n", @shellcraft.mov('eax', 'SYS_write'))
34
+ assert_equal(" xor ax, ax\n mov al, 1 /* (SYS_write) */\n", @shellcraft.mov('ax', 'SYS_write'))
35
+ assert_equal(" /* moving ax into al, but this is a no-op */\n", @shellcraft.mov('al', 'ax'))
36
+ assert_equal(<<-'EOS', @shellcraft.mov('rax', 0x11dead00ff))
37
+ mov rax, 0x101010101010101
38
+ push rax
39
+ mov rax, 0x1010110dfac01fe
40
+ xor [rsp], rax /* 0x11dead00ff == 0x101010101010101 ^ 0x1010110dfac01fe */
41
+ pop rax
42
+ EOS
43
+ # raises
44
+ err = assert_raises(ArgumentError) { @shellcraft.mov('eax', 'rdx') }
45
+ assert_equal('cannot mov eax, rdx: dst is smaller than src', err.message)
46
+ err = assert_raises(ArgumentError) { @shellcraft.mov('rcx', 0x7f00000000, stack_allowed: false) }
47
+ assert_equal('Cannot put 0x7f00000000 into \'rcx\' without using stack.', err.message)
48
+ end
49
+ end
50
+
51
+ def test_i386
52
+ context.local(arch: 'i386') do
53
+ assert_equal(" mov eax, ebx\n", @shellcraft.mov('eax', 'ebx'))
54
+ assert_equal(" xor eax, eax /* 0 */\n", @shellcraft.mov('eax', 0))
55
+ assert_equal(" xor ax, ax /* 0 */\n", @shellcraft.mov('ax', 0))
56
+ assert_equal(" xor ax, ax\n mov al, 0x11\n", @shellcraft.mov('ax', 17))
57
+ assert_equal(<<-EOS, @shellcraft.mov('edi', 10))
58
+ push 9 /* mov edi, '\\n' */
59
+ pop edi
60
+ inc edi
61
+ EOS
62
+ assert_equal(" /* moving ax into al, but this is a no-op */\n", @shellcraft.mov('al', 'ax'))
63
+ assert_equal(" /* moving esp into esp, but this is a no-op */\n", @shellcraft.mov('esp', 'esp'))
64
+ assert_equal(" movzx ax, bl\n", @shellcraft.mov('ax', 'bl'))
65
+ assert_equal(" push 1\n pop eax\n", @shellcraft.mov('eax', 1))
66
+ assert_equal(" xor eax, eax\n mov al, 1\n", @shellcraft.mov('eax', 1, stack_allowed: false))
67
+ assert_equal(" mov eax, 0xdeadbeaf\n", @shellcraft.mov('eax', 0xdeadbeaf))
68
+ assert_equal(" mov eax, -0xdead00ff\n neg eax\n", @shellcraft.mov('eax', 0xdead00ff))
69
+ assert_equal(" xor eax, eax\n mov al, 0xc0\n", @shellcraft.mov('eax', 0xc0))
70
+ assert_equal(" mov edi, -0xc0\n neg edi\n", @shellcraft.mov('edi', 0xc0))
71
+ assert_equal(" xor eax, eax\n mov ah, 0xc000 >> 8\n", @shellcraft.mov('eax', 0xc000))
72
+ assert_equal(<<-EOS, @shellcraft.mov('eax', 0xffc000))
73
+ mov eax, 0x1010101
74
+ xor eax, 0x1fec101 /* 0xffc000 == 0x1010101 ^ 0x1fec101 */
75
+ EOS
76
+ assert_equal(<<-EOS, @shellcraft.mov('edi', 0xc000))
77
+ mov edi, (-1) ^ 0xc000
78
+ not edi
79
+ EOS
80
+ assert_equal(<<-EOS, @shellcraft.mov('edi', 0xf500))
81
+ mov edi, 0x1010101
82
+ xor edi, 0x101f401 /* 0xf500 == 0x1010101 ^ 0x101f401 */
83
+ EOS
84
+ assert_equal(" xor eax, eax\n mov ax, 0xc0c0\n", @shellcraft.mov('eax', 0xc0c0))
85
+ assert_equal(<<-EOS, @shellcraft.mov('eax', 'SYS_execve'))
86
+ push 0xb /* (SYS_execve) */
87
+ pop eax
88
+ EOS
89
+ assert_equal(<<-EOS, @shellcraft.mov('eax', 'PROT_READ | PROT_WRITE | PROT_EXEC'))
90
+ push 7 /* (PROT_READ | PROT_WRITE | PROT_EXEC) */
91
+ pop eax
92
+ EOS
93
+ # raises
94
+ err = assert_raises(ArgumentError) { @shellcraft.mov('ax', 'ebx') }
95
+ assert_equal('cannot mov ax, ebx: dst is smaller than src', err.message)
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: ASCII-8BIT
2
+
3
+ require 'test_helper'
4
+
5
+ require 'pwnlib/context'
6
+ require 'pwnlib/shellcraft/shellcraft'
7
+
8
+ class NopTest < MiniTest::Test
9
+ include ::Pwnlib::Context
10
+
11
+ def setup
12
+ @shellcraft = ::Pwnlib::Shellcraft::Shellcraft.instance
13
+ end
14
+
15
+ def test_amd64
16
+ context.local(arch: 'amd64') do
17
+ assert_equal(" nop\n", @shellcraft.nop)
18
+ end
19
+ end
20
+
21
+ def test_i386
22
+ context.local(arch: 'i386') do
23
+ assert_equal(" nop\n", @shellcraft.nop)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: ASCII-8BIT
2
+
3
+ require 'test_helper'
4
+
5
+ require 'pwnlib/context'
6
+ require 'pwnlib/shellcraft/shellcraft'
7
+
8
+ class PopadTest < MiniTest::Test
9
+ include ::Pwnlib::Context
10
+
11
+ def setup
12
+ @shellcraft = ::Pwnlib::Shellcraft::Shellcraft.instance
13
+ end
14
+
15
+ def test_amd64
16
+ context.local(arch: 'amd64') do
17
+ assert_equal(<<-'EOS', @shellcraft.popad)
18
+ pop rdi
19
+ pop rsi
20
+ pop rbp
21
+ pop rbx /* add rsp, 8 */
22
+ pop rbx
23
+ pop rdx
24
+ pop rcx
25
+ pop rax
26
+ EOS
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,91 @@
1
+ # encoding: ASCII-8BIT
2
+
3
+ require 'test_helper'
4
+
5
+ require 'pwnlib/context'
6
+ require 'pwnlib/shellcraft/shellcraft'
7
+
8
+ class PushstrArrayTest < MiniTest::Test
9
+ include ::Pwnlib::Context
10
+
11
+ def setup
12
+ @shellcraft = ::Pwnlib::Shellcraft::Shellcraft.instance
13
+ end
14
+
15
+ def test_amd64
16
+ context.local(arch: 'amd64') do
17
+ assert_equal(<<-'EOS', @shellcraft.pushstr_array('rcx', ['A']))
18
+ /* push argument array ["A\x00"] */
19
+ /* push "A\x00" */
20
+ push 0x41
21
+ xor ecx, ecx /* 0 */
22
+ push rcx /* null terminate */
23
+ push 8
24
+ pop rcx
25
+ add rcx, rsp
26
+ push rcx /* "A\x00" */
27
+ mov rcx, rsp
28
+ EOS
29
+ assert_equal(<<-'EOS', @shellcraft.pushstr_array('rsp', ['sh', '-c', 'echo pusheen']))
30
+ /* push argument array ["sh\x00", "-c\x00", "echo pusheen\x00"] */
31
+ /* push "sh\x00-c\x00echo pusheen\x00" */
32
+ push 0x1010101 ^ 0x6e65
33
+ xor dword ptr [rsp], 0x1010101
34
+ mov rax, 0x6568737570206f68
35
+ push rax
36
+ mov rax, 0x101010101010101
37
+ push rax
38
+ mov rax, 0x626401622c016972 /* 0x101010101010101 ^ 0x636500632d006873 */
39
+ xor [rsp], rax
40
+ xor esp, esp /* 0 */
41
+ push rsp /* null terminate */
42
+ push 0xe
43
+ pop rsp
44
+ add rsp, rsp
45
+ push rsp /* "echo pusheen\x00" */
46
+ push 0x13
47
+ pop rsp
48
+ add rsp, rsp
49
+ push rsp /* "-c\x00" */
50
+ push 0x18
51
+ pop rsp
52
+ add rsp, rsp
53
+ push rsp /* "sh\x00" */
54
+ /* moving rsp into rsp, but this is a no-op */
55
+ EOS
56
+ end
57
+ end
58
+
59
+ def test_i386
60
+ context.local(arch: 'i386') do
61
+ assert_equal(<<-'EOS', @shellcraft.pushstr_array('esp', ['sh', '-c', 'echo pusheen']))
62
+ /* push argument array ["sh\x00", "-c\x00", "echo pusheen\x00"] */
63
+ /* push "sh\x00-c\x00echo pusheen\x00" */
64
+ push 0x1010101
65
+ xor dword ptr [esp], 0x1016f64 /* 0x1010101 ^ 0x6e65 */
66
+ push 0x65687375
67
+ push 0x70206f68
68
+ push 0x1010101
69
+ xor dword ptr [esp], 0x62640162 /* 0x1010101 ^ 0x63650063 */
70
+ push 0x1010101
71
+ xor dword ptr [esp], 0x2c016972 /* 0x1010101 ^ 0x2d006873 */
72
+ xor esp, esp /* 0 */
73
+ push esp /* null terminate */
74
+ push 9 /* mov esp, '\n' */
75
+ pop esp
76
+ inc esp
77
+ add esp, esp
78
+ push esp /* "echo pusheen\x00" */
79
+ push 0xb
80
+ pop esp
81
+ add esp, esp
82
+ push esp /* "-c\x00" */
83
+ push 0xc
84
+ pop esp
85
+ add esp, esp
86
+ push esp /* "sh\x00" */
87
+ /* moving esp into esp, but this is a no-op */
88
+ EOS
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,108 @@
1
+ # encoding: ASCII-8BIT
2
+
3
+ require 'test_helper'
4
+
5
+ require 'pwnlib/context'
6
+ require 'pwnlib/shellcraft/shellcraft'
7
+
8
+ class PushstrTest < MiniTest::Test
9
+ include ::Pwnlib::Context
10
+
11
+ def setup
12
+ @shellcraft = ::Pwnlib::Shellcraft::Shellcraft.instance
13
+ end
14
+
15
+ def test_amd64
16
+ context.local(arch: 'amd64') do
17
+ assert_equal(<<-'EOS', @shellcraft.pushstr('A'))
18
+ /* push "A\x00" */
19
+ push 0x41
20
+ EOS
21
+ assert_equal(<<-'EOS', @shellcraft.pushstr("\n"))
22
+ /* push "\n\x00" */
23
+ push 0xb
24
+ dec byte ptr [rsp]
25
+ EOS
26
+ assert_equal(<<-'EOS', @shellcraft.pushstr('A' * 4))
27
+ /* push "AAAA\x00" */
28
+ push 0x41414141
29
+ EOS
30
+ assert_equal(<<-'EOS', @shellcraft.pushstr('A' * 8))
31
+ /* push "AAAAAAAA\x00" */
32
+ push 1
33
+ dec byte ptr [rsp]
34
+ mov rax, 0x4141414141414141
35
+ push rax
36
+ EOS
37
+ assert_equal(<<-'EOS', @shellcraft.pushstr('A' * 8, append_null: false))
38
+ /* push "AAAAAAAA" */
39
+ mov rax, 0x4141414141414141
40
+ push rax
41
+ EOS
42
+ assert_equal(<<-'EOS', @shellcraft.pushstr("\n" * 4))
43
+ /* push "\n\n\n\n\x00" */
44
+ push 0x1010101 ^ 0xa0a0a0a
45
+ xor dword ptr [rsp], 0x1010101
46
+ EOS
47
+ assert_equal(<<-'EOS', @shellcraft.pushstr('/bin/sh'))
48
+ /* push "/bin/sh\x00" */
49
+ mov rax, 0x101010101010101
50
+ push rax
51
+ mov rax, 0x169722e6f68632e /* 0x101010101010101 ^ 0x68732f6e69622f */
52
+ xor [rsp], rax
53
+ EOS
54
+ assert_equal(<<-'EOS', @shellcraft.pushstr("\x00\xff\xff\xff\xff\xff\xff\xff", append_null: false))
55
+ /* push "\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF" */
56
+ mov rax, 0x101010101010101
57
+ push rax
58
+ mov rax, -0x1010101010101ff /* 0x101010101010101 ^ -0x100 */
59
+ xor [rsp], rax
60
+ EOS
61
+ end
62
+ end
63
+
64
+ def test_i386
65
+ context.local(arch: 'i386') do
66
+ assert_equal(<<-'EOS', @shellcraft.pushstr('A'))
67
+ /* push "A\x00" */
68
+ push 0x41
69
+ EOS
70
+ assert_equal(<<-'EOS', @shellcraft.pushstr("\n"))
71
+ /* push "\n\x00" */
72
+ push 0xb
73
+ dec byte ptr [esp]
74
+ EOS
75
+ assert_equal(<<-'EOS', @shellcraft.pushstr('A' * 4))
76
+ /* push "AAAA\x00" */
77
+ push 1
78
+ dec byte ptr [esp]
79
+ push 0x41414141
80
+ EOS
81
+ assert_equal(<<-'EOS', @shellcraft.pushstr('A' * 8))
82
+ /* push "AAAAAAAA\x00" */
83
+ push 1
84
+ dec byte ptr [esp]
85
+ push 0x41414141
86
+ push 0x41414141
87
+ EOS
88
+ assert_equal(<<-'EOS', @shellcraft.pushstr('A' * 8, append_null: false))
89
+ /* push "AAAAAAAA" */
90
+ push 0x41414141
91
+ push 0x41414141
92
+ EOS
93
+ assert_equal(<<-'EOS', @shellcraft.pushstr("\n" * 4))
94
+ /* push "\n\n\n\n\x00" */
95
+ push 1
96
+ dec byte ptr [esp]
97
+ push 0x1010101
98
+ xor dword ptr [esp], 0xb0b0b0b /* 0x1010101 ^ 0xa0a0a0a */
99
+ EOS
100
+ assert_equal(<<-'EOS', @shellcraft.pushstr('/bin/sh'))
101
+ /* push "/bin/sh\x00" */
102
+ push 0x1010101
103
+ xor dword ptr [esp], 0x169722e /* 0x1010101 ^ 0x68732f */
104
+ push 0x6e69622f
105
+ EOS
106
+ end
107
+ end
108
+ end