pwntools 0.1.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +96 -15
  3. data/Rakefile +8 -2
  4. data/lib/pwn.rb +10 -7
  5. data/lib/pwnlib/abi.rb +61 -0
  6. data/lib/pwnlib/asm.rb +357 -0
  7. data/lib/pwnlib/constants/constant.rb +19 -3
  8. data/lib/pwnlib/constants/constants.rb +46 -20
  9. data/lib/pwnlib/constants/linux/amd64.rb +32 -1
  10. data/lib/pwnlib/constants/linux/i386.rb +2 -0
  11. data/lib/pwnlib/context.rb +128 -27
  12. data/lib/pwnlib/dynelf.rb +122 -54
  13. data/lib/pwnlib/elf/elf.rb +340 -0
  14. data/lib/pwnlib/errors.rb +31 -0
  15. data/lib/pwnlib/ext/array.rb +2 -1
  16. data/lib/pwnlib/ext/helper.rb +6 -5
  17. data/lib/pwnlib/ext/integer.rb +2 -1
  18. data/lib/pwnlib/ext/string.rb +3 -2
  19. data/lib/pwnlib/logger.rb +245 -0
  20. data/lib/pwnlib/memleak.rb +59 -29
  21. data/lib/pwnlib/pwn.rb +27 -9
  22. data/lib/pwnlib/reg_sort.rb +109 -110
  23. data/lib/pwnlib/runner.rb +53 -0
  24. data/lib/pwnlib/shellcraft/generators/amd64/common/common.rb +16 -0
  25. data/lib/pwnlib/shellcraft/generators/amd64/common/infloop.rb +24 -0
  26. data/lib/pwnlib/shellcraft/generators/amd64/common/memcpy.rb +35 -0
  27. data/lib/pwnlib/shellcraft/generators/amd64/common/mov.rb +131 -0
  28. data/lib/pwnlib/shellcraft/generators/amd64/common/nop.rb +18 -0
  29. data/lib/pwnlib/shellcraft/generators/amd64/common/popad.rb +28 -0
  30. data/lib/pwnlib/shellcraft/generators/amd64/common/pushstr.rb +66 -0
  31. data/lib/pwnlib/shellcraft/generators/amd64/common/pushstr_array.rb +24 -0
  32. data/lib/pwnlib/shellcraft/generators/amd64/common/ret.rb +33 -0
  33. data/lib/pwnlib/shellcraft/generators/amd64/common/setregs.rb +24 -0
  34. data/lib/pwnlib/shellcraft/generators/amd64/linux/cat.rb +24 -0
  35. data/lib/pwnlib/shellcraft/generators/amd64/linux/execve.rb +24 -0
  36. data/lib/pwnlib/shellcraft/generators/amd64/linux/exit.rb +24 -0
  37. data/lib/pwnlib/shellcraft/generators/amd64/linux/linux.rb +16 -0
  38. data/lib/pwnlib/shellcraft/generators/amd64/linux/ls.rb +24 -0
  39. data/lib/pwnlib/shellcraft/generators/amd64/linux/open.rb +24 -0
  40. data/lib/pwnlib/shellcraft/generators/amd64/linux/sh.rb +24 -0
  41. data/lib/pwnlib/shellcraft/generators/amd64/linux/sleep.rb +24 -0
  42. data/lib/pwnlib/shellcraft/generators/amd64/linux/syscall.rb +24 -0
  43. data/lib/pwnlib/shellcraft/generators/helper.rb +115 -0
  44. data/lib/pwnlib/shellcraft/generators/i386/common/common.rb +16 -0
  45. data/lib/pwnlib/shellcraft/generators/i386/common/infloop.rb +24 -0
  46. data/lib/pwnlib/shellcraft/generators/i386/common/memcpy.rb +34 -0
  47. data/lib/pwnlib/shellcraft/generators/i386/common/mov.rb +93 -0
  48. data/lib/pwnlib/shellcraft/generators/i386/common/nop.rb +18 -0
  49. data/lib/pwnlib/shellcraft/generators/i386/common/pushstr.rb +41 -0
  50. data/lib/pwnlib/shellcraft/generators/i386/common/pushstr_array.rb +24 -0
  51. data/lib/pwnlib/shellcraft/generators/i386/common/setregs.rb +24 -0
  52. data/lib/pwnlib/shellcraft/generators/i386/linux/cat.rb +24 -0
  53. data/lib/pwnlib/shellcraft/generators/i386/linux/execve.rb +24 -0
  54. data/lib/pwnlib/shellcraft/generators/i386/linux/exit.rb +24 -0
  55. data/lib/pwnlib/shellcraft/generators/i386/linux/linux.rb +16 -0
  56. data/lib/pwnlib/shellcraft/generators/i386/linux/ls.rb +24 -0
  57. data/lib/pwnlib/shellcraft/generators/i386/linux/open.rb +24 -0
  58. data/lib/pwnlib/shellcraft/generators/i386/linux/sh.rb +24 -0
  59. data/lib/pwnlib/shellcraft/generators/i386/linux/sleep.rb +24 -0
  60. data/lib/pwnlib/shellcraft/generators/i386/linux/syscall.rb +24 -0
  61. data/lib/pwnlib/shellcraft/generators/x86/common/common.rb +29 -0
  62. data/lib/pwnlib/shellcraft/generators/x86/common/infloop.rb +24 -0
  63. data/lib/pwnlib/shellcraft/generators/x86/common/memcpy.rb +17 -0
  64. data/lib/pwnlib/shellcraft/generators/x86/common/mov.rb +17 -0
  65. data/lib/pwnlib/shellcraft/generators/x86/common/pushstr.rb +17 -0
  66. data/lib/pwnlib/shellcraft/generators/x86/common/pushstr_array.rb +86 -0
  67. data/lib/pwnlib/shellcraft/generators/x86/common/setregs.rb +84 -0
  68. data/lib/pwnlib/shellcraft/generators/x86/linux/cat.rb +54 -0
  69. data/lib/pwnlib/shellcraft/generators/x86/linux/execve.rb +72 -0
  70. data/lib/pwnlib/shellcraft/generators/x86/linux/exit.rb +34 -0
  71. data/lib/pwnlib/shellcraft/generators/x86/linux/linux.rb +16 -0
  72. data/lib/pwnlib/shellcraft/generators/x86/linux/ls.rb +67 -0
  73. data/lib/pwnlib/shellcraft/generators/x86/linux/open.rb +47 -0
  74. data/lib/pwnlib/shellcraft/generators/x86/linux/sh.rb +53 -0
  75. data/lib/pwnlib/shellcraft/generators/x86/linux/sleep.rb +52 -0
  76. data/lib/pwnlib/shellcraft/generators/x86/linux/syscall.rb +52 -0
  77. data/lib/pwnlib/shellcraft/registers.rb +148 -0
  78. data/lib/pwnlib/shellcraft/shellcraft.rb +73 -0
  79. data/lib/pwnlib/timer.rb +67 -0
  80. data/lib/pwnlib/tubes/buffer.rb +99 -0
  81. data/lib/pwnlib/tubes/process.rb +155 -0
  82. data/lib/pwnlib/tubes/serialtube.rb +114 -0
  83. data/lib/pwnlib/tubes/sock.rb +101 -0
  84. data/lib/pwnlib/tubes/tube.rb +442 -0
  85. data/lib/pwnlib/ui.rb +21 -0
  86. data/lib/pwnlib/util/cyclic.rb +97 -94
  87. data/lib/pwnlib/util/fiddling.rb +288 -220
  88. data/lib/pwnlib/util/getdents.rb +85 -0
  89. data/lib/pwnlib/util/hexdump.rb +116 -112
  90. data/lib/pwnlib/util/lists.rb +58 -0
  91. data/lib/pwnlib/util/packing.rb +223 -228
  92. data/lib/pwnlib/util/ruby.rb +19 -0
  93. data/lib/pwnlib/version.rb +3 -1
  94. data/test/abi_test.rb +22 -0
  95. data/test/asm_test.rb +177 -0
  96. data/test/constants/constant_test.rb +2 -0
  97. data/test/constants/constants_test.rb +5 -2
  98. data/test/context_test.rb +14 -3
  99. data/test/data/assembly/aarch64.s +19 -0
  100. data/test/data/assembly/amd64.s +21 -0
  101. data/test/data/assembly/arm.s +9 -0
  102. data/test/data/assembly/i386.s +21 -0
  103. data/test/data/assembly/mips.s +16 -0
  104. data/test/data/assembly/mips64.s +6 -0
  105. data/test/data/assembly/powerpc.s +18 -0
  106. data/test/data/assembly/powerpc64.s +36 -0
  107. data/test/data/assembly/sparc.s +33 -0
  108. data/test/data/assembly/sparc64.s +5 -0
  109. data/test/data/assembly/thumb.s +37 -0
  110. data/test/data/echo.rb +16 -0
  111. data/test/data/elfs/Makefile +24 -0
  112. data/test/data/elfs/amd64.frelro.elf +0 -0
  113. data/test/data/elfs/amd64.frelro.pie.elf +0 -0
  114. data/test/data/elfs/amd64.nrelro.elf +0 -0
  115. data/test/data/elfs/amd64.prelro.elf +0 -0
  116. data/test/data/elfs/amd64.static.elf +0 -0
  117. data/test/data/elfs/i386.frelro.pie.elf +0 -0
  118. data/test/data/elfs/i386.prelro.elf +0 -0
  119. data/test/data/elfs/source.cpp +19 -0
  120. data/test/data/flag +1 -0
  121. data/test/data/lib32/ld.so.2 +0 -0
  122. data/test/data/lib32/libc.so.6 +0 -0
  123. data/test/data/lib64/ld.so.2 +0 -0
  124. data/test/data/lib64/libc.so.6 +0 -0
  125. data/test/dynelf_test.rb +62 -25
  126. data/test/elf/elf_test.rb +147 -0
  127. data/test/ext_test.rb +4 -2
  128. data/test/files/use_pwn.rb +3 -6
  129. data/test/files/use_pwnlib.rb +2 -1
  130. data/test/full_file_test.rb +6 -0
  131. data/test/logger_test.rb +120 -0
  132. data/test/memleak_test.rb +5 -33
  133. data/test/reg_sort_test.rb +4 -1
  134. data/test/runner_test.rb +32 -0
  135. data/test/shellcraft/infloop_test.rb +27 -0
  136. data/test/shellcraft/linux/cat_test.rb +87 -0
  137. data/test/shellcraft/linux/ls_test.rb +109 -0
  138. data/test/shellcraft/linux/sh_test.rb +120 -0
  139. data/test/shellcraft/linux/sleep_test.rb +68 -0
  140. data/test/shellcraft/linux/syscalls/execve_test.rb +137 -0
  141. data/test/shellcraft/linux/syscalls/exit_test.rb +57 -0
  142. data/test/shellcraft/linux/syscalls/open_test.rb +87 -0
  143. data/test/shellcraft/linux/syscalls/syscall_test.rb +84 -0
  144. data/test/shellcraft/memcpy_test.rb +50 -0
  145. data/test/shellcraft/mov_test.rb +99 -0
  146. data/test/shellcraft/nop_test.rb +27 -0
  147. data/test/shellcraft/popad_test.rb +30 -0
  148. data/test/shellcraft/pushstr_array_test.rb +92 -0
  149. data/test/shellcraft/pushstr_test.rb +109 -0
  150. data/test/shellcraft/registers_test.rb +33 -0
  151. data/test/shellcraft/ret_test.rb +31 -0
  152. data/test/shellcraft/setregs_test.rb +63 -0
  153. data/test/shellcraft/shellcraft_test.rb +30 -0
  154. data/test/test_helper.rb +61 -2
  155. data/test/timer_test.rb +42 -0
  156. data/test/tubes/buffer_test.rb +46 -0
  157. data/test/tubes/process_test.rb +105 -0
  158. data/test/tubes/serialtube_test.rb +162 -0
  159. data/test/tubes/sock_test.rb +68 -0
  160. data/test/tubes/tube_test.rb +320 -0
  161. data/test/ui_test.rb +18 -0
  162. data/test/util/cyclic_test.rb +3 -1
  163. data/test/util/fiddling_test.rb +12 -3
  164. data/test/util/getdents_test.rb +33 -0
  165. data/test/util/hexdump_test.rb +9 -10
  166. data/test/util/lists_test.rb +22 -0
  167. data/test/util/packing_test.rb +5 -3
  168. metadata +357 -37
