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.

VBAMacroReader.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.macros;
  16. import static org.apache.poi.util.StringUtil.startsWithIgnoreCase;
  17. import static org.apache.poi.util.StringUtil.endsWithIgnoreCase;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.Closeable;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.PushbackInputStream;
  26. import java.nio.charset.Charset;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. import java.util.zip.ZipEntry;
  30. import java.util.zip.ZipInputStream;
  31. import org.apache.poi.poifs.filesystem.DirectoryNode;
  32. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  33. import org.apache.poi.poifs.filesystem.DocumentNode;
  34. import org.apache.poi.poifs.filesystem.Entry;
  35. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  36. import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
  37. import org.apache.poi.util.CodePageUtil;
  38. import org.apache.poi.util.HexDump;
  39. import org.apache.poi.util.IOUtils;
  40. import org.apache.poi.util.RLEDecompressingInputStream;
  41. /**
  42. * <p>Finds all VBA Macros in an office file (OLE2/POIFS and OOXML/OPC),
  43. * and returns them.
  44. * </p>
  45. * <p>
  46. * <b>NOTE:</b> This does not read macros from .ppt files.
  47. * See org.apache.poi.hslf.usermodel.TestBugs.getMacrosFromHSLF() in the scratchpad
  48. * module for an example of how to do this. Patches that make macro
  49. * extraction from .ppt more elegant are welcomed!
  50. * </p>
  51. *
  52. * @since 3.15-beta2
  53. */
  54. public class VBAMacroReader implements Closeable {
  55. protected static final String VBA_PROJECT_OOXML = "vbaProject.bin";
  56. protected static final String VBA_PROJECT_POIFS = "VBA";
  57. // FIXME: When minimum supported version is Java 7, replace with java.nio.charset.StandardCharsets.UTF_16LE
  58. private static final Charset UTF_16LE = Charset.forName("UTF-16LE");
  59. private NPOIFSFileSystem fs;
  60. public VBAMacroReader(InputStream rstream) throws IOException {
  61. PushbackInputStream stream = new PushbackInputStream(rstream, 8);
  62. byte[] header8 = IOUtils.peekFirst8Bytes(stream);
  63. if (NPOIFSFileSystem.hasPOIFSHeader(header8)) {
  64. fs = new NPOIFSFileSystem(stream);
  65. } else {
  66. openOOXML(stream);
  67. }
  68. }
  69. public VBAMacroReader(File file) throws IOException {
  70. try {
  71. this.fs = new NPOIFSFileSystem(file);
  72. } catch (OfficeXmlFileException e) {
  73. openOOXML(new FileInputStream(file));
  74. }
  75. }
  76. public VBAMacroReader(NPOIFSFileSystem fs) {
  77. this.fs = fs;
  78. }
  79. private void openOOXML(InputStream zipFile) throws IOException {
  80. ZipInputStream zis = new ZipInputStream(zipFile);
  81. ZipEntry zipEntry;
  82. while ((zipEntry = zis.getNextEntry()) != null) {
  83. if (endsWithIgnoreCase(zipEntry.getName(), VBA_PROJECT_OOXML)) {
  84. try {
  85. // Make a NPOIFS from the contents, and close the stream
  86. this.fs = new NPOIFSFileSystem(zis);
  87. return;
  88. } catch (IOException e) {
  89. // Tidy up
  90. zis.close();
  91. // Pass on
  92. throw e;
  93. }
  94. }
  95. }
  96. zis.close();
  97. throw new IllegalArgumentException("No VBA project found");
  98. }
  99. public void close() throws IOException {
  100. fs.close();
  101. fs = null;
  102. }
  103. /**
  104. * Reads all macros from all modules of the opened office file.
  105. * @return All the macros and their contents
  106. *
  107. * @since 3.15-beta2
  108. */
  109. public Map<String, String> readMacros() throws IOException {
  110. final ModuleMap modules = new ModuleMap();
  111. findMacros(fs.getRoot(), modules);
  112. Map<String, String> moduleSources = new HashMap<String, String>();
  113. for (Map.Entry<String, Module> entry : modules.entrySet()) {
  114. Module module = entry.getValue();
  115. if (module.buf != null && module.buf.length > 0) { // Skip empty modules
  116. moduleSources.put(entry.getKey(), new String(module.buf, modules.charset));
  117. }
  118. }
  119. return moduleSources;
  120. }
  121. protected static class Module {
  122. Integer offset;
  123. byte[] buf;
  124. void read(InputStream in) throws IOException {
  125. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  126. IOUtils.copy(in, out);
  127. out.close();
  128. buf = out.toByteArray();
  129. }
  130. }
  131. protected static class ModuleMap extends HashMap<String, Module> {
  132. Charset charset = Charset.forName("Cp1252"); // default charset
  133. }
  134. /**
  135. * Recursively traverses directory structure rooted at <tt>dir</tt>.
  136. * For each macro module that is found, the module's name and code are
  137. * added to <tt>modules<tt>.
  138. *
  139. * @param dir The directory of entries to look at
  140. * @param modules The resulting map of modules
  141. * @throws IOException If reading the VBA module fails
  142. * @since 3.15-beta2
  143. */
  144. protected void findMacros(DirectoryNode dir, ModuleMap modules) throws IOException {
  145. if (VBA_PROJECT_POIFS.equalsIgnoreCase(dir.getName())) {
  146. // VBA project directory, process
  147. readMacros(dir, modules);
  148. } else {
  149. // Check children
  150. for (Entry child : dir) {
  151. if (child instanceof DirectoryNode) {
  152. findMacros((DirectoryNode)child, modules);
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. * Read <tt>length</tt> bytes of MBCS (multi-byte character set) characters from the stream
  159. *
  160. * @param stream the inputstream to read from
  161. * @param length number of bytes to read from stream
  162. * @param charset the character set encoding of the bytes in the stream
  163. * @return a java String in the supplied character set
  164. * @throws IOException If reading from the stream fails
  165. */
  166. private static String readString(InputStream stream, int length, Charset charset) throws IOException {
  167. byte[] buffer = new byte[length];
  168. int count = stream.read(buffer);
  169. return new String(buffer, 0, count, charset);
  170. }
  171. /**
  172. * reads module from DIR node in input stream and adds it to the modules map for decompression later
  173. * on the second pass through this function, the module will be decompressed
  174. *
  175. * Side-effects: adds a new module to the module map or sets the buf field on the module
  176. * to the decompressed stream contents (the VBA code for one module)
  177. *
  178. * @param in the run-length encoded input stream to read from
  179. * @param streamName the stream name of the module
  180. * @param modules a map to store the modules
  181. * @throws IOException If reading data from the stream or from modules fails
  182. */
  183. private static void readModule(RLEDecompressingInputStream in, String streamName, ModuleMap modules) throws IOException {
  184. int moduleOffset = in.readInt();
  185. Module module = modules.get(streamName);
  186. if (module == null) {
  187. // First time we've seen the module. Add it to the ModuleMap and decompress it later
  188. module = new Module();
  189. module.offset = moduleOffset;
  190. modules.put(streamName, module);
  191. // Would adding module.read(in) here be correct?
  192. } else {
  193. // Decompress a previously found module and store the decompressed result into module.buf
  194. InputStream stream = new RLEDecompressingInputStream(
  195. new ByteArrayInputStream(module.buf, moduleOffset, module.buf.length - moduleOffset)
  196. );
  197. module.read(stream);
  198. stream.close();
  199. }
  200. }
  201. private static void readModule(DocumentInputStream dis, String name, ModuleMap modules) throws IOException {
  202. Module module = modules.get(name);
  203. // TODO Refactor this to fetch dir then do the rest
  204. if (module == null) {
  205. // no DIR stream with offsets yet, so store the compressed bytes for later
  206. module = new Module();
  207. modules.put(name, module);
  208. module.read(dis);
  209. } else if (module.buf == null) { //if we haven't already read the bytes for the module keyed off this name...
  210. if (module.offset == null) {
  211. //This should not happen. bug 59858
  212. throw new IOException("Module offset for '" + name + "' was never read.");
  213. }
  214. // we know the offset already, so decompress immediately on-the-fly
  215. long skippedBytes = dis.skip(module.offset);
  216. if (skippedBytes != module.offset) {
  217. throw new IOException("tried to skip " + module.offset + " bytes, but actually skipped " + skippedBytes + " bytes");
  218. }
  219. InputStream stream = new RLEDecompressingInputStream(dis);
  220. module.read(stream);
  221. stream.close();
  222. }
  223. }
  224. /**
  225. * Skips <tt>n</tt> bytes in an input stream, throwing IOException if the
  226. * number of bytes skipped is different than requested.
  227. * @throws IOException If skipping would exceed the available data or skipping did not work.
  228. */
  229. private static void trySkip(InputStream in, long n) throws IOException {
  230. long skippedBytes = in.skip(n);
  231. if (skippedBytes != n) {
  232. if (skippedBytes < 0) {
  233. throw new IOException(
  234. "Tried skipping " + n + " bytes, but no bytes were skipped. "
  235. + "The end of the stream has been reached or the stream is closed.");
  236. } else {
  237. throw new IOException(
  238. "Tried skipping " + n + " bytes, but only " + skippedBytes + " bytes were skipped. "
  239. + "This should never happen.");
  240. }
  241. }
  242. }
  243. // Constants from MS-OVBA: https://msdn.microsoft.com/en-us/library/office/cc313094(v=office.12).aspx
  244. private static final int EOF = -1;
  245. private static final int VERSION_INDEPENDENT_TERMINATOR = 0x0010;
  246. @SuppressWarnings("unused")
  247. private static final int VERSION_DEPENDENT_TERMINATOR = 0x002B;
  248. private static final int PROJECTVERSION = 0x0009;
  249. private static final int PROJECTCODEPAGE = 0x0003;
  250. private static final int STREAMNAME = 0x001A;
  251. private static final int MODULEOFFSET = 0x0031;
  252. @SuppressWarnings("unused")
  253. private static final int MODULETYPE_PROCEDURAL = 0x0021;
  254. @SuppressWarnings("unused")
  255. private static final int MODULETYPE_DOCUMENT_CLASS_OR_DESIGNER = 0x0022;
  256. @SuppressWarnings("unused")
  257. private static final int PROJECTLCID = 0x0002;
  258. @SuppressWarnings("unused")
  259. private static final int MODULE_NAME = 0x0019;
  260. @SuppressWarnings("unused")
  261. private static final int MODULE_NAME_UNICODE = 0x0047;
  262. @SuppressWarnings("unused")
  263. private static final int MODULE_DOC_STRING = 0x001c;
  264. private static final int STREAMNAME_RESERVED = 0x0032;
  265. /**
  266. * Reads VBA Project modules from a VBA Project directory located at
  267. * <tt>macroDir</tt> into <tt>modules</tt>.
  268. *
  269. * @since 3.15-beta2
  270. */
  271. protected void readMacros(DirectoryNode macroDir, ModuleMap modules) throws IOException {
  272. for (Entry entry : macroDir) {
  273. if (! (entry instanceof DocumentNode)) { continue; }
  274. String name = entry.getName();
  275. DocumentNode document = (DocumentNode)entry;
  276. DocumentInputStream dis = new DocumentInputStream(document);
  277. try {
  278. if ("dir".equalsIgnoreCase(name)) {
  279. // process DIR
  280. RLEDecompressingInputStream in = new RLEDecompressingInputStream(dis);
  281. String streamName = null;
  282. int recordId = 0;
  283. try {
  284. while (true) {
  285. recordId = in.readShort();
  286. if (EOF == recordId
  287. || VERSION_INDEPENDENT_TERMINATOR == recordId) {
  288. break;
  289. }
  290. int recordLength = in.readInt();
  291. switch (recordId) {
  292. case PROJECTVERSION:
  293. trySkip(in, 6);
  294. break;
  295. case PROJECTCODEPAGE:
  296. int codepage = in.readShort();
  297. modules.charset = Charset.forName(CodePageUtil.codepageToEncoding(codepage, true));
  298. break;
  299. case STREAMNAME:
  300. streamName = readString(in, recordLength, modules.charset);
  301. int reserved = in.readShort();
  302. if (reserved != STREAMNAME_RESERVED) {
  303. throw new IOException("Expected x0032 after stream name before Unicode stream name, but found: "+
  304. Integer.toHexString(reserved));
  305. }
  306. int unicodeNameRecordLength = in.readInt();
  307. readUnicodeString(in, unicodeNameRecordLength);
  308. // do something with this at some point
  309. break;
  310. case MODULEOFFSET:
  311. readModule(in, streamName, modules);
  312. break;
  313. default:
  314. trySkip(in, recordLength);
  315. break;
  316. }
  317. }
  318. } catch (final IOException e) {
  319. throw new IOException(
  320. "Error occurred while reading macros at section id "
  321. + recordId + " (" + HexDump.shortToHex(recordId) + ")", e);
  322. }
  323. finally {
  324. in.close();
  325. }
  326. } else if (!startsWithIgnoreCase(name, "__SRP")
  327. && !startsWithIgnoreCase(name, "_VBA_PROJECT")) {
  328. // process module, skip __SRP and _VBA_PROJECT since these do not contain macros
  329. readModule(dis, name, modules);
  330. }
  331. }
  332. finally {
  333. dis.close();
  334. }
  335. }
  336. }
  337. private String readUnicodeString(RLEDecompressingInputStream in, int unicodeNameRecordLength) throws IOException {
  338. byte[] buffer = new byte[unicodeNameRecordLength];
  339. IOUtils.readFully(in, buffer);
  340. return new String(buffer, UTF_16LE);
  341. }
  342. }