forked from fozavci/viproxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplace_zip.rb
32 lines (27 loc) · 857 Bytes
/
replace_zip.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require "zip"
h = self.index("\x50\x4b\x03\x04")
if h
puts "zip signature found"
begin
Zip::InputStream.open(StringIO.new(self[h, self.length])) do |io|
while entry = io.get_next_entry
puts "entry: #{entry.name}"
puts "content: #{io.read}"
end
end
rescue Exception => msg
# buffer data, wait for the next packet
:wait
else
# save intercepted zip archive
#File.open("/tmp/1.zip", "wb") {|f| f.write(self[h, self.length]) }
# read new one
newzip = File.new("/tmp/1_mod.zip", "r").read
# calculate length difference between them
diff = self.length - h - newzip.force_encoding('ASCII-8BIT').length
# substitute content of the old archive with the new one
self[h, self.length] = newzip.force_encoding('ASCII-8BIT') + "\x00"*diff
# send packet further
:ok
end
end