data/test/ext_test.rb CHANGED
@@ -1,13 +1,15 @@
1
1
  # encoding: ASCII-8BIT
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'test_helper'
5
+
4
6
  require 'pwnlib/ext/array'
5
7
  require 'pwnlib/ext/integer'
6
8
  require 'pwnlib/ext/string'
7
9
 
8
10
  class ExtTest < MiniTest::Test
9
- # Thought that test one method in each module for each type is enough, since it's quite
10
- # stupid (and meaningless) to copy the list of proxied functions to here...
11
+ # Thought that test one method in each module for each type is enough, since it's quite stupid (and meaningless) to
12
+ # copy the list of proxied functions to here...
11
13
  def test_ext_string
12
14
  assert_equal(0x4142, 'AB'.u16(endian: 'be'))
13
15
  assert_equal([1, 1, 0, 0, 0, 1, 0, 0], "\xC4".bits)
@@ -1,4 +1,5 @@
1
1
  # encoding: ASCII-8BIT
2
+ # frozen_string_literal: true
2
3
 
3
4
  # Make sure we're using local copy for local testing.
4
5
  $LOAD_PATH.unshift File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
@@ -8,12 +9,8 @@ require 'pwn'
8
9
  context[arch: 'amd64']
9
10
 
10
11
  raise 'pack fail' unless pack(1) == "\x01\0\0\0\0\0\0\0"
