You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OOXMLPasswordsTry.java 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.examples.crypt;
  20. import java.io.File;
  21. import java.nio.file.Files;
  22. import java.nio.file.Paths;
  23. import java.security.GeneralSecurityException;
  24. import java.util.Optional;
  25. import java.util.function.Predicate;
  26. import java.util.stream.Stream;
  27. import org.apache.poi.poifs.crypt.Decryptor;
  28. import org.apache.poi.poifs.crypt.EncryptionInfo;
  29. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  30. /**
  31. * Tries a list of possible passwords for an OOXML protected file
  32. *
  33. * Note that this isn't very fast, and is aimed at when you have
  34. * just a few passwords to check.
  35. * For serious processing, you'd be best off grabbing the hash
  36. * out with POI or office2john.py, then running that against
  37. * "John The Ripper" or GPU enabled version of "hashcat"
  38. */
  39. public final class OOXMLPasswordsTry {
  40. private OOXMLPasswordsTry() {}
  41. @SuppressWarnings({"java:S106","java:S4823"})
  42. public static void main(String[] args) throws Exception {
  43. if (args.length < 2) {
  44. System.err.println("Use:");
  45. System.err.println(" OOXMLPasswordsTry <file.ooxml> <wordlist>");
  46. System.exit(1);
  47. }
  48. String ooxml = args[0];
  49. String words = args[1];
  50. System.out.println("Trying passwords from " + words + " against " + ooxml);
  51. System.out.println();
  52. try (POIFSFileSystem fs = new POIFSFileSystem(new File(ooxml), true)) {
  53. EncryptionInfo info = new EncryptionInfo(fs);
  54. Decryptor d = Decryptor.getInstance(info);
  55. final long start = System.currentTimeMillis();
  56. final int[] count = { 0 };
  57. Predicate<String> counter = s -> {
  58. if (++count[0] % 1000 == 0) {
  59. int secs = (int) ((System.currentTimeMillis() - start) / 1000);
  60. System.out.println("Done " + count[0] + " passwords, " + secs + " seconds, last password " + s);
  61. }
  62. return true;
  63. };
  64. // Try each password in turn, reporting progress
  65. try (Stream<String> lines = Files.lines(Paths.get(words))) {
  66. Optional<String> found = lines.filter(counter).filter(w -> isValid(d, w)).findFirst();
  67. System.out.println(found.map(s -> "Password found: " + s).orElse("Error - No password matched"));
  68. }
  69. }
  70. }
  71. private static boolean isValid(Decryptor dec, String password) {
  72. try {
  73. return dec.verifyPassword(password);
  74. } catch (GeneralSecurityException e) {
  75. return false;
  76. }
  77. }
  78. }