Skip to content

Commit

Permalink
Extract cc param and compile_c method out into Mixins::CC.
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Dec 15, 2023
1 parent edb060e commit 7a38292
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 192 deletions.
66 changes: 3 additions & 63 deletions lib/ronin/payloads/c_payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#

require 'ronin/payloads/binary_payload'
require 'ronin/payloads/mixins/cc'

module Ronin
module Payloads
Expand All @@ -28,6 +29,8 @@ module Payloads
#
class CPayload < BinaryPayload

include Mixins::CC

#
# Returns the type or kind of payload.
#
Expand All @@ -42,69 +45,6 @@ def self.payload_type
:c
end

#
# The default C compiler.
#
# @return [String]
#
def self.cc
ENV['CC'] || 'cc'
end

param :cc, required: true,
default: -> { cc },
desc: 'The C compiler to use'

#
# Compiles one or more source files using `cc`.
#
# @param [Array<String>] source_files
# The source file(s) to compile.
#
# @param [String] output
# The output file path.
#
# @param [Array<String>, Hash{Symbol,String => String}, nil] defs
# Additional macro definitions to pass to the compiler.
#
# @raise [ArgumentError]
# `defs` was not an Array or a Hash.
#
# @raise [BuildFailed]
# The `cc` command failed or is not installed.
#
# @since 0.2.0
#
def compile_c(*source_files, output: , defs: nil)
args = [params[:cc], '-o', output]

if defs
case defs
when Array
defs.each do |value|
args << "-D#{value}"
end
when Hash
defs.each do |name,value|
args << "-D#{name}=#{value}"
end
else
raise(ArgumentError,"defs must be either an Array or a Hash: #{defs.inspect}")
end
end

args.concat(source_files)

case system(*args)
when false
raise(BuildFailed,"cc command failed: #{args.join(' ')}")
when nil
raise(BuildFailed,"cc command not installed")
end
end

alias compile compile_c

end
end
end
96 changes: 96 additions & 0 deletions lib/ronin/payloads/mixins/cc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# frozen_string_literal: true
#
# ronin-payloads - A Ruby micro-framework for writing and running exploit
# payloads.
#
# Copyright (c) 2007-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# ronin-payloads is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ronin-payloads is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ronin-payloads. If not, see <https://www.gnu.org/licenses/>.
#

module Ronin
module Payloads
module Mixins
#
# Mixin for using the C compiler.
#
# @since 0.2.0
#
module CC
#
# The default C compiler.
#
# @return [String]
#
def self.cc
ENV['CC'] || 'cc'
end

def self.included(payload_class)
payload_class.param :cc, required: true,
default: -> { cc },
desc: 'The C compiler to use'
end

#
# Compiles one or more source files using `cc`.
#
# @param [Array<String>] source_files
# The source file(s) to compile.
#
# @param [String] output
# The output file path.
#
# @param [Array<String>, Hash{Symbol,String => String}, nil] defs
# Additional macro definitions to pass to the compiler.
#
# @raise [ArgumentError]
# `defs` was not an Array or a Hash.
#
# @raise [BuildFailed]
# The `cc` command failed or is not installed.
#
def compile_c(*source_files, output: , defs: nil)
args = [params[:cc], '-o', output]

if defs
case defs
when Array
defs.each do |value|
args << "-D#{value}"
end
when Hash
defs.each do |name,value|
args << "-D#{name}=#{value}"
end
else
raise(ArgumentError,"defs must be either an Array or a Hash: #{defs.inspect}")
end
end

args.concat(source_files)

case system(*args)
when false
raise(BuildFailed,"cc command failed: #{args.join(' ')}")
when nil
raise(BuildFailed,"cc command not installed")
end
end

alias compile compile_c
end
end
end
end
131 changes: 2 additions & 129 deletions spec/c_payload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,134 +6,7 @@
expect(described_class.superclass).to be(Ronin::Payloads::BinaryPayload)
end

describe ".cc" do
subject { described_class }

before do
@cc = ENV['CC']
ENV.delete('CC')
end

context "when ENV['CC'] is set" do
let(:cc) { 'gcc' }

before { ENV['CC'] = cc }

it "must return ENV['CC']" do
expect(subject.cc).to eq(cc)
end

after { ENV.delete('CC') }
end

context "when ENV['CC'] is not set" do
it "must return 'cc'" do
expect(subject.cc).to eq('cc')
end
end

after { ENV['CC'] = @cc if @cc }
end

describe "params" do
subject { described_class }

it "must define a :cc param" do
expect(subject.params[:cc]).to_not be_nil
end

it "must default the :cc param to #{described_class}.cc" do
expect(subject.params[:cc].default_value).to eq(subject.cc)
end
end

describe "#compile_c" do
let(:source_files) { %w[foo.c bar.c baz.c] }
let(:output) { 'output' }

it "must call system with params[:cc], the output and source files" do
expect(subject).to receive(:system).with(
subject.params[:cc],'-o',output,*source_files
).and_return(true)

subject.compile_c(*source_files, output: output)
end

context "when the defs: keyword argument is given" do
context "and it's an Array" do
let(:def1) { 'foo' }
let(:def2) { 'bar=baz' }
let(:defs) { [def1, def2] }

it "must append the values with '-D' flags" do
expect(subject).to receive(:system).with(
subject.params[:cc],
'-o', output,
"-D#{def1}",
"-D#{def2}",
*source_files
).and_return(true)

subject.compile_c(*source_files, output: output, defs: defs)
end
end

context "and it's a Hash" do
let(:name1) { "foo" }
let(:value1) { "1" }
let(:name2) { "bar" }
let(:value2) { "2" }

let(:def1) { "#{name1}=#{value1}" }
let(:def2) { "#{name2}=#{value2}" }
let(:defs) { {name1 => value1, name2 => value2} }

it "must append the values with '-D' flags" do
expect(subject).to receive(:system).with(
subject.params[:cc],
'-o', output,
"-D#{def1}",
"-D#{def2}",
*source_files
).and_return(true)

subject.compile_c(*source_files, output: output, defs: defs)
end
end

context "but it's not an Array or a Hash" do
let(:defs) { Object.new }

it do
expect {
subject.compile_c(*source_files, output: output, defs: defs)
}.to raise_error(ArgumentError,"defs must be either an Array or a Hash: #{defs.inspect}")
end
end
end

context "when system() returns false" do
let(:source_file) { 'foo.go' }

it do
allow(subject).to receive(:system).and_return(false)

expect {
subject.compile_c(source_file, output: output)
}.to raise_error(Ronin::Payloads::BuildFailed,"cc command failed: #{subject.params[:cc]} -o #{output} #{source_file}")
end
end

context "when system() returns nil" do
let(:source_file) { 'foo.go' }

it do
allow(subject).to receive(:system).and_return(nil)

expect {
subject.compile_c(source_file, output: output)
}.to raise_error(Ronin::Payloads::BuildFailed,"cc command not installed")
end
end
it "must include Ronin::Payloads::Mixins::CC" do
expect(described_class).to include(Ronin::Payloads::Mixins::CC)
end
end
Loading

0 comments on commit 7a38292

Please sign in to comment.