11
- unless ::Pwnlib::Util::Fiddling.__send__(:context).object_id == context.object_id
12
- raise 'not unique context'
13
- end
14
- unless ::Pwnlib::Context.context.object_id == context.object_id
15
- raise 'not unique context'
16
- end
12
+ raise 'not unique context' unless ::Pwnlib::Util::Fiddling.__send__(:context).equal?(context)
13
+ raise 'not unique context' unless ::Pwnlib::Context.context.equal?(context)
17
14
 
18
15
  # Make sure things aren't polluting Object
19
16
  begin
@@ -1,4 +1,5 @@
1
1
  # encoding: ASCII-8BIT
2
+ # frozen_string_literal: true
2
3
 
3
4
  # Make sure we're using local copy for local testing.
4
5
  $LOAD_PATH.unshift File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
@@ -8,7 +9,7 @@ require 'pwnlib/util/packing'
8
9
 
9
10
  raise 'call from module fail' unless ::Pwnlib::Util::Packing.p8(0x61) == 'a'
10
11
 
11
- include ::Pwnlib::Util::Packing::ClassMethods
12
+ include ::Pwnlib::Util::Packing
12
13
  raise 'include module and call fail' unless p8(0x61) == 'a'
13
14
 
14
15
  begin
@@ -1,4 +1,5 @@
1
1
  # encoding: ASCII-8BIT
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'open3'
4
5
 
