Skip to content

Commit

Permalink
Merge pull request #29 from girorme/feat/multi-ocurrences
Browse files Browse the repository at this point in the history
chore: add multi ocurrences
  • Loading branch information
girorme authored Jun 23, 2024
2 parents 32b6b7d + cef5681 commit cc23b6b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
8 changes: 7 additions & 1 deletion lib/binoculo/args.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ defmodule Binoculo.Args do
value_name: "read",
short: "-r",
long: "--read",
help: "Save only responses that match with this string, e.g: Apache"
help: "Save only responses that match with this string, e.g: Apache | nginx,php",
parser: fn read_payload ->
case String.contains?(read_payload, ",") do
true -> {:ok, String.split(read_payload, ",")}
false -> {:ok, read_payload}
end
end
]
]
)
Expand Down
6 changes: 2 additions & 4 deletions lib/binoculo/cross_saver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Binoculo.CrossSaver do
Save to multiple sources
"""

alias Binoculo.{Config, Msearch, Results, Util}
alias Binoculo.{Config, Msearch, Refiner, Results, Util}

def save_results() do
check_and_save_to_file()
Expand Down Expand Up @@ -60,9 +60,7 @@ defmodule Binoculo.CrossSaver do
results

read_payload ->
Enum.filter(results, fn %{response: response} ->
Regex.match?(~r/#{read_payload}/i, response)
end)
Refiner.find_occurrences_in_responses(read_payload, results)
end
end
end
13 changes: 13 additions & 0 deletions lib/binoculo/refiner.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Binoculo.Refiner do
def find_occurrences_in_responses(patterns, responses) when is_list(patterns) do
Enum.filter(responses, fn %{response: response} ->
Enum.all?(patterns, fn pattern -> String.contains?(response, pattern) end)
end)
end

def find_occurrences_in_responses(pattern, responses) do
Enum.filter(responses, fn %{response: response} ->
String.contains?(response, pattern)
end)
end
end
28 changes: 27 additions & 1 deletion test/args_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,39 @@ defmodule ArgsTest do

alias Binoculo.Args

describe "parse args" do
describe "parse_args/1" do
test "should parse valid args" do
args = ["--range", "192.168.101.1", "-p", "80", "--output", "my_file.txt"]
parsed_args = Args.parse_args(args) |> Map.from_struct()

assert %{options: %{host_notation: "192.168.101.1", ports: [80], output: "my_file.txt"}} =
parsed_args
end

test "should parse read payload with one pattern" do
args = ["--range", "192.168.101.1", "-p", "80", "--read", "nginx"]
parsed_args = Args.parse_args(args) |> Map.from_struct()

assert %{
options: %{
host_notation: "192.168.101.1",
ports: [80],
read: "nginx"
}
} = parsed_args
end

test "should parse read payload with multiple patterns" do
args = ["--range", "192.168.101.1", "-p", "80", "--read", "nginx,php"]
parsed_args = Args.parse_args(args) |> Map.from_struct()

assert %{
options: %{
host_notation: "192.168.101.1",
ports: [80],
read: ["nginx", "php"]
}
} = parsed_args
end
end
end
27 changes: 27 additions & 0 deletions test/refiner_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule RefinerTest do
use ExUnit.Case, async: true

alias Binoculo.Refiner

@responses [
%{response: "hello and world"},
%{response: "world"},
%{response: "test"},
%{response: "test xoo"}
]

describe "Refiner.find_occurrences_in_responses/2" do
test "should find occurrences in responses with single pattern" do
assert [%{response: "test"}, %{response: "test xoo"}] ==
Refiner.find_occurrences_in_responses("test", @responses)
end

test "should find occurrences in responses with multiple patterns (AND)" do
assert [%{response: "hello and world"}] ==
Refiner.find_occurrences_in_responses(["hello", "world"], @responses)

assert [%{response: "test xoo"}] ==
Refiner.find_occurrences_in_responses(["xoo"], @responses)
end
end
end

0 comments on commit cc23b6b

Please sign in to comment.