CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/lib/rex/parser/winscp_spec.rb
Views: 11655
1
2
INI_SECURITY = "[Configuration\\Security]\nUseMasterPassword=1\nMasterPasswordVerifier=\n"
3
4
USERNAME = 'username'
5
HOST = 'server.feralhosting.com'
6
PASSWORD='A35C7659654B2AB83C292F392E323D31392F392E2A392E723A392E3D3034332F2835323B723F33312F383A2F383A3B2F3B3B3B'
7
SAMPLE_INI = <<-END
8
[Sessions\\[email protected]]
9
HostName=#{HOST}
10
Timeout=6000
11
SshProt=3
12
UserName=#{USERNAME}
13
UpdateDirectories=0
14
Utf=1
15
Password=#{PASSWORD}
16
Shell=/bin/bash}
17
END
18
19
RSpec.describe Rex::Parser::WinSCP do
20
let(:target) do
21
d = Class.new { include Rex::Parser::WinSCP }
22
d.new
23
end
24
25
context "#parse_protocol" do
26
it "returns 'Unknown' for unknown protocols" do
27
expect(target.parse_protocol(nil)).to eq('Unknown')
28
expect(target.parse_protocol(99)).to eq('Unknown')
29
expect(target.parse_protocol('stuff')).to eq('Unknown')
30
end
31
32
it "returns 'SSH' for protocol 0" do
33
expect(target.parse_protocol(0)).to eq('SSH')
34
end
35
36
it "returns 'FTP' for protocol 5" do
37
expect(target.parse_protocol(5)).to eq('FTP')
38
end
39
end
40
41
context "#decrypt_next_char" do
42
it "returns 0 and the pwd if pwd length <= 0" do
43
r, pwd = target.decrypt_next_char('')
44
expect(r).to eq(0)
45
expect(pwd).to eq('')
46
end
47
48
it "strips the first two characters from the return value" do
49
_, pwd = target.decrypt_next_char('A3')
50
expect(pwd).to eq('')
51
end
52
53
it "returns 255 for 'A3'" do
54
r, _ = target.decrypt_next_char('A3')
55
expect(r).to eq(Rex::Parser::WinSCP::PWDALG_SIMPLE_FLAG)
56
end
57
end
58
59
context "#decrypt_password" do
60
it "returns 'sdfsdfgsggg' for the example password" do
61
expect(target.decrypt_password(PASSWORD, "#{USERNAME}#{HOST}")).to eq('sdfsdfgsggg')
62
end
63
end
64
65
context "#parse_ini" do
66
it "raises a RuntimeError if ini is nil or empty" do
67
expect { target.parse_ini('') }.to raise_error(RuntimeError, /No data/i)
68
expect { target.parse_ini(nil) }.to raise_error(RuntimeError, /No data/i)
69
end
70
71
it "raises a RuntimeError if UseMasterPassword is 1" do
72
expect { target.parse_ini(INI_SECURITY) }.to raise_error(RuntimeError, /Master/i)
73
end
74
75
it "parses the example ini" do
76
r = target.parse_ini(SAMPLE_INI).first
77
expect(r[:hostname]).to eq(HOST)
78
expect(r[:password]).to eq('sdfsdfgsggg')
79
expect(r[:username]).to eq(USERNAME)
80
expect(r[:protocol]).to eq('SSH')
81
expect(r[:portnumber]).to eq(22)
82
end
83
end
84
85
context "#read_and_parse_ini" do
86
it "returns nil if file is empty or doesn't exist" do
87
expect(File).to receive(:read).and_return(nil)
88
expect(target.read_and_parse_ini('blah')).to be nil
89
end
90
91
it "parses the example ini and return a single result" do
92
expect(File).to receive(:read).and_return(SAMPLE_INI)
93
expect(target.read_and_parse_ini(SAMPLE_INI).count).to eq 1
94
end
95
end
96
end
97
98
99