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

Log IP on SSH authentication failure for Built-in SSH server #13150

Merged
merged 9 commits into from
Dec 8, 2020

Conversation

elesiuta
Copy link
Contributor

targets #13094

Line 127 logs the IP address when connecting with a key from a user that does not have permission for the repo they're trying to clone or the repo does not exist.
Line 188 logs the IP address when connecting with a key not recognized by the server.
I'm not sure which cases Line 131 will be hit (if any and if Line 127 already covers this), but including it seemed prudent.

The regex from this guide could also be updated to omit the 'for' so it matches this along with failed logins, should I include that in this pull request or make a new one? It could also be updated to match "invalid credentials from" in the log when cloning over HTTP.

@elesiuta elesiuta force-pushed the ssh_log_ip branch 2 times, most recently from 6f437f8 to b436c7d Compare October 15, 2020 02:09
@elesiuta elesiuta marked this pull request as draft October 15, 2020 02:13
@elesiuta elesiuta marked this pull request as ready for review October 15, 2020 02:13
@GiteaBot GiteaBot added the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label Oct 15, 2020
@codecov-io
Copy link

codecov-io commented Oct 15, 2020

Codecov Report

Merging #13150 (cb4c9dd) into master (a33db35) will decrease coverage by 0.04%.
The diff coverage is 61.53%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #13150      +/-   ##
==========================================
- Coverage   42.27%   42.23%   -0.05%     
==========================================
  Files         708      708              
  Lines       77191    77196       +5     
==========================================
- Hits        32634    32604      -30     
- Misses      39198    39230      +32     
- Partials     5359     5362       +3     
Impacted Files Coverage Δ
routers/repo/http.go 42.34% <0.00%> (-0.11%) ⬇️
services/pull/merge.go 49.18% <0.00%> (ø)
services/repository/push.go 44.64% <44.00%> (-13.39%) ⬇️
routers/private/serv.go 28.23% <50.00%> (+0.14%) ⬆️
modules/templates/helper.go 49.44% <70.00%> (-0.10%) ⬇️
modules/notification/action/action.go 60.49% <70.58%> (+2.23%) ⬆️
modules/ssh/ssh.go 46.04% <100.00%> (ø)
models/repo_mirror.go 2.38% <0.00%> (-11.91%) ⬇️
modules/indexer/stats/queue.go 52.94% <0.00%> (-11.77%) ⬇️
modules/repository/push.go 47.61% <0.00%> (-4.77%) ⬇️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4d66ee1...abf10c0. Read the comment docs.

@techknowlogick techknowlogick added the type/enhancement An improvement of existing functionality label Oct 15, 2020
@techknowlogick techknowlogick added this to the 1.14.0 milestone Oct 15, 2020
@lafriks
Copy link
Member

lafriks commented Oct 15, 2020

Yes, please update docs in this PR

@lafriks lafriks added the topic/security Something leaks user information or is otherwise vulnerable. Should be fixed! label Oct 15, 2020
also match failed authentication over command line
@@ -124,11 +124,11 @@ func sessionHandler(session ssh.Session) {
// Wait for the command to exit and log any errors we get
err = cmd.Wait()
if err != nil {
log.Error("SSH: Wait: %v", err)
log.Error("SSH: Wait: %v Failed authentication attempt from %s", err, session.RemoteAddr())
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure that every error from cmd wait will be due to an authentication failure?

Copy link
Contributor Author

@elesiuta elesiuta Oct 15, 2020

Choose a reason for hiding this comment

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

Nope, maybe a check if err is exit status 1 but then err could also be other things and still be an authentication failure and I don't want to miss those. I'm not too familiar with the code base yet and only started using gitea this week (if my log file was longer, finding other cases might be easier).

Another option is prepend it with 'Possible' or omit 'Failed authentication attempt' entirely, leaving just the IP and leave it to the user to decide whether to include this in fail2ban.

}

if err := session.Exit(getExitStatusFromError(err)); err != nil {
log.Error("Session failed to exit. %s", err)
log.Error("Session failed to exit. %s Failed authentication attempt from %s", err, session.RemoteAddr())
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm even less sure about this one, wasn't able to find an input that triggers this in my testing.

@zeripath
Copy link
Contributor

Ok so I think this is all in the wrong place.

By the time the session handler is involved they have authenticated with the server as using a public key.

You presumably want to catch the people who are failing to authenticate.

In terms of errors from the session handler - those are more authorization errors and you'd be better off logging them in routers/private/serv.go

@elesiuta
Copy link
Contributor Author

Ok so I think this is all in the wrong place.

By the time the session handler is involved they have authenticated with the server as using a public key.

You presumably want to catch the people who are failing to authenticate.

In terms of errors from the session handler - those are more authorization errors and you'd be better off logging them in routers/private/serv.go

Thank you! You're right, that place is much better. I've updated it to reflect this, and also added one more location to log the remote IP address, when trying to clone over HTTP for a user that does not exist.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Dec 8, 2020
Copy link
Member

@6543 6543 left a comment

Choose a reason for hiding this comment

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

.

@6543
Copy link
Member

6543 commented Dec 8, 2020

🚀

@6543 6543 merged commit abb9cff into go-gitea:master Dec 8, 2020
@go-gitea go-gitea locked and limited conversation to collaborators Jan 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. topic/security Something leaks user information or is otherwise vulnerable. Should be fixed! type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants