Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/core/exploit/git.rb
Views: 11784
# -*- coding: binary -*-12module Msf34# This mixin provides helper functions for building Git repositories5module Exploit::Git67class GitObject89attr_reader :type, :content, :sha1, :path, :compressed1011def initialize(type, content, sha1, compressed)12@type = type13@content = content14@sha1 = sha115@compressed = compressed16@path = "#{@sha1[0...2]}/#{@sha1[2..40]}"17end1819# Wrapper for `build_object()` to create20# a GitObject of type `commit`21# @param [ Hash ] containing sha1s for the tree,22# (optional) parent sha1 and optional data23# such as commit message, committer name,24# company name, and email address25def self.build_commit_object(opts = {})26full_name = opts[:name] || Faker::Name.name27email = opts[:email] || Faker::Internet.email(name: full_name, separators: ['-', '_'])28company = opts[:company] || Faker::Company.name29commit_text = opts[:message] || "Initial commit to open git repository for #{company}!"30tree_sha1 = opts[:tree_sha1]31parent_sha1 = opts[:parent_sha1]3233tstamp = Time.now.to_i34author_time = rand(tstamp)35commit_time = rand(author_time)36tz_off = rand(10)3738commit_msg = "tree #{tree_sha1}\n"39commit_msg << "parent #{parent_sha1}\n" unless parent_sha1.nil?40commit_msg << "author #{full_name} <#{email}> #{author_time} -0#{tz_off}00\n"41commit_msg << "committer #{full_name} <#{email}> #{commit_time} -0#{tz_off}00\n"42commit_msg << "\n"43commit_msg << "#{commit_text}\n"4445sha1, compressed = build_object('commit', commit_msg)46GitObject.new('commit', commit_msg, sha1, compressed)47end4849# Wrapper for `build_object()` to create50# a GitObject of type `blob`51# @param [ String ] the data that the object52# will represent53def self.build_blob_object(content)54sha1, compressed = build_object('blob', content)55GitObject.new('blob', content, sha1, compressed)56end5758# Creates a GitObject of type `tree`59# @param [ Hash ] entries containing60# the file mode, name, and sha1 from61# a previously-created `blob` object62# Ex:63# {64# mode: '100755', file_name: 'hooks',65# sha1: 'a372436ad8331b380e20e8c9861f547063d76a46'66# }67def self.build_tree_object(tree_entries)68tree = ''69unless tree_entries.is_a?(Array)70tree_entries = [ tree_entries ]71end7273tree_entries.each do |entry|74tree += "#{entry[:mode]} #{entry[:file_name]}\0#{[entry[:sha1]].pack('H*')}"75end7677sha1, compressed = build_object('tree', tree)78GitObject.new('tree', tree, sha1, compressed)79end8081# Generates a git object of the specified82# type, ex: blob, tree, commit83#84# @param [ String ] type of object to create85# @param [ String ] the data that the resulting86# object will represent87# Returns an Array containing the sha1 hash88# and Zlib-compressed data89def self.build_object(type, content)90# taken from http://schacon.github.io/gitbook/7_how_git_stores_objects.html91header = "#{type} #{content.size}\0"92store = header + content93[Digest::SHA1.hexdigest(store), Rex::Text.zlib_deflate(store, Zlib::DEFAULT_COMPRESSION)]94end9596# Given a sha1 and list of Git objects97# find the object with the matching sha198#99# @param [ String ] sha1 of the object to find100# @param [ Array ] list of GitObjects to search101# @return GitObject with the matching sha1, nil otherwise102def self.find_object(sha1, objs = [])103return nil if objs.empty?104105objs.find { |obj| obj.sha1 == sha1 }106end107end108end109end110111112