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

[BUG] cannot connect to DB for sql injection lesson #502

Closed
bchurchill opened this issue Sep 26, 2019 · 10 comments
Closed

[BUG] cannot connect to DB for sql injection lesson #502

bchurchill opened this issue Sep 26, 2019 · 10 comments
Labels

Comments

@bchurchill
Copy link
Contributor

Describe the bug
After following the docker setup instructions with the latest release, some of the lessons won't work. In particular, the "Sql Injection" lesson (the first, unnumbered one) won't accept any correct answer. Instead, there's an error in the console about tomcat being unable to connect to the database with the user firstBloodyMessL.

To Reproduce
Steps to reproduce the behavior:

  1. Create an Ubuntu 18.04 VM
  2. Run exactly the commands on the project homepage to setup Shepherd with docker (I used the 3.1 release)
  3. Login as administrator
  4. Go to the "Sql Injection Module"
  5. Type in the correct attack string

Expected behavior
It gives the key for the lesson.

Actual behavior
It says the answer is wrong -- even when it's right. A log message appears in the console showing that tomcat could not connect to the database with the appropriate account:

secshep_tomcat | FATAL me at XXX.XXX.XXX.XXX from ?.?.?.? @ 26 Sep 03:18:07,076 [https-openssl-nio-8443-exec-6] (Database.java:193) - Unable to create database connection: java.sql.SQLException: Access denied for user 'firstBloodyMessL'@'secshep_tomcat.securityshepherd31_default' (using password: YES)
secshep_tomcat | java.sql.SQLException: Access denied for user 'firstBloodyMessL'@'secshep_tomcat.securityshepherd31_default' (using password: YES)
secshep_tomcat | 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
secshep_tomcat | 	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
secshep_tomcat | 	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
secshep_tomcat | 	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
secshep_tomcat | 	at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
secshep_tomcat | 	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
secshep_tomcat | 	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2483)
secshep_tomcat | 	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
secshep_tomcat | 	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
secshep_tomcat | 	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
secshep_tomcat | 	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
secshep_tomcat | 	at sun.reflect.GeneratedConstructorAccessor16.newInstance(Unknown Source)
secshep_tomcat | 	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
secshep_tomcat | 	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
secshep_tomcat | 	at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
secshep_tomcat | 	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
secshep_tomcat | 	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
secshep_tomcat | 	at java.sql.DriverManager.getConnection(DriverManager.java:664)
secshep_tomcat | 	at java.sql.DriverManager.getConnection(DriverManager.java:247)
secshep_tomcat | 	at dbProcs.Database.getSqlInjLessonConnection(Database.java:189)
secshep_tomcat | 	at servlets.module.lesson.SqlInjectionLesson.getSqlInjectionResult(SqlInjectionLesson.java:133)
secshep_tomcat | 	at servlets.module.lesson.SqlInjectionLesson.doPost(SqlInjectionLesson.java:81)
secshep_tomcat | 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
secshep_tomcat | 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
secshep_tomcat | 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
secshep_tomcat | 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
secshep_tomcat | 	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
secshep_tomcat | 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
secshep_tomcat | 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
secshep_tomcat | 	at servlets.SetupFilter.doFilter(SetupFilter.java:39)
secshep_tomcat | 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
secshep_tomcat | 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
secshep_tomcat | 	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
secshep_tomcat | 	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
secshep_tomcat | 	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:610)
secshep_tomcat | 	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
secshep_tomcat | 	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
secshep_tomcat | 	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660)
secshep_tomcat | 	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
secshep_tomcat | 	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
secshep_tomcat | 	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
secshep_tomcat | 	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
secshep_tomcat | 	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:808)
secshep_tomcat | 	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
secshep_tomcat | 	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
secshep_tomcat | 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
secshep_tomcat | 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
secshep_tomcat | 	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
secshep_tomcat | 	at java.lang.Thread.run(Thread.java:748)

Desktop (please complete the following information):

  • OS: Linux
  • Browser: Chrome

Further Information:

  • mvn version 3.6.0
  • docker version 18.09.7
  • docker-compose version 1.17.1
  • java version openjdk 11.0.4
@bchurchill bchurchill added the Bug label Sep 26, 2019
@bchurchill
Copy link
Contributor Author

