Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn when lhost set to 127.0.0.1 #8041

Merged
merged 13 commits into from Mar 16, 2017
Merged

Conversation

bermannoah
Copy link
Contributor

@bermannoah bermannoah commented Mar 1, 2017

See issue #7188. This pull request adds an alert if you set LHOST to a loopback address (e.g. 127.0.01). Please let me know if there is any additional behavior desired or if I ought to change the warning message. I set the alert to a print_warning as requested.

Verification

The steps needed to make sure this thing works:

  • ./msfconsole
  • use exploit/multi/handler
  • set payload android/meterpreter/reverse_tcp
  • set lhost 127.0.0.1
  • set lport 4444
  • exploit
  • Verify: It will still set lhost to 127.0.0.1 but it will print a warning: "You are attempting to listen on a loopback address by setting LHOST to 127.0.0.1, did you mean to set ReverseListenerBindAddress instead?"
  • set lhost 0.0.0.0
  • export
  • Verify: It will set lhost to that address with no warnings. (Unless you use a different address that produces a warning.)
  • rake spec to test that all tests still pass.

Console Output

msf > exploit 
[ ! ] You are attempting to listen on a loopback address by setting LHOST to 127.0.0.1, did you mean to set ReverseListenerBindAddress instead?

[ * ] Started reverse TCP handler on 127.0.0.1:4444
[ * ] Starting the payload handler...

@busterb
Copy link
Member

busterb commented Mar 2, 2017

thanks @bermannoah . I think we may want to move this validation somewhere else. It seems odd to have this in the middle of the command dispatcher, since you can set LHOST from various places.

ip = args[1]

if (name.upcase == "LHOST" && ip == "127.0.0.1")
print_error("You may experience errors with this choice of address for LHOST.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wherever we decide to put this check, if print_warning is available, please use that instead. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wvu-r7: made the change -- thanks!

@bermannoah
Copy link
Contributor Author

@busterb: is there any place in particular you'd recommend moving it? I tried to find a more widely available place to put it and this seemed the most... succinct?

@busterb
Copy link
Member

busterb commented Mar 3, 2017

hi @bermannoah , I would suggest instead putting the warning inside of lib/msf/core/handler/reverse.rb inside of setup_handler right after it successfully sets up a listening socket, so it warns when it actually starts listening, rather than assignment from the UI. That will also allow it to warn from other contexts as well.

That also puts the warning where the actual networking code lives. You could also check for 127.0.0.0/8 or ::1 instead of just the single IPv4 loopback address.

@rwhitcroft
Copy link
Contributor

rwhitcroft commented Mar 3, 2017

Might need to go in reverse_http.rb as well to handle http(s) stuff, among others. Is there a higher base class I'm not seeing?

@busterb
Copy link
Member

busterb commented Mar 4, 2017

I think you're right @rwhitcroft.

@cbrnrd cbrnrd mentioned this pull request Mar 7, 2017
11 tasks
@busterb
Copy link
Member

busterb commented Mar 7, 2017

I think adding the test here is appropriate so you catch all of the loopback IPs: https://github.com/rapid7/metasploit-framework/pull/8063/files#r104604972

I also think I see the root cause for session instability in the original bug report. If you're port forwarding over a tunnel, you probably want to set ReverseListenerBindAddress to 127.0.0.1, but leave LHOST (which gets embedded into your payload) as an externally routable IP. Otherwise, your staged payload will likely be connecting to the wrong address, unless you're a developer testing payloads.

So, the message might say 'You are attempting to listen on a loopback address by setting LHOST to #{address}, did you mean to set ReverseListenerBindAddress instead?' .

@itsmeroy2012
Copy link
Contributor

I have a solution to this but I don't know how to pull @bermannoah 's fork as a remote, and send him a pull request on his topic branch.

@bermannoah
Copy link
Contributor Author

@itsmeroy2012 you can pull down my repo and push to the lhost-setting-warning branch or just do the work on your own repo and make a separate PR. I had a couple IRL things come up this weekend and haven't finished fixing my own patch. :)

@itsmeroy2012
Copy link
Contributor

I have done the work on my repo. @busterb told me to make suggestions on here rather than hijacking with an alternate PR and I'm new to github so I'm unable to pull down your repo and push to the 'lhost-setting-warning' branch. Could you guide me through it?

@bermannoah
Copy link
Contributor Author

@itsmeroy2012 yup! Here's a high level overview, let me know if it doesn't match your system setup. Basically:

1 - git clone git@github.com:bermannoah/metasploit-framework.git if you're using SSH with github (here's a guide) otherwise, git clone https://github.com/bermannoah/metasploit-framework.git. You might need to do it in a different directory than your version of the repo.

