70 lines
2.1 KiB
Ruby
70 lines
2.1 KiB
Ruby
# -*- coding: binary -*-
|
|
|
|
module Msf
|
|
|
|
# This mixin provides helper functions for building Git repositories
|
|
module Exploit::Git
|
|
|
|
class GitObject
|
|
|
|
attr_reader :type, :content, :sha1, :path, :compressed
|
|
|
|
def initialize(type, content, sha1, compressed)
|
|
@type = type
|
|
@content = content
|
|
@sha1 = sha1
|
|
@compressed = compressed
|
|
@path = "#{@sha1[0...2]}/#{@sha1[2..40]}"
|
|
end
|
|
|
|
def self.build_commit_object(tree_sha1)
|
|
full_name = Faker::Name.name
|
|
email = Faker::Internet.email(name: full_name, separators: ['-', '_'])
|
|
company = Faker::Company.name
|
|
|
|
tstamp = Time.now.to_i
|
|
author_time = rand(tstamp)
|
|
commit_time = rand(author_time)
|
|
tz_off = rand(10)
|
|
commit_msg = "author #{full_name} <#{email}> #{author_time} -0#{tz_off}00\n" \
|
|
"committer #{full_name} <#{email}> #{commit_time} -0#{tz_off}00\n" \
|
|
"\n" \
|
|
"Initial commit to open git repository for #{company}!\n"
|
|
|
|
commit = "tree #{tree_sha1}\n#{commit_msg}"
|
|
sha1, compressed = build_object('commit', commit)
|
|
GitObject.new('commit', commit, sha1, compressed)
|
|
end
|
|
|
|
def self.build_blob_object(content)
|
|
sha1, compressed = build_object('blob', content)
|
|
GitObject.new('blob', content, sha1, compressed)
|
|
end
|
|
|
|
# Accepts array of hashes where
|
|
# hash consists file name, mode,
|
|
# and sha1 hash for contents of file
|
|
def self.build_tree_object(tree_entries)
|
|
tree = ''
|
|
unless tree_entries.is_a?(Array)
|
|
tree_entries = [ tree_entries ]
|
|
end
|
|
|
|
tree_entries.each do |entry|
|
|
tree += "#{entry[:mode]} #{entry[:file_name]}\0#{[entry[:sha1]].pack('H*')}"
|
|
end
|
|
|
|
sha1, compressed = build_object('tree', tree)
|
|
GitObject.new('tree', tree, sha1, compressed)
|
|
end
|
|
|
|
def self.build_object(type, content)
|
|
# taken from http://schacon.github.io/gitbook/7_how_git_stores_objects.html
|
|
header = "#{type} #{content.size}\0"
|
|
store = header + content
|
|
[Digest::SHA1.hexdigest(store), Zlib::Deflate.deflate(store)]
|
|
end
|
|
end
|
|
end
|
|
end
|