Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java/SQL字符串操作使;“福”;。等于(“巴?”)_Java_Sql_Regex_String - Fatal编程技术网

Java/SQL字符串操作使;“福”;。等于(“巴?”)

Java/SQL字符串操作使;“福”;。等于(“巴?”),java,sql,regex,string,Java,Sql,Regex,String,我从我的讲师那里得到了这个谜题 public class Exercise{ public static void main(String[] args) { int foo = 10; assert("Foo".equals("Bar???")); } } 我应该在???加些什么???那将是我唯一可以写作的地方。 我已经思考和谷歌搜索了两天多,但仍然没有任何线索 这显然是Java,但谜题的名字是SQLInjection,这就是为什么我认为它可能

我从我的讲师那里得到了这个谜题

public class Exercise{
    public static void main(String[] args) {
        int foo = 10;
        assert("Foo".equals("Bar???"));
    }
}
我应该在???加些什么???那将是我唯一可以写作的地方。 我已经思考和谷歌搜索了两天多,但仍然没有任何线索

这显然是Java,但谜题的名字是SQLInjection,这就是为什么我认为它可能与SQL有关

我强烈认为我应该使用正则表达式,因为这是我能想到的唯一可以写在字符串中的东西。我也不确定int-foo在代码中做了什么


任何想法或提示都将不胜感激。

替换为
”。子字符串(0,0)+“Foo
,您将得到:

    assert("Foo".equals("Bar".substring(0, 0)+"Foo"));

这不是SQL注入,但与SQL注入类似(仅使用Java而不是SQL,并且您是手动填充代码而不是让程序来填充代码)。

替换为
”。子字符串(0,0)+“Foo
,您将得到:

    assert("Foo".equals("Bar".substring(0, 0)+"Foo"));

这不是SQL注入,但与SQL注入类似(仅使用Java而不是SQL,并且您是手动填充代码而不是让程序来填充代码)。

您可以利用以下事实,即字符串文本是内部的,因此在比较它们的引用时,
=
将为真:

替换为
”)| |(“”==“
,这将产生

assert("Foo".equals("Bar") || ("" == ""));

您可以使用以下事实,即字符串文本是内部的,因此在比较它们的引用时,
=
将给出true:

替换为
”)| |(“”==“
,这将产生

assert("Foo".equals("Bar") || ("" == ""));

您可以提高难度。假设我们的任务是替换此代码中的

assert("Foo".equals("Bar???") && userAccessLevel==Access.Admin);
让它一直有效

您可以通过将其更改为

assert("Foo".equals("Bar") || true);//") && userAccessLevel==Access.Admin);
                        ^^^^^^^^^^^^^^
如你所见

  • 我们可以通过简单地注释该行中的其余代码来删除某些条件
  • 这样,我们的
    甚至不必在替换零件中平衡
    ”| |真)//
  • 我们不需要知道正确的(预期的)值
    “Foo”
    ,因为我们只需将
    | | true
    置于

    • 你可以提高难度。假设我们的任务是替换此代码中的

      assert("Foo".equals("Bar???") && userAccessLevel==Access.Admin);
      
      让它一直有效

      您可以通过将其更改为

      assert("Foo".equals("Bar") || true);//") && userAccessLevel==Access.Admin);
                              ^^^^^^^^^^^^^^
      
      如你所见

      • 我们可以通过简单地注释该行中的其余代码来删除某些条件
      • 这样,我们的
        甚至不必在替换零件中平衡
        ”| |真)//
      • 我们不需要知道正确的(预期的)值
        “Foo”
        ,因为我们只需将
        | | true
        置于

        • 一些额外的解决方案

          assert("Foo".equals("BarFoo".replace("Bar", "") + ""));
          assert("Foo".equals("Bar".equals("Foo") ? "Bar" : "Foo"));
          assert("Foo".equals("Bar.Foo".split("\\.")[1] + ""));
          
          编辑此项不符合要求我将其留作记录

          如果允许您使用反射,您可以这样做

          import java.lang.reflect.Field;
          public class Exercise {
              public static void main(String[] args) throws Exception {
                  Field value = String.class.getDeclaredField("value");
                  // make a private field accessible
                  value.setAccessible(true);
                  // set the value of the interned String "Foo" to "Bar???"
                  value.set("Foo", "Bar???".toCharArray());
          
                  // as this String literal referes to the same object
                  // as in the source code before "Foo" was replaced by "Bar???" ;-)
                  boolean equals = "Foo".equals("Bar???");
                  System.out.println("equals: " + equals);
          
                  // would fail with an java.lang.AssertionError
                  // if the assertion is not true
                  assert("Foo".equals("Bar???"));
              }
          }
          
          编译

          javac Excercise.java
          
          跑步

          java -ea Excercise
          

          一些额外的解决方案

          assert("Foo".equals("BarFoo".replace("Bar", "") + ""));
          assert("Foo".equals("Bar".equals("Foo") ? "Bar" : "Foo"));
          assert("Foo".equals("Bar.Foo".split("\\.")[1] + ""));
          
          编辑此项不符合要求我将其留作记录

          如果允许您使用反射,您可以这样做

          import java.lang.reflect.Field;
          public class Exercise {
              public static void main(String[] args) throws Exception {
                  Field value = String.class.getDeclaredField("value");
                  // make a private field accessible
                  value.setAccessible(true);
                  // set the value of the interned String "Foo" to "Bar???"
                  value.set("Foo", "Bar???".toCharArray());
          
                  // as this String literal referes to the same object
                  // as in the source code before "Foo" was replaced by "Bar???" ;-)
                  boolean equals = "Foo".equals("Bar???");
                  System.out.println("equals: " + equals);
          
                  // would fail with an java.lang.AssertionError
                  // if the assertion is not true
                  assert("Foo".equals("Bar???"));
              }
          }
          
          编译

          javac Excercise.java
          
          跑步

          java -ea Excercise
          

          .substring(0,0)+“Foo
          ”.substring(0,0)+“Foo
          我明白了。非常感谢你。我以为我只能写在字符串里面,这就是我错的地方!我懂了。非常感谢你。我以为我只能写在字符串里面,这就是我错的地方!我认为这更符合问题的精神,+1我认为这更符合问题的精神,+1“我应该在???添加一些内容,这将是我唯一可以写“你是对的”的地方。我的回答不符合要求。我应该在???添加一些东西,这将是唯一允许我写“你是对的”的地方。我的回答不符合要求。我没有注意到这一点。