2 - cd into the repo directory

3 - just in case, run git fetch to make sure you have all branches.

4 - git checkout lhost-setting-warning && git pull origin lhost-setting-warning (just in case)

5 - you'll need to go into the core.rb file and remove or alter my old solution to this issue

6 - make the changes you have in mind. make sure they work etc etc

7 - do all the usual git add . and git commit -S -m "commit text here" stuff. then run git push origin lhost-setting-warning and it should push to this PR.

Let me know if that works for you or if you run into any troubles!

@itsmeroy2012
Copy link
Contributor

When I run 'git checkout lhost-setting-warning && git pull origin lhost-setting-warning ' there is a fatal error. Will this be a problem later?

screenshot from 2017-03-07 22-43-19

@itsmeroy2012
Copy link
Contributor

And then there is this. I think I don't have the rights to write into the repository.
screenshot from 2017-03-07 22-50-10

@bermannoah
Copy link
Contributor Author

@itsmeroy2012 oh shoot, I'm sorry. It looks like your branch is up to date so you won't need to run git pull. For the future though it looks like you're leaving out origin -- it should be git pull origin branchname. When it comes to the second command: it looks like you don't have a GPG key setup for your commits. The contributor guide for metasploit recommends it -- here's a guide to generating a key and here's a guide to connecting it to github.

Let me know if you have any more questions! Good luck! :)

@itsmeroy2012
Copy link
Contributor

Everything ran successfully until this error popped out while executing the command "git push origin lhost-setting-warning"

remote: Permission to bermannoah/metasploit-framework.git denied to itsmeroy2012.
fatal: unable to access 'https://github.com/bermannoah/metasploit-framework.git/': The requested URL returned error: 403

@bermannoah
Copy link
Contributor Author

bermannoah commented Mar 7, 2017

@itsmeroy2012 i'll add you as a contributor, one sec

@itsmeroy2012
Copy link
Contributor

root@kali:~/bermannoah/metasploit-framework# git remote -v
origin https://github.com/bermannoah/metasploit-framework.git (fetch)
origin https://github.com/bermannoah/metasploit-framework.git (push)

@bermannoah
Copy link
Contributor Author

@itsmeroy2012 awesome -- added you as a contributor to my repo. try again?

@itsmeroy2012
Copy link
Contributor

Do I need to repeat the steps again? Because the same error pops out , the one I mentioned above.

@bermannoah
Copy link
Contributor Author

@itsmeroy2012 did you accept the contributor invite?

@itsmeroy2012
Copy link
Contributor

Thanks a lot for your precious time. I think it is done.

@@ -55,6 +55,17 @@ def bind_port
(port > 0) ? port : datastore['LPORT'].to_i
end

# Checking if LHOST is a loopback address
def is_loopback_address(address)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method needs to be named is_loopback_address?. Fixing myself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And for the record, this code is @busterb's: #8063 (comment).

@@ -82,12 +93,16 @@ def setup_handler
'MsfPayload' => self,
'MsfExploit' => assoc_exploit
})

if is_loopback_address(ip)
print_warning ("You are attempting to listen on a loopback address by setting LHOST to #{ip}, did you mean to set ReverseListenerBindAddress instead?")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extraneous whitespace and comma splice. Fixing.

@wvu
Copy link
Contributor

wvu commented Mar 15, 2017

msf exploit(handler) > set lhost 192.168.33.1
lhost => 192.168.33.1
msf exploit(handler) > set reverselistenerbindaddress 127.0.0.1
reverselistenerbindaddress => 127.0.0.1
msf exploit(handler) > run

[!] You are attempting to listen on a loopback address by setting LHOST to 127.0.0.1, did you mean to set ReverseListenerBindAddress instead?
[*] Started reverse TCP handler on 127.0.0.1:4444 
[*] Starting the payload handler...

This does not work.

@busterb
Copy link
Member

busterb commented Mar 15, 2017

Yeah, that's exactly the opposite of what this was intended to alert on.

@itsmeroy2012
Copy link
Contributor

itsmeroy2012 commented Mar 15, 2017

When we give the command: -

'set reverselistenerbindaddress 127.0.0.1' , the 'ip' variable in the file lib/msf/core/handler/reverse.rb changes in the following code:-

if is_loopback_address(ip)
print_warning ("You are attempting to listen on a loopback address by setting LHOST to #{ip}, did you mean to set ReverseListenerBindAddress instead?")
end

