This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
file.ex
129 lines (113 loc) · 3.38 KB
/
file.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
defmodule Helix.Software.Public.File do
alias HELL.IPv4
alias Helix.Cache.Query.Cache, as: CacheQuery
alias Helix.Network.Model.Network
alias Helix.Network.Model.Tunnel
alias Helix.Process.Model.Process
alias Helix.Server.Model.Server
alias Helix.Software.Action.Flow.File, as: FileFlow
alias Helix.Software.Action.Flow.File.Transfer, as: FileTransferFlow
alias Helix.Software.Model.File
alias Helix.Software.Model.Storage
alias Helix.Software.Query.File, as: FileQuery
alias Helix.Software.Query.Storage, as: StorageQuery
@type download_errors ::
{:error, {:file, :not_found}}
| {:error, {:storage, :not_found}}
| {:error, :internal}
@spec download(Tunnel.t, Storage.idt, File.idt) ::
{:ok, Process.t}
| download_errors
@doc """
Starts FileTransferProcess, responsible for downloading `file_id` into the
given storage.
"""
def download(tunnel, storage, file_id = %File.ID{}) do
with file = %{} <- FileQuery.fetch(file_id) do
download(tunnel, storage, file)
else
_ ->
{:error, {:file, :not_found}}
end
end
def download(tunnel, storage_id = %Storage.ID{}, file) do
storage = StorageQuery.fetch(storage_id)
if storage do
download(tunnel, storage, file)
else
{:error, {:storage, :not_found}}
end
end
def download(tunnel = %Tunnel{}, storage = %Storage{}, file = %File{}) do
network_info =
%{
gateway_id: tunnel.gateway_id,
destination_id: tunnel.destination_id,
network_id: tunnel.network_id,
bounces: [] # TODO 256
}
download(:download, file, storage, network_info)
end
def pftp_download(network_id, gateway_id, pftp, storage, file) do
network_info =
%{
gateway_id: gateway_id,
destination_id: pftp.server_id,
network_id: network_id,
bounces: [] # TODO 256
}
download(:pftp_download, file, storage, network_info)
end
# def pftp_add_file(pftp = %PublicFTP{}, file = %File{}) do
# end
defp download(type, file, storage, network_info) do
case FileTransferFlow.transfer(type, file, storage, network_info) do
{:ok, process} ->
{:ok, process}
{:error, _} ->
{:error, :internal}
end
end
@spec bruteforce(Server.id, Network.id, IPv4.t, [Server.id]) ::
{:ok, Process.t}
| {:error, %{message: String.t}}
| FileFlow.error
@doc """
Starts a bruteforce attack against `(network_id, target_ip)`, originating from
`gateway_id` and having `bounces` as intermediaries.
"""
def bruteforce(gateway_id, network_id, target_ip, bounces) do
create_params = fn ->
with \
{:ok, target_server_id} <-
CacheQuery.from_nip_get_server(network_id, target_ip)
do
%{
target_server_id: target_server_id,
network_id: network_id,
target_server_ip: target_ip
}
end
end
create_meta = fn ->
%{bounces: bounces}
end
get_cracker = fn ->
FileQuery.fetch_best(gateway_id, :bruteforce)
end
with \
params = %{} <- create_params.(),
meta = create_meta.(),
cracker = %{} <- get_cracker.() || :no_cracker,
{:ok, process} <-
FileFlow.execute_file(cracker, gateway_id, params, meta)
do
{:ok, process}
else
:no_cracker ->
{:error, %{message: "cracker_not_found"}}
error ->
error
end
end
end