@@ -13,4 +14,9 @@ class FullFileTest < MiniTest::Test
13
14
  assert(status.success?, stderr)
14
15
  end
15
16
  end
17
+
18
+ def test_ruby_cli
19
+ _, stderr, status = Open3.capture3('ruby', '-e', 'require "pwn"', binmode: true)
20
+ assert(status.success?, stderr)
21
+ end
16
22
  end
@@ -0,0 +1,120 @@
1
+ # encoding: ASCII-8BIT
2
+ # frozen_string_literal: true
3
+
4
+ require 'open3'
5
+ require 'tempfile'
6
+
7
+ require 'test_helper'
8
+
9
+ require 'pwnlib/context'
10
+ require 'pwnlib/logger'
11
+
12
+ class LoggerTest < MiniTest::Test
13
+ include ::Pwnlib::Context
14
+ include ::Pwnlib::Logger
15
+
16
+ def setup
17
+ @logger = ::Pwnlib::Logger::LoggerType.new
18
+ class << @logger
19
+ def add(*)
20
+ clear
21
+ super
22
+ @logdev.string
23
+ end
24
+
25
+ def indented(*, **)
26
+ clear
27
+ super
28
+ @logdev.string
29
+ end
30
+
31
+ def clear
32
+ @logdev = StringIO.new
33
+ end
34
+ end
35
+ end
36
+
37
+ def test_log
38
+ str = 'darkhh i4 so s4d'
39
+ context.local(log_level: DEBUG) do
40
+ %w(DEBUG INFO WARN ERROR FATAL).each do |type|
41
+ assert_equal("[#{type}] #{str}\n", @logger.public_send(type.downcase, str))
42
+ assert_equal("[#{type}] #{str}\n", @logger.public_send(type.downcase) { str })
43
+ end
44
+ end
45
+
46
+ assert_empty(@logger.debug(str))
47
+ assert_empty(@logger.debug { str })
48
+ %w(INFO WARN ERROR FATAL).each do |type|
49
+ assert_equal("[#{type}] #{str}\n", @logger.public_send(type.downcase, str))
50
+ assert_equal("[#{type}] #{str}\n", @logger.public_send(type.downcase) { str })
51
+ end
52
+ end
53
+
54
+ def test_indented
55
+ assert_silent { log.indented('darkhh', level: DEBUG) }
56
+ assert_empty(@logger.indented('A', level: DEBUG))
57
+
58
+ data = ['meow', 'meow meow', 'meowmeowmeow'].join("\n")
59
+ assert_equal(<<-EOS, @logger.indented(data, level: INFO))
60
+ meow
61
+ meow meow
62
+ meowmeowmeow
63
+ EOS
64
+ end
65
+
66
+ def test_dump
67
+ x = 2
68
+ y = 3
69
+ assert_equal(<<-EOS, @logger.dump(x + y, x * y))
70
+ [DUMP] (x + y) = 5, (x * y) = 6
71
+ EOS
72
+
73
+ libc = 0x7fc0bdd13000
74
+ # check if source code parsing works good
75
+ msg = @logger.dump(
76
+ libc # comment is ok
77
+ .to_s(16),
78
+ libc - libc * 1
79
+ )
80
+ assert_equal(<<-EOS, msg)
81
+ [DUMP] libc.to_s(16) = "7fc0bdd13000", (libc - (libc * 1)) = 0
82
+ EOS
83
+
84
+ libc = 0x7fc0bdd13000
85
+ assert_equal(<<-EOS, @logger.dump { libc.to_s(16) })
86
+ [DUMP] libc.to_s(16) = "7fc0bdd13000"
87
+ EOS
88
+
89
+ res = @logger.dump do
90
+ libc = 12_345_678
91
+ libc <<= 12
92
+ # comments will be ignored
93
+ libc.to_s # dummy line
94
+ libc.to_s(16)
95
+ end
96
+ assert_equal(<<-EOS, res)
97
+ [DUMP] libc = 12345678
98
+ libc = (libc << 12)
99
+ libc.to_s
100
+ libc.to_s(16) = "bc614e000"
101
+ EOS
102
+
103
+ lib_path = File.expand_path(File.join(__dir__, '..', 'lib'))
104
+ f = Tempfile.new(['dump', '.rb'])
105
+ begin
106
+ f.write <<~EOS
107
+ $LOAD_PATH.unshift #{lib_path.inspect}
108
+ require 'pwn'
109
+ FileUtils.remove(__FILE__)
110
+ log.dump 1337
111
+ EOS
112
+ f.close
113
+ _, stderr, status = Open3.capture3('ruby', f.path, binmode: true)
114
+ assert(status.success?, stderr)
115
+ ensure
116
+ f.close
117
+ f.unlink
118
+ end
119
+ end
120
+ end
data/test/memleak_test.rb CHANGED
@@ -1,48 +1,20 @@
1
1
  # encoding: ASCII-8BIT
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'open3'
4
5
 
