Skip to content

Commit

Permalink
Stash some handy msf-dev tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Tod Beardsley committed Mar 10, 2015
1 parent d360865 commit 91759b1
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
55 changes: 55 additions & 0 deletions resplat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env ruby
# -*- coding: binary -*-

# Replace comment splat with something shorter and
# more sensible.
#
# Usage:
# metasploit-framework$ ./tools/dev/resplat.rb [path]
#
# Some cargo-culting of tools/dev/retab.rb

require 'fileutils'
require 'find'

dir = ARGV[0] || "."

raise ArgumentError, "Need a filename or directory" unless (dir and File.readable? dir)

def is_ruby?(fname)
return true if fname =~ /\.rb$/
end

def resplat(line)
if line =~ /This file is part of the Metasploit Framework/
# This module requires Metasploit: http://metasploit.com/download
elsif line =~ /# redistribution and commercial restrictions\./
return "# Current source: https://github.com/rapid7/metasploit-framework\n"
else
return nil
end
end

Find.find(dir) do |infile|
next if infile =~ /\.git[\x5c\x2f]/
next unless File.file? infile
next unless is_ruby? infile
outfile = infile

data = File.open(infile, "rb") {|f| f.read f.stat.size}
fixed = []
data.each_line do |line|
case line
when /^[\s]*#( ##)? This file is part of the Metasploit Framework and may be subject to/, /^[\s]*# redistribution and commercial restrictions\. Please see the Metasploit/, /^[\s]*# web site for more information on licensing and terms of use\./, /^[\s]*#[\s]{1,3}http:\/\/metasploit.com\/(framework\/)?/, /^# Framework web site for more information on licensing and terms of use./
new_line = resplat(line)
fixed << new_line if new_line
else
fixed << line
end
end

fh = File.open(outfile, "wb")
fh.write fixed.join
fh.close
puts "Resplatted #{fh.path}"
end
70 changes: 70 additions & 0 deletions retab.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env ruby
# -*- coding: binary -*-

# Replace leading tabs with 2-width spaces.
# I'm sure there's a sed/awk/perl oneliner that's
# a million times better but this is more readable for me.
#
# Usage:
# metasploit-framework$ ./tools/dev/retab.rb [path]
#
# If local backups are desired, prepend with "MSF_RETAB_BACKUPS" set,
# like so:
# metasploit-framework$ MSF_RETAB_BACKUPS=1 ./tools/dev/retab.rb [path]

require 'fileutils'
require 'find'

dir = ARGV[0] || "."
keep_backups = !!(ENV['MSF_RETAB_BACKUPS'] || ENV['MSF_RETAB_BACKUP'])
puts "Keeping .notab backups" if keep_backups

raise ArgumentError, "Need a filename or directory" unless (dir and File.readable? dir)

def is_ruby?(fname)
return true if fname =~ /\.rb$/
file_util = ""
begin
file_util = %x{which file}.to_s.chomp
rescue Errno::ENOENT
end
if File.executable? file_util
file_fingerprint = %x{#{file_util} #{fname}}
!!(file_fingerprint =~ /Ruby script/)
end
end

Find.find(dir) do |infile|
next if infile =~ /\.git[\x5c\x2f]/
next unless File.file? infile
next unless is_ruby? infile
outfile = infile

if keep_backups
backup = "#{infile}.notab"
FileUtils.cp infile, backup
end

data = File.open(infile, "rb") {|f| f.read f.stat.size}
fixed = []
data.each_line do |line|
fixed << line
next unless line =~ /^\x09/
index = []
i = 0
line.each_char do |char|
break unless char =~ /[\x20\x09]/
index << i if char == "\x09"
i += 1
end
index.reverse.each do |idx|
line[idx] = " "
end
fixed[-1] = line
end

fh = File.open(outfile, "wb")
fh.write fixed.join
fh.close
puts "Retabbed #{fh.path}"
end
28 changes: 28 additions & 0 deletions set_binary_encoding.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env ruby
# -*- coding: binary -*-

str = '# -*- coding: binary -*-'

fname = ARGV.shift || exit
data = ''
done = nil
fd = ::File.open(fname, "rb")
fd.each_line do |line|
if line =~ /^#.*coding:.*/
done = true
end

if not done
unless line =~ /^#\!.*env ruby/
data << str + "\n"
done = true
end
end

data << line
end
fd.close

fd = ::File.open(fname, "wb")
fd.write(data)
fd.close

0 comments on commit 91759b1

Please sign in to comment.