Skip to content

Commit

Permalink
Fix keyword argument errors raised in Ruby 2.8 (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
david942j authored Jun 2, 2020
1 parent 9ca4ef0 commit de6f783
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 43 deletions.
4 changes: 2 additions & 2 deletions lib/pwnlib/ext/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def def_proxy_method(mod, *ms, **m2)
.concat(m2.to_a)
.each do |method, proxy_to|
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def #{method}(*args, &block)
#{mod}.#{proxy_to}(self, *args, &block)
def #{method}(*args, **kwargs, &block)
#{mod}.#{proxy_to}(self, *args, **kwargs, &block)
end
EOS
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pwnlib/shellcraft/generators/amd64/common/memcpy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Common
def memcpy(dst, src, n)
cat "/* memcpy(#{pretty(dst)}, #{pretty(src)}, #{pretty(n)}) */"
cat 'cld'
cat Common.setregs(rdi: dst, rsi: src, rcx: n)
cat Common.setregs({ rdi: dst, rsi: src, rcx: n })
cat 'rep movsb'
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pwnlib/shellcraft/generators/amd64/common/setregs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module Common
# @overload setregs(reg_context, stack_allowed: true)
#
# @see Generators::X86::Common#setregs
def setregs(*args)
def setregs(*args, **kwargs)
context.local(arch: :amd64) do
cat X86::Common.setregs(*args)
cat X86::Common.setregs(*args, **kwargs)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pwnlib/shellcraft/generators/amd64/linux/cat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module Linux
# @overload cat(filename, fd: 1)
#
# @see Generators::X86::Linux#cat
def cat(*args)
def cat(*args, **kwargs)
context.local(arch: :amd64) do
cat X86::Linux.cat(*args)
cat X86::Linux.cat(*args, **kwargs)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pwnlib/shellcraft/generators/amd64/linux/sh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module Linux
# @overload sh(argv: false)
#
# @see Generators::X86::Linux#sh
def sh(*args)
def sh(**kwargs)
context.local(arch: :amd64) do
cat X86::Linux.sh(*args)
cat X86::Linux.sh(**kwargs)
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions lib/pwnlib/shellcraft/generators/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,14 @@ class << mod
# Each method runs in an independent 'runner', so methods would not effect each other.
runner = Runner.new
method = instance_method(m).bind(runner)
define_singleton_method(m) do |*args|
define_singleton_method(m) do |*args, **kwargs|
runner.clear
method.call(*args)
# TODO(david942j): remove the check when we drop Ruby 2.6 support
if kwargs.empty?
method.call(*args)
else
method.call(*args, **kwargs)
end
runner.typesetting
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pwnlib/shellcraft/generators/i386/common/memcpy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Common
def memcpy(dst, src, n)
cat "/* memcpy(#{pretty(dst)}, #{pretty(src)}, #{pretty(n)}) */"
cat 'cld'
cat Common.setregs(edi: dst, esi: src, ecx: n)
cat Common.setregs({ edi: dst, esi: src, ecx: n })
cat 'rep movsb'
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pwnlib/shellcraft/generators/i386/common/setregs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module Common
# @overload setregs(reg_context, stack_allowed: true)
#
# @see Generators::X86::Common#setregs
def setregs(*args)
def setregs(*args, **kwargs)
context.local(arch: :i386) do
cat X86::Common.setregs(*args)
cat X86::Common.setregs(*args, **kwargs)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pwnlib/shellcraft/generators/i386/linux/cat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module Linux
# @overload cat(filename, fd: 1)
#
# @see Generators::X86::Linux#cat
def cat(*args)
def cat(*args, **kwargs)
context.local(arch: :i386) do
cat X86::Linux.cat(*args)
cat X86::Linux.cat(*args, **kwargs)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pwnlib/shellcraft/generators/i386/linux/sh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module Linux
# @overload sh(argv: false)
#
# @see Generators::X86::Linux#sh
def sh(*args)
def sh(**kwargs)
context.local(arch: :i386) do
cat X86::Linux.sh(*args)
cat X86::Linux.sh(**kwargs)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/pwnlib/shellcraft/generators/x86/common/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ module X86
module Common
class << self
def define_arch_dependent_method(method)
define_method(method) do |*args|
define_method(method) do |*args, **kwargs|
if context.arch == 'amd64'
cat Amd64::Common.public_send(method, *args)
cat Amd64::Common.public_send(method, *args, **kwargs)
elsif context.arch == 'i386'
cat I386::Common.public_send(method, *args)
cat I386::Common.public_send(method, *args, **kwargs)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/pwnlib/shellcraft/generators/x86/common/setregs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ module Common
#
# @example
# context.arch = 'i386'
# puts shellcraft.setregs(rax: 'ebx', ebx: 'ecx', ecx: 0x123)
# # mov rax, rbx
# puts shellcraft.setregs({ eax: 'ebx', ebx: 'ecx', ecx: 0x123 })
# # mov eax, ebx
# # mov ebx, ecx
# # xor ecx, ecx
# # mov cx, 0x123
# @example
# context.arch = 'amd64'
# puts shellcraft.setregs(rdi: 'rsi', rsi: 'rdi')
# puts shellcraft.setregs({ rdi: 'rsi', rsi: 'rdi' })
# # xchg rdi, rsi
#
# puts shellcraft.setregs(rax: -1)
# puts shellcraft.setregs({ rax: -1 })
# # push -1
# # pop rax
#
# puts shellcraft.setregs({rax: -1}, stack_allowed: false)
# puts shellcraft.setregs({ rax: -1 }, stack_allowed: false)
# # mov rax, -1
def setregs(reg_context, stack_allowed: true)
abi = ::Pwnlib::ABI::ABI.default
Expand Down
4 changes: 2 additions & 2 deletions lib/pwnlib/shellcraft/shellcraft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def initialize
#
# With this method, +context.local(arch: 'amd64') { shellcraft.sh }+ will invoke
# {Shellcraft::Generators::Amd64::Linux#sh}.
def method_missing(method, *args, &block)
def method_missing(method, *args, **kwargs, &block)
mod = find_module_for(method)
return super if mod.nil?

mod.public_send(method, *args, &block)
mod.public_send(method, *args, **kwargs, &block)
end

# For +respond_to?+.
Expand Down
4 changes: 2 additions & 2 deletions test/asm_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def test_require
assert_match(/meow/, err.message)
end

def make_elf_file(*args)
elf = Asm.make_elf(*args)
def make_elf_file(data, **kwargs)
elf = Asm.make_elf(data, **kwargs)
stream = StringIO.new(elf)
[elf, ::ELFTools::ELFFile.new(stream)]
end
Expand Down
6 changes: 3 additions & 3 deletions test/logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class LoggerTest < MiniTest::Test
def setup
@logger = ::Pwnlib::Logger::LoggerType.new
class << @logger
def add(*args)
def add(*)
clear
super
@logdev.string
end

def indented(*args)
def indented(*, **)
clear
super(*args)
super
@logdev.string
end

Expand Down
16 changes: 8 additions & 8 deletions test/shellcraft/setregs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ def setup

def test_amd64
context.local(arch: 'amd64') do
assert_equal(<<-'EOS', @shellcraft.setregs(rax: 1, rbx: 'rax'))
assert_equal(<<-'EOS', @shellcraft.setregs({ rax: 1, rbx: 'rax' }))
mov rbx, rax
push 1
pop rax
EOS
assert_equal(<<-'EOS', @shellcraft.setregs(rax: 'SYS_write', rbx: 'rax'))
assert_equal(<<-'EOS', @shellcraft.setregs({ rax: 'SYS_write', rbx: 'rax' }))
mov rbx, rax
push 1 /* (SYS_write) */
pop rax
EOS
assert_equal(<<-'EOS', @shellcraft.setregs(rax: 'rbx', rbx: 'rax', rcx: 'rbx'))
assert_equal(<<-'EOS', @shellcraft.setregs({ rax: 'rbx', rbx: 'rax', rcx: 'rbx' }))
mov rcx, rbx
xchg rax, rbx
EOS
assert_equal(<<-'EOS', @shellcraft.setregs(rax: 1, rdx: 0))
assert_equal(<<-'EOS', @shellcraft.setregs({ rax: 1, rdx: 0 }))
push 1
pop rax
cdq /* rdx=0 */
EOS
# issue #50
assert_equal(<<-'EOS', @shellcraft.setregs(rdi: :rax, rsi: :rdi))
assert_equal(<<-'EOS', @shellcraft.setregs({ rdi: :rax, rsi: :rdi }))
mov rsi, rdi
mov rdi, rax
EOS
Expand All @@ -44,16 +44,16 @@ def test_amd64

def test_i386
context.local(arch: 'i386') do
assert_equal(<<-EOS, @shellcraft.setregs(eax: 1, ebx: 'eax'))
assert_equal(<<-EOS, @shellcraft.setregs({ eax: 1, ebx: 'eax' }))
mov ebx, eax
push 1
pop eax
EOS
assert_equal(<<-EOS, @shellcraft.setregs(eax: 'ebx', ebx: 'eax', ecx: 'ebx'))
assert_equal(<<-EOS, @shellcraft.setregs({ eax: 'ebx', ebx: 'eax', ecx: 'ebx' }))
mov ecx, ebx
xchg eax, ebx
EOS
assert_equal(<<-'EOS', @shellcraft.setregs(eax: 1, edx: 0))
assert_equal(<<-'EOS', @shellcraft.setregs({ eax: 1, edx: 0 }))
push 1
pop eax
cdq /* edx=0 */
Expand Down
4 changes: 2 additions & 2 deletions test/util/packing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def test_us
def test_up_rand
srand(217)
[8, 16, 32, 64].each do |sz|
u = ->(*x) { ::Pwnlib::Util::Packing.public_send("u#{sz}", *x) }
p = ->(*x) { ::Pwnlib::Util::Packing.public_send("p#{sz}", *x) }
u = ->(*x, **k) { ::Pwnlib::Util::Packing.public_send("u#{sz}", *x, **k) }
p = ->(*x, **k) { ::Pwnlib::Util::Packing.public_send("p#{sz}", *x, **k) }
100.times do
limit = (1 << sz)
val = rand(0...limit)
Expand Down

0 comments on commit de6f783

Please sign in to comment.