5
6
  require 'tty-platform'
6
7
 
7
8
  require 'test_helper'
9
+
8
10
  require 'pwnlib/memleak'
9
11
 
10
12
  class MemLeakTest < MiniTest::Test
11
13
  def setup
12
- @victim = IO.binread(File.expand_path('../data/victim32', __FILE__))
14
+ @victim = IO.binread(File.expand_path('data/victim32', __dir__))
13
15
  @leak = ::Pwnlib::MemLeak.new { |addr| @victim[addr] }
14
16
  end
15
17
 
16
- def test_find_elf_base_basic
17
- assert_equal(0, @leak.find_elf_base(@victim.length * 2 / 3))
18
- end
19
-
20
- def test_find_elf_base_running
21
- skip 'Only tested on linux' unless TTY::Platform.new.linux?
22
- [32, 64].each do |b|
23
- # TODO(hh): Use process instead of popen2
24
- Open3.popen2(File.expand_path("../data/victim#{b}", __FILE__)) do |i, o, t|
25
- main_ra = o.readline[2...-1].to_i(16)
26
- realbase = nil
27
- IO.readlines("/proc/#{t.pid}/maps").map(&:split).each do |s|
28
- st, ed = s[0].split('-').map { |x| x.to_i(16) }
29
- next unless main_ra.between?(st, ed)
30
- realbase = st
31
- break
32
- end
33
- refute_nil(realbase)
34
- mem = open("/proc/#{t.pid}/mem", 'rb')
35
- l2 = ::Pwnlib::MemLeak.new do |addr|
36
- mem.seek(addr)
37
- mem.getc
38
- end
39
- assert_equal(realbase, l2.find_elf_base(main_ra))
40
- mem.close
41
- i.write('bye')
42
- end
43
- end
44
- end
45
-
46
18
  def test_n