Why is this happening and does the same variable stores both the lhost and the reverselistenerbindaddress?
@wvu-r7 @busterb

@wvu
Copy link
Contributor

wvu commented Mar 15, 2017

@itsmeroy2012: It's looping through each bind address. Look at the code just above that.

@itsmeroy2012
Copy link
Contributor

You're talking about this part right? @wvu-r7

bind_addresses.each do |ip|
begin ...
....

@wvu
Copy link
Contributor

wvu commented Mar 16, 2017

@itsmeroy2012: Correct.

@itsmeroy2012
Copy link
Contributor

What's the complexity of this issue now? Is it still under 'newbie-friendly' ? @wvu-r7

@itsmeroy2012
Copy link
Contributor

This seems to solve the problem specified above. But there's one flaw, the user has to specify a value for LPORT else the callback_host stores the value '172.16.176.36' for some reason. Should I push the change? @wvu-r7

bind_addresses.each do |ip|
begin
self.listener_sock = Rex::Socket::TcpServer.create(
'LocalHost' => ip,
'LocalPort' => local_port,
'Comm' => comm,
'Context' =>
{
'Msf' => framework,
'MsfPayload' => self,
'MsfExploit' => assoc_exploit
})

        callback_host = datastore['LHOST']
        if is_loopback_address(callback_host)
          print_warning ("You are attempting to listen on a loopback address by setting LHOST to #{ip}, did you mean to set ReverseListenerBindAddress instead?")
        end

wvu added a commit to wvu/metasploit-framework that referenced this pull request Mar 16, 2017
wvu added a commit to wvu/metasploit-framework that referenced this pull request Mar 16, 2017
wvu added a commit to wvu/metasploit-framework that referenced this pull request Mar 16, 2017
@wvu wvu merged commit ad929b6 into rapid7:master Mar 16, 2017
@wvu
Copy link
Contributor

wvu commented Mar 16, 2017

Please see af3cd18 for edification.

@itsmeroy2012
Copy link
Contributor

What was the fix?? @wvu-r7

@wvu
Copy link
Contributor

wvu commented Mar 16, 2017

Read the commit!!

@bermannoah bermannoah deleted the lhost-setting-warning branch March 17, 2017 00:53
@pbarry-r7
Copy link
Contributor

pbarry-r7 commented Mar 21, 2017

Release Notes

A warning now displays when the use of loopback addresses may be unintentional and lead to undesired/unexpected behavior.

@tdoan-r7 tdoan-r7 added the rn-enhancement release notes enhancement label Mar 22, 2017
@Alex942
Copy link

Alex942 commented Nov 1, 2017

hey, i am stuck with this problem.. someone help me in solving this problem.......plzzzz

@wvu
Copy link
Contributor

wvu commented Nov 1, 2017

@Alex942: What problem? Don't comment on old tickets for support. Go to IRC.

@Alex942
Copy link

Alex942 commented Nov 2, 2017

@wvu-r7 my metasploit showing error when i am setting my lhost to 127.0.0.1......i am unable to fix this

@wvu
Copy link
Contributor

wvu commented Nov 2, 2017

Not an error. It works fine, just not suggested.

@Alex942
Copy link

Alex942 commented Nov 3, 2017

@wvu-r7 can you please explain me step by step how to fix it..........

@rwhitcroft
Copy link
Contributor

rwhitcroft commented Nov 3, 2017

@Alex942 You're not listening. This is not an error, so there's nothing to fix. You are telling Metasploit you want to listen on the local address (127.0.0.1) which means no clients will be able to connect to you. Metasploit thinks that's weird, so it shows you this warning.

Please stop posting in this thread. Also, some suggested reading for you: https://en.wikipedia.org/wiki/Localhost

@Alex942
Copy link

Alex942 commented Nov 4, 2017

@rwhitcroft i just want my metasploit to start a meterpreter session when i choose 127.0.0.1 as local host. That all.............i know you all are pro of this area and you know many times more than me thats why i am asking...............

@wvu
Copy link
Contributor

wvu commented Nov 4, 2017

@Alex942: You have provided close to zero details about your problem. How can we help? This isn't even the right place still. Please ask on IRC. I don't want to lock this thread.

If you want to catch a shell on localhost, your payload needs to connect back to that address. Basically, LHOST should match between handler and payload. This is all the help I can provide without further information. Thanks.

@wvu
Copy link
Contributor

wvu commented Nov 4, 2017

@rapid7 rapid7 locked and limited conversation to collaborators Nov 4, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants