Skip to content

Instantly share code, notes, and snippets.

@MSylvia
Forked from 0x0dea/process_gsub.rb
Last active August 29, 2015 14:20
Show Gist options
  • Save MSylvia/3b45a04f93264200dca6 to your computer and use it in GitHub Desktop.
Save MSylvia/3b45a04f93264200dca6 to your computer and use it in GitHub Desktop.
def Process.gsub pat, sub
mem = File.open('/proc/self/mem', 'r+')
maps = File.open('/proc/self/maps')
maps.each do |map|
from, to, perms, offset = map.scan(/(\h+)-(\h+) (\S+) (\h+)/)[0]
if perms['rw']
from, to = [from, to].map { |addr| addr.hex + offset.hex }
data = mem.tap { |m| m.seek from }.read(to - from) rescue next
i = -1
while i = data.index(pat, i + 1)
match = data[pat]
mem.seek(from + i)
mem << data[i, match.size].sub(pat, sub)
end
end
break if map[/\[heap\]$/]
end
mem.close
maps.close
end
def foo
puts 'This is strange.'
puts 'This is foolish.'
end
foo
Process.gsub(/(This is) [a-z]+\./, '\1 amazing!')
foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment