Skip to content

Commit 8af6bae

Browse files
Merge pull request rapid7#20906 from rudraditya21/fix/ssh-cmd-exec-trailing-newlines
Fix SSH command shells dying on cmd_exec with trailing newline
2 parents 2289e88 + 5f0ada0 commit 8af6bae

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/msf/core/session/provider/single_command_shell.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def shell_command_token_base(cmd, timeout=10, command_separator="\n")
145145

146146
# Send the command to the session's stdin.
147147
delimiter = "echo #{token}"
148-
if cmd.strip.end_with?(command_separator)
148+
if cmd.match?(/\r?\n\z/) || cmd.strip.end_with?(command_separator)
149149
# This command already ends with a delimiter - don't need to add another one
150150
shell_data = cmd + "#{delimiter}#{command_termination}"
151151
else
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe Msf::Session::Provider::SingleCommandShell do
6+
class DummySingleCommandShell
7+
include Msf::Session::Provider::SingleCommandShell
8+
9+
attr_reader :writes
10+
11+
def initialize
12+
@writes = []
13+
end
14+
15+
def platform
16+
'linux'
17+
end
18+
19+
def shell_init
20+
true
21+
end
22+
23+
def shell_read(*)
24+
nil
25+
end
26+
27+
def shell_write(buf)
28+
@writes << buf
29+
end
30+
31+
def shell_close
32+
true
33+
end
34+
35+
def shell_read_until_token(_token, _wanted_idx = 0, _timeout = 10)
36+
''
37+
end
38+
end
39+
40+
describe '#shell_command_token_base' do
41+
it 'does not inject a separator after a trailing newline' do
42+
shell = DummySingleCommandShell.new
43+
44+
shell.shell_command_token_base("id\n", 1, ';')
45+
46+
expect(shell.writes.last).not_to match(/\n;/)
47+
end
48+
end
49+
end

0 commit comments

Comments
 (0)