Class: Hijack::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/hijack/payload.rb

Class Method Summary collapse

Class Method Details

.inject(pid) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/hijack/payload.rb', line 3

def self.inject(pid)
  gdb = nil
  trap('SIGINT') do
    puts
    @received_sigint = true
  end
  gdb = GDB.new(pid)
  gdb.eval(payload(pid))
  gdb.detach
  exit if @received_sigint
end

.payload(pid) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hijack/payload.rb', line 15

def self.payload(pid)
  <<-RUBY
    require 'stringio'
    require 'drb'

    unless defined?(Hijack)
      module Hijack
        class OutputCopier
          def self.remote
            @remote
          end

          def self.stop
            @remote = nil
            [$stdout, $stderr].each do |io|
              if io.respond_to?(:write_with_copying)
                class << io
                  alias_method :write, :write_without_copying
                  remove_method :write_with_copying
                end
              end
            end
          end

          def self.start(pid)
            @remote = DRbObject.new(nil, 'drbunix://tmp/hijack.' + pid + '.sock')

            class << $stdout
              def write_with_copying(str)
                write_without_copying(str)
                begin
                  Hijack::OutputCopier.remote.write('stdout', str)
                rescue Exception
                  Hijack.stop
                end
              end
              alias_method :write_without_copying, :write
              alias_method :write, :write_with_copying
            end

            class << $stderr
              def write_with_copying(str)
                write_without_copying(str)
                begin
                  Hijack::OutputCopier.remote.write('stderr', str)
                rescue Exception
                  Hijack.stop
                end
              end
              alias_method :write_without_copying, :write
              alias_method :write, :write_with_copying
            end
          end
        end

        class Evaluator
          def initialize(context)
            @context = context
            @file = __FILE__
          end

          def evaluate(rb)
            if rb =~ /__hijack_output_receiver_ready_([\\d]+)/
              OutputCopier.start($1)
            elsif rb =~ /__hijack_get_remote_file_name/
              @file
            elsif rb =~ /__hijack_exit/
              Hijack.stop
            else
              @context.instance_eval(rb)
            end
          end
        end

        def self.start(context)
          return if @service && @service.alive?
          evaluator = Hijack::Evaluator.new(context)
          @service = DRb.start_service('#{Hijack.socket_for(pid)}', evaluator)
          File.chmod(0600, '#{Hijack.socket_path_for(pid)}')
        end

        def self.stop
          begin
            OutputCopier.stop
            @service.stop_service
            @service = nil
          rescue Exception
          end
        end
      end
    end
    Hijack.start(self)
  RUBY
end