-
-
Save MSylvia/3b45a04f93264200dca6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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