47
19
  assert_equal("\x7fELF", @leak.n(0, 4))
48
20
  assert_equal(@victim[0xf0, 0x20], @leak.n(0xf0, 0x20))
@@ -50,8 +22,8 @@ class MemLeakTest < MiniTest::Test
50
22
  end
51
23
 
52
24
  def test_b
53
- assert_equal(@victim[0x100], @leak.b(0x100))
54
- assert_equal(@victim[514], @leak.b(514))
25
+ assert_equal(::Pwnlib::Util::Packing.u8(@victim[0x100]), @leak.b(0x100))
26
+ assert_equal(::Pwnlib::Util::Packing.u8(@victim[514]), @leak.b(514))
55
27
  end
56
28
 
57
29
  def test_w
@@ -1,9 +1,12 @@
1
1
  # encoding: ASCII-8BIT
2
+ # frozen_string_literal: true
3
+
2
4
  require 'test_helper'
5
+
3
6
  require 'pwnlib/reg_sort'
4
7
 
5
8
  class RegSortTest < MiniTest::Test
6
- include ::Pwnlib::RegSort::ClassMethods
9
+ include ::Pwnlib::RegSort
7
10
 
8
11
  def setup
9
12
  @regs = %w(a b c d x y z)
@@ -0,0 +1,32 @@
1
+ # encoding: ASCII-8BIT
2
+ # frozen_string_literal: true
3
+
4
+ require 'test_helper'
5
+
6
+ require 'pwnlib/runner'
7
+ require 'pwnlib/shellcraft/shellcraft'
8
+
9
+ class RunnerTest < MiniTest::Test
10
+ include ::Pwnlib::Context
11
+
12
+ def setup
13
+ linux_only 'Runner can only be used on Linux'
14
+ end
15
+
16
+ def shellcraft
17
+ ::Pwnlib::Shellcraft::Shellcraft.instance
18
+ end
19
+
20
+ def test_i386_run_assembly
21
+ context.local(arch: 'i386') do
22
+ r = ::Pwnlib::Runner.run_assembly(
23
+ shellcraft.pushstr('run_assembly') +
24
+ shellcraft.syscall('SYS_write', 1, 'esp', 12) +
25
+ shellcraft.exit(0)
26
+ )
27
+ assert_equal('run_assembly', r.recvn(12))
28
+ # Test if reach EOF
29
+ assert_raises(::Pwnlib::Errors::EndOfTubeError) { r.recv }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: ASCII-8BIT
2
+ # frozen_string_literal: true
3
+
4
+ require 'test_helper'
5
+
6
+ require 'pwnlib/context'
7
+ require 'pwnlib/shellcraft/shellcraft'
8
+
9
+ class InfloopTest < MiniTest::Test
10
+ include ::Pwnlib::Context
11
+
12
+ def setup
13
+ @shellcraft = ::Pwnlib::Shellcraft::Shellcraft.instance
14
+ end
15
+
16
+ def test_amd64
17
+ context.local(arch: 'amd64') do
18
+ assert_match(/\Ainfloop_\d+:\n jmp infloop_\d+\n\Z/, @shellcraft.infloop)
19
+ end
20
+ end
21
+
22
+ def test_i386
23
+ context.local(arch: 'i386') do
24
+ assert_match(/\Ainfloop_\d+:\n jmp infloop_\d+\n\Z/, @shellcraft.infloop)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,87 @@
1
+ # encoding: ASCII-8BIT
2
+ # frozen_string_literal: true
3
+
4
+ require 'test_helper'
5
+
6
+ require 'pwnlib/context'
7
+ require 'pwnlib/shellcraft/shellcraft'
8
+
9
+ class CatTest < MiniTest::Test
10
+ include ::Pwnlib::Context
11
+
12
+ def setup
13
+ @shellcraft = ::Pwnlib::Shellcraft::Shellcraft.instance
14
+ end
15
+
16
+ def test_amd64
17
+ context.local(arch: 'amd64') do
18
+ assert_equal(<<-'EOS', @shellcraft.cat('flag'))
19
+ /* push "flag\x00" */
20
+ push 0x67616c66
21
+ /* call open("rsp", "O_RDONLY", 0) */
22
+ push 2 /* (SYS_open) */
23
+ pop rax
24
+ mov rdi, rsp
25
+ xor esi, esi /* (O_RDONLY) */
26
+ cdq /* rdx=0 */
27
+ syscall
28
+ /* call sendfile(1, "rax", 0, 2147483647) */
29
+ push 1
30
+ pop rdi
31
+ mov rsi, rax
32
+ push 0x28 /* (SYS_sendfile) */
33
+ pop rax
34
+ mov r10d, 0x7fffffff
35
+ cdq /* rdx=0 */
36
+ syscall
37
+ EOS
38
+ assert_equal(<<-'EOS', @shellcraft.cat('flag', fd: 2))
39
+ /* push "flag\x00" */
40
+ push 0x67616c66
41
+ /* call open("rsp", "O_RDONLY", 0) */
42
+ push 2 /* (SYS_open) */
43
+ pop rax
44
+ mov rdi, rsp
45
+ xor esi, esi /* (O_RDONLY) */
46
+ cdq /* rdx=0 */
47
+ syscall
48
+ /* call sendfile(2, "rax", 0, 2147483647) */
49
+ push 2
50
+ pop rdi
51
+ mov rsi, rax
52
+ push 0x28 /* (SYS_sendfile) */
53
+ pop rax
54
+ mov r10d, 0x7fffffff
55
+ cdq /* rdx=0 */
56
+ syscall
57
+ EOS
58
+ end
59
+ end
60
+
61
+ def test_i386
62
+ context.local(arch: 'i386') do
63
+ assert_equal(<<-'EOS', @shellcraft.cat('flag'))
64
+ /* push "flag\x00" */
65
+ push 1
66
+ dec byte ptr [esp]
67
+ push 0x67616c66
68
+ /* call open("esp", "O_RDONLY", 0) */
69
+ push 5 /* (SYS_open) */
70
+ pop eax
71
+ mov ebx, esp
72
+ xor ecx, ecx /* (O_RDONLY) */
73
+ cdq /* edx=0 */
74
+ int 0x80
75
+ /* call sendfile(1, "eax", 0, 2147483647) */
76
+ push 1
77
+ pop ebx
78
+ mov ecx, eax
79
+ xor eax, eax
80
+ mov al, 0xbb /* (SYS_sendfile) */
81
+ mov esi, 0x7fffffff
82
+ cdq /* edx=0 */
83
+ int 0x80
84
+ EOS
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,109 @@
1
+ # encoding: ASCII-8BIT
2
+ # frozen_string_literal: true
3
+
4
+ require 'test_helper'
5
+
6
+ require 'pwnlib/context'
7
+ require 'pwnlib/shellcraft/shellcraft'
8
+
9
+ class LsTest < MiniTest::Test
10
+ include ::Pwnlib::Context
11
+
12
+ def setup
13
+ @shellcraft = ::Pwnlib::Shellcraft::Shellcraft.instance
14
+ end
15
+
16
+ def test_amd64
17
+ context.local(arch: 'amd64') do
18
+ assert_equal(<<-'EOS', @shellcraft.ls)
19
+ /* push ".\x00" */
20
+ push 0x2e
21
+ /* call open("rsp", 0, 0) */
22
+ push 2 /* (SYS_open) */
23
+ pop rax
24
+ mov rdi, rsp
25
+ xor esi, esi /* 0 */
26
+ cdq /* rdx=0 */
27
+ syscall
28
+ /* call getdents("rax", "rsp", 4096) */
29
+ mov rdi, rax
30
+ push 0x4e /* (SYS_getdents) */
31
+ pop rax
32
+ mov rsi, rsp
33
+ xor edx, edx
34
+ mov dh, 0x1000 >> 8
35
+ syscall
36
+ /* call write(1, "rsp", "rax") */
37
+ push 1
38
+ pop rdi
39
+ mov rsi, rsp
40
+ mov rdx, rax
41
+ push 1 /* (SYS_write) */
42
+ pop rax
43
+ syscall
44
+ EOS
45
+ assert_equal(<<-'EOS', @shellcraft.ls('/usr/bin'))
46
+ /* push "/usr/bin\x00" */
47
+ push 1
48
+ dec byte ptr [rsp]
49
+ mov rax, 0x6e69622f7273752f
50
+ push rax
51
+ /* call open("rsp", 0, 0) */
52
+ push 2 /* (SYS_open) */
53
+ pop rax
54
+ mov rdi, rsp
55
+ xor esi, esi /* 0 */
56
+ cdq /* rdx=0 */
57
+ syscall
58
+ /* call getdents("rax", "rsp", 4096) */
59
+ mov rdi, rax
60
+ push 0x4e /* (SYS_getdents) */
61
+ pop rax
62
+ mov rsi, rsp
63
+ xor edx, edx
64
+ mov dh, 0x1000 >> 8
65
+ syscall
66
+ /* call write(1, "rsp", "rax") */
67
+ push 1
68
+ pop rdi
69
+ mov rsi, rsp
70
+ mov rdx, rax
71
+ push 1 /* (SYS_write) */
72
+ pop rax
73
+ syscall
74
+ EOS
75
+ end
76
+ end
77
+
78
+ def test_i386
79
+ context.local(arch: 'i386') do
80
+ assert_equal(<<-'EOS', @shellcraft.ls)
81
+ /* push ".\x00" */
82
+ push 0x2e
83
+ /* call open("esp", 0, 0) */
84
+ push 5 /* (SYS_open) */
85
+ pop eax
86
+ mov ebx, esp
87
+ xor ecx, ecx /* 0 */
88
+ cdq /* edx=0 */
89
+ int 0x80
90
+ /* call getdents("eax", "esp", 4096) */
91
+ mov ebx, eax
92
+ xor eax, eax
93
+ mov al, 0x8d /* (SYS_getdents) */
94
+ mov ecx, esp
95
+ xor edx, edx
96
+ mov dh, 0x1000 >> 8
97
+ int 0x80
98
+ /* call write(1, "esp", "eax") */
99
+ push 1
100
+ pop ebx
101
+ mov ecx, esp
102
+ mov edx, eax
103
+ push 4 /* (SYS_write) */
104
+ pop eax
105
+ int 0x80
106
+ EOS
107
+ end
108
+ end
109
+ end