Skip to content

Commit

Permalink
Merge pull request #3 from ninoseki/improve-detection
Browse files Browse the repository at this point in the history
refactor: improve detection method
  • Loading branch information
ninoseki committed Sep 17, 2018
2 parents 02ea288 + 2a1992d commit b331607
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 29 deletions.
1 change: 1 addition & 0 deletions lib/miteru.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "miteru/error"
require "miteru/website"
require "miteru/crawler"
require "miteru/cli"
require "miteru/version"
Expand Down
18 changes: 6 additions & 12 deletions lib/miteru/crawler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,17 @@ def suspicous_urls
res["results"].map { |result| result.dig("task", "url") }
end

def has_kit?(url)
begin
res = get(url)
rescue HTTPResponseError => _
false
end

rules = ["Index of", ".zip"]
rules.all? { |rule| res.include? rule }
end

def execute
pool = Thread.pool(threads)
results = []

suspicous_urls.each do |url|
pool.process { results << url if has_kit?(url) }
pool.process do
doc = Website.new(url)
results << url if doc.has_kit?
rescue HTTPResponseError => _
next
end
end
pool.shutdown

Expand Down
49 changes: 49 additions & 0 deletions lib/miteru/website.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require "http"
require "oga"

module Miteru
class Website
attr_reader :url
def initialize(url)
@url = url
end

def title
doc.at_css("title")&.text
end

def zip_files
@zip_files ||= doc.css("a").map do |a|
href = a.get("href")
href&.end_with?(".zip") ? href : nil
end.compact
end

def index?
title == "Index of /"
end

def zip_files?
!zip_files.empty?
end

def has_kit?
index? && zip_files?
end

private

def get
res = HTTP.get(url)
raise HTTPResponseError if res.code != 200

res.body.to_s
end

def doc
@doc ||= Oga.parse_html(get)
end
end
end
1 change: 1 addition & 0 deletions miteru.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "webmock", "~> 3.4"

spec.add_dependency "http", "~> 3.3"
spec.add_dependency "oga", "~> 2.15"
spec.add_dependency "thor", "~> 0.19"
spec.add_dependency "thread", "~> 0.2.2"
end
24 changes: 7 additions & 17 deletions spec/crawler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,15 @@
expect(results.length).to eq(100)
end
end
describe "#has_kit?" do
context "when giving a url which contains a phishint kit" do
before do
path = File.expand_path("./fixtures/index.html", __dir__)
data = File.read(path)

allow_any_instance_of(Miteru::Crawler).to receive(:get).and_return(data)
end
it "should return true" do
expect(subject.has_kit?("http://localhost")).to eq(true)
end
end
end
context "when giving a url which doesn't contain a phishint kit" do
describe "#execute" do
before do
allow_any_instance_of(Miteru::Crawler).to receive(:get).and_return("None")
allow_any_instance_of(Miteru::Crawler).to receive(:suspicous_urls).and_return(%w(http://localhost))
allow_any_instance_of(Miteru::Website).to receive(:has_kit?).and_return(true)
end
it "should return false" do
expect(subject.has_kit?("http://localhost")).to eq(false)
it "should return an Array" do
results = subject.execute
expect(results).to be_an(Array)
expect(results.length).to eq(1)
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/website_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

RSpec.describe Miteru::Website, :vcr do
subject { Miteru::Website }
describe "#has_kit?" do
context "when giving a url which contains a phishint kit" do
before do
path = File.expand_path("./fixtures/index.html", __dir__)
data = File.read(path)

allow_any_instance_of(Miteru::Website).to receive(:get).and_return(data)
end
it "should return true" do
expect(subject.new("http://localhost").has_kit?).to eq(true)
end
end
end
context "when giving a url which doesn't contain a phishint kit" do
before do
allow_any_instance_of(Miteru::Website).to receive(:get).and_return("None")
end
it "should return false" do
expect(subject.new("http://localhost").has_kit?).to eq(false)
end
end
end

0 comments on commit b331607

Please sign in to comment.