I also tried with the latest version of security shepherd (from the git repository) rather than release 3.1. Had the same problem.

@ismisepaul
Copy link
Member

ismisepaul commented Sep 26, 2019

Looking at your log file;

secshep_tomcat | java.sql.SQLException: Access denied for user 'firstBloodyMessL'@'secshep_tomcat.securityshepherd31_default' (using password: YES)

there seems to be a problem with your hostname: secshep_tomcat.securityshepherd31_default

The SQL challenges work by using grants in the following way levelUser@containerName.DockerNetworkName

GRANT USAGE ON *.* TO 'firstBloodyMessL'@'localhost' IDENTIFIED BY 'password';

The above SQL script has localhost. We then sed it to inject our variables (contained in the .env)

RUN sed -i 's/@'\''localhost'\''/@'\'''"$CONTAINER_TOMCAT"'.'"$DOCKER_NETWORK_NAME"''\''/g' /docker-entrypoint-initdb.d/moduleSchemas.sql

The problem looks like somehow the hostname has been changed on your environment, it should be;

DOCKER_NETWORK_NAME=securityshepherd_default

Not securityshepherd31_default as your logs show.

Can you confirm you haven't done anything to modify you docker network to be called securityshepherd31_default

The network name is set up in the docker-compose.yml

DOCKER_NETWORK_NAME: ${DOCKER_NETWORK_NAME}

@bchurchill
Copy link
Contributor Author

Thanks for your help! Definitely gives me something to look into, at the least.

I haven't touched the default docker networking configuration at all, and my DOCKER_NETWORK_NAME was set to securityshepherd_default. I have no idea where this securityshephderd31_default came from. I did try setting DOCKER_NETWORK_NAME to securityshepherd31_default to see if that could, perchance, address the issue, and then I got these log messages:

secshep_tomcat | DEBUG admin at XXX.XXX.XXX.XXX from ?.?.?.? @ 26 Sep 18:42:23,721 [https-openssl-nio-8443-exec-3] (SqlInjectionLesson.java:76) - SQL Injection Lesson servlet accessed by: admin
secshep_tomcat | DEBUG admin at XXX.XXX.XXX.XXX from ?.?.?.? @ 26 Sep 18:42:23,722 [https-openssl-nio-8443-exec-3] (SqlInjectionLesson.java:78) - User Submitted - x' OR '1'='1
secshep_tomcat | DEBUG admin at XXX.XXX.XXX.XXX from ?.?.?.? @ 26 Sep 18:42:23,723 [https-openssl-nio-8443-exec-3] (SqlInjectionLesson.java:80) - Servlet root = /usr/local/tomcat/webapps/ROOT/
secshep_mysql | 2019-09-26T18:42:23.726293Z 200 [Note] Access denied for user 'firstBloodyMessL'@'172.18.0.4' (using password: YES)
secshep_tomcat | FATAL admin at XXX.XXX.XXX.XXX from ?.?.?.? @ 26 Sep 18:42:23,727 [https-openssl-nio-8443-exec-3] (Database.java:198) - Unable to create database connection: java.sql.SQLException: Access denied for user 'firstBloodyMessL'@'172.18.0.4' (using password: YES)
secshep_tomcat | java.sql.SQLException: Access denied for user 'firstBloodyMessL'@'172.18.0.4' (using password: YES)
secshep_tomcat | 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
secshep_tomcat | 	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)

If it makes any difference, I'm connecting to this machine over the Internet. I don't recognize the IP address 172.18.0.4, I'm assuming that must be on the network created by docker-compose? Any other things I might try?

I've verified the content of docker-compose.yml and it looks right -- same as the file in the repo.

@bchurchill
Copy link
Contributor Author

Maybe the 31 came from using the 3.1 release? I don't know how it would have persisted... I did a full docker-compose down and rebuilt everything. But I can try getting a fresh VM, using the latest github code, and see if I can get it to work (I'm not proficient in working with containers yet!).

@bchurchill
Copy link
Contributor Author

So I've started over with the github code, and the issue with "securityshepherd31" is all gone. But now I'm still encountering the message below. It seems like the network connectivity is working, but the password is wrong? I don't see how that's possible with all the default config.

