Skip to content

Commit

Permalink
feat: enable HTTP header customization
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Nov 6, 2019
1 parent 8a06889 commit b6ef731
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/apullo/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
module Apullo
class CLI < Thor
desc "check [Target]", "Take fingerprints from a target(IP, domain or URL)"
method_option :headers, type: :hash, default: {}
def check(target)
target = Target.new(target)
headers = options["headers"]

results = build_results(target)
results = build_results(target, headers: headers)
meta = { target: target.id }
results = results.merge(meta: meta)

puts JSON.pretty_generate(results)
end

no_commands do
def build_results(target)
def build_results(target, headers: {})
unless target.valid?
return {
error: "Invalid target is given. Target should be an IP, domain or URL."
Expand All @@ -27,6 +29,8 @@ def build_results(target)

Parallel.map(Apullo.fingerprints) do |klass|
fingerprint = klass.new(target)
fingerprint.headers = headers if fingerprint.respond_to?(:headers=)

[fingerprint.name, fingerprint.results]
end.to_h
end
Expand Down
13 changes: 12 additions & 1 deletion lib/apullo/fingerprints/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
module Apullo
module Fingerprint
class HTTP < Base
attr_writer :headers

def initialize(target)
@target = target
@headers = {}
end

def results
@results ||= [].tap do |out|
get(target.uri.path)
Expand Down Expand Up @@ -53,6 +60,10 @@ def favicon

private

def headers
@headers.compact
end

def default_favicon_url
"#{target.uri.scheme}://#{target.uri.host}:#{target.uri.port}/favicon.ico"
end
Expand All @@ -73,7 +84,7 @@ def favicon_url
def get(path, limit: 3)
http = build_http
path = path.empty? ? "/" : path
request = Net::HTTP::Get.new(path)
request = Net::HTTP::Get.new(path, headers)
response = http.request request

location = response["Location"]
Expand Down
8 changes: 8 additions & 0 deletions spec/fingerprints/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@
expect(results.dig(:cert, key)).to eq(nil)
end
end

context "when given headers" do
it do
subject.headers = { "User-Agent": "foo" }
results = subject.results
expect(results).to be_a(Hash)
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b6ef731

Please sign in to comment.