ecshep_tomcat | DEBUG admin at XXX.XXX.XXX.XXX from ?.?.?.? @ 26 Sep 19:41:41,212 [https-openssl-nio-8443-exec-4] (SqlInjectionLesson.java:76) - SQL Injection Lesson servlet accessed by: admin
secshep_tomcat | DEBUG admin at XXX.XXX.XXX.XXX from ?.?.?.? @ 26 Sep 19:41:41,213 [https-openssl-nio-8443-exec-4] (SqlInjectionLesson.java:78) - User Submitted - x' OR '1'='1
secshep_tomcat | DEBUG admin at XXX.XXX.XXX.XXX from ?.?.?.? @ 26 Sep 19:41:41,213 [https-openssl-nio-8443-exec-4] (SqlInjectionLesson.java:80) - Servlet root = /usr/local/tomcat/webapps/ROOT/
secshep_mysql | 2019-09-26T19:41:41.217305Z 87 [Note] Access denied for user 'firstBloodyMessL'@'172.18.0.4' (using password: YES)
secshep_tomcat | FATAL admin at XXX.XXX.XXX.XXX from ?.?.?.? @ 26 Sep 19:41:41,218 [https-openssl-nio-8443-exec-4] (Database.java:198) - Unable to create database connection: java.sql.SQLException: Access denied for user 'firstBloodyMessL'@'172.18.0.4' (using password: YES)
secshep_tomcat | java.sql.SQLException: Access denied for user 'firstBloodyMessL'@'172.18.0.4' (using password: YES)
secshep_tomcat | 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
secshep_tomcat | 	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)

@bchurchill
Copy link
Contributor Author

After experimenting with networking settings for a while, I managed to solve the problem by changing the sql script so that it uses a wildcard hostname. Since docker-compose uses its own network that shields mysql from external hosts, I don't think this poses a risk to the security of the system. Would you be open to a pull request that updates the sed command in the docker file to do this?

(I looked for other solutions but couldn't come up with anything; I tried using different versions of Ubuntu to see if the version of docker made a difference, but that didn't help.)

@ismisepaul
Copy link
Member

Maybe the 31 came from using the 3.1 release?

Yeah that might be the case but its not clear how that is happening?

secshep_mysql | 2019-09-26T19:41:41.217305Z 87 [Note] Access denied for user 'firstBloodyMessL'@'172.18.0.4' (using password: YES)
You're still seeing the issues because the hostname / IP address is not allowed because of the explicit grant in the core db schema

You are correct with your solution by changing the script. You can modify the sed command in the docker image to allow everything with a wildcard or remove them completely from the core schema. But it's there because of a security concern and when I built the docker images I had to work with those grants. Shepherd is still being release as a VM and we have to factor in.

Before we accept a change in how the grants are I'd like to run it by @markdenihan first.

@bchurchill
Copy link
Contributor Author

Maybe the 31 came from using the 3.1 release?

Yeah that might be the case but its not clear how that is happening?

I can answer that now: the docs say "Your app’s network is given a name based on the “project name”, which is based on the name of the directory it lives in. You can override the project name with either the --project-name flag or the COMPOSE_PROJECT_NAME environment variable." Because I unzipped the tarball from the website, the default folder name SecurityShepherd3.1, and that was mangled by docker into securityshepherd31_default. So another bug we can fix is to use the COMPOSE_PROJECT_NAME variable for DOCKER_NETWORK_NAME. But this wasn't enough to solve my issue.

Shepherd is still being release as a VM and we have to factor in.

Instead of changing the SQL script, we can change the dockerfile that mangles the SQL script. That way it only affects docker builds, where none of the database services are exposed anyway. We could look for other solutions, but I think this will be the most reliable. Looking forward to @markdenihan 's comments.

@ismisepaul
Copy link
Member

ismisepaul commented Oct 1, 2019

That's some really nice work! Thanks.

I agree with your solution if you want you could create a branch called dev#502 make your changes to the docker file and create a merge request.

We can review it and look to merge it into dev 😄

@anquanbiji
Copy link

anquanbiji commented Aug 30, 2021

Hi
I have the same problem when I install it with source code.

I found that changing the current path to 'securityshepherd' can resolve the problem.

Hope it useful for others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants