{"id":946,"date":"2018-11-28T16:29:07","date_gmt":"2018-11-29T00:29:07","guid":{"rendered":"https:\/\/www.pnfsoftware.com\/blog\/?p=946"},"modified":"2018-12-19T13:32:05","modified_gmt":"2018-12-19T21:32:05","slug":"native-types-and-typelibs-with-jeb","status":"publish","type":"post","link":"https:\/\/www.pnfsoftware.com\/blog\/native-types-and-typelibs-with-jeb\/","title":{"rendered":"Native types and type libraries"},"content":{"rendered":"<p>JEB 3.0.7 ships with our internal type library generation tool. In this post, we will show how to use native types with the client and API, and how power-users can generate custom type libraries.<\/p>\n<h2>Type libraries (typelibs)<\/h2>\n<p>Type libraries are <strong>*.typelib<\/strong> files stored in the JEB&#8217;s\u00a0typelibs\/ folder. They contain type information for a given component (eg, an OS or an SDK), such as:<\/p>\n<ul>\n<li>Types (aliases, structures, enumerations, etc.) and prototypes (~function pointers)<\/li>\n<li>Publicly exported routines<\/li>\n<li>Constants<\/li>\n<\/ul>\n<p>JEB ships with typelibs for major sub-systems (such as Windows win32 (user-mode), Windows Driver Kit (kernel), Linux GNU, Linux Android, etc.) running on the most popular architectures (x86, x86-64, arm, aarch64, mips).<\/p>\n<p>Let&#8217;s see how types can be used to ease your reverse-engineering tasks.<\/p>\n<h2>Using native types with the UI client<\/h2>\n<h3>Applying types<\/h3>\n<p>Using types with JEB is straightforward. If your file&#8217;s target environment was identified (or partially identified), then, matching typelibs will be loaded and their types be made available to the user.<\/p>\n<p>The file shown below is an x86 file compiled for Windows 32-bit:<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/7dca73b1f2eea3234f918f457936adb2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/7dca73b1f2eea3234f918f457936adb2.png\" alt=\"\" width=\"1000\" height=\"544\" \/><\/a><\/p>\n<p>As such,\u00a0 win32 typelibs were loaded. You can verify that by clicking File, Engines, Type Libraries&#8230;:<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/7a0be9e3c5fe885bfbcbb69a99889004.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/7a0be9e3c5fe885bfbcbb69a99889004.png\" alt=\"\" width=\"597\" height=\"353\" \/><\/a><\/p>\n<p>Let&#8217;s define the bytes at address 0x403000 as belonging to a <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms724284(v=vs.85).aspx\">FILETIME<\/a> structure. You may right-click and select <em>Edit Type (Y)<\/em>:<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/26cf8d6bf827ec0464b3b5745a3d299f.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/26cf8d6bf827ec0464b3b5745a3d299f.png\" alt=\"\" width=\"596\" height=\"410\" \/><\/a><\/p>\n<p>and input the exact type name: (the type must exist)<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/37a769ba2631e5ca04bed52b22758111.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/37a769ba2631e5ca04bed52b22758111.png\" alt=\"\" width=\"254\" height=\"182\" \/><\/a><\/p>\n<p>Alternatively, it is easier to select a type using <em>Select Type (T)<\/em>. A list of available types is displayed. Filter on &#8220;FILETIME&#8221;:<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/7425183438645c9d72462c033c14cf95.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/7425183438645c9d72462c033c14cf95.png\" alt=\"\" width=\"499\" height=\"380\" \/><\/a><\/p>\n<p>And apply it.<\/p>\n<p>The resulting updated disassembly listing will be:<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/e7a1fd18ab2ae0d5cfbb8d2a54cdcc31.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/e7a1fd18ab2ae0d5cfbb8d2a54cdcc31.png\" alt=\"\" width=\"658\" height=\"110\" \/><\/a><\/p>\n<h3>Type editor<\/h3>\n<p>JEB features a powerful native type editor, that allows the modification of existing &#8220;complex&#8221; types (that is, structure and derivative) and the definition of new types. Open it with Ctrl+Alt+T (macOS: Cmd+Alt+T).<\/p>\n<p>Below, we are selecting an existing well-known Windows type, IMAGE_DOS_HEADER.<\/p>\n<figure style=\"width: 1044px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/fc72efc9019fbab039a89825d4c32c13.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/fc72efc9019fbab039a89825d4c32c13.png\" alt=\"\" width=\"1044\" height=\"616\" \/><\/a><figcaption class=\"wp-caption-text\">The left panel allows you to define the exact structure layout. The right panel is a C-like view of the structure, with actual offsets.<\/figcaption><\/figure>\n<p>Let&#8217;s create a new type.<\/p>\n<p>To create a structure type, click Create, and input a name, such as MyStruc1. The type editor will display your empty structure:<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/ce4b4808c9b66edaae788b183164257f.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/ce4b4808c9b66edaae788b183164257f.png\" alt=\"\" width=\"799\" height=\"226\" \/><\/a><\/p>\n<p>You may then add or remove fields, using the following hotkeys:<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/24e3429f2afd3d542baee469c53ffce9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/24e3429f2afd3d542baee469c53ffce9.png\" alt=\"\" width=\"807\" height=\"320\" \/><\/a><\/p>\n<p>Here, we define MyStruc1 to be as such: a structure containing primitives, a nested structure, and arrays.<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/12a9bf5e30647e8a0451889acaa407ad.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/12a9bf5e30647e8a0451889acaa407ad.png\" alt=\"\" width=\"1045\" height=\"405\" \/><\/a><\/p>\n<p>As seen earlier, we can apply our type MyStruc1\u00a0 anywhere on bytes, eg at offset 0x403027:<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/84ec3fe7d0b22f9790cd1c3941e1d281.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/84ec3fe7d0b22f9790cd1c3941e1d281.png\" alt=\"\" width=\"656\" height=\"294\" \/><\/a><\/p>\n<h3>Constants<\/h3>\n<p>Typelib files also bundle well-known constants, generally defined in header files with #DEFINE pre-processor commands. You may use them to replace immediate values in your assembly or decompiler views.<\/p>\n<p>Here is an example, again, coming from a Windows win32 file. The following decompiled method makes use of <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/desktop\/api\/winuser\/nf-winuser-sendmessage\">SendMessage<\/a>\u00a0routine:<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/2c40de31f32e400e1839ebf0b65458f0.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/2c40de31f32e400e1839ebf0b65458f0.png\" alt=\"\" width=\"860\" height=\"488\" \/><\/a><\/p>\n<p>Note that the second parameter is the message id. The MSDN provides <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/desktop\/winmsg\/about-messages-and-message-queues\">a long list of well-known ids<\/a>; Most of them are bundled with Windows typelibs shipping with JEB.<\/p>\n<p>Right click on the immediate value (176), and select <em>Replace<\/em> to see what is offered:<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/611425296bd97dbb2fceab36e621c38c.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/611425296bd97dbb2fceab36e621c38c.png\" alt=\"\" width=\"712\" height=\"420\" \/><\/a><\/p>\n<p>Click OK to perform the replacement:<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/095c5057bbc711cd23e7a5cf7be1da89.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/095c5057bbc711cd23e7a5cf7be1da89.png\" alt=\"\" width=\"516\" height=\"116\" \/><\/a><\/p>\n<p>More readable, isn&#8217;t it?<\/p>\n<h2>Custom typelibs<\/h2>\n<p>There exist scenarios where users will want to create their own typelibs, generally when many custom types would have to be created and\/or may need to be reused later. Examples:<\/p>\n<ul>\n<li>Analysis of a Windows kernel component making use of Driver Kit headers whose types were not added to JEB&#8217;s pre-built WDK typelibs (our own\u00a0<em>wdk10-&lt;arch&gt;.typelib<\/em> files do not contain all WDK components, although they do contain the most important ones).<\/li>\n<li>The types of platform X were not compiled for a given architecture (eg, JEB does not ship with Linux types specific to Atmel AVR microcontrollers).<\/li>\n<li>The binary to be analyzed makes use of a third-party SDK and the program is dynamically linked to that SDK. In that scenario, a user may want to generate typelibs for the SDK for the platform of their choosing.<\/li>\n<\/ul>\n<h3>Creating custom typelibs<\/h3>\n<p>Creating a custom typelib file is a fairly simple process: the generator is called by executing your JEB startup script (eg, jeb_wincon.bat) with the following flags:<\/p>\n<pre>$ jeb - c --typelibgen=&lt;typelib_configuration_file&gt;<\/pre>\n<p>JEB ships with a sample typelib cfg file: <strong>typelibs\/custom\/sample-typelib.cfg<\/strong>. This key-value file is mostly self-explanatory, please refer to it for reference. (Below, we focus solely on the two most important entries, <em>hdrsrc<\/em> and <em>cstsrc<\/em>.)<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/3ad0a45eea45a6943ce54c81eb34d0a0.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/3ad0a45eea45a6943ce54c81eb34d0a0.png\" alt=\"\" width=\"646\" height=\"729\" \/><\/a><\/p>\n<p>You may want to copy the sample configuration file and adjust it to match your requirements.<\/p>\n<p>The input files can be either or both of the following:<\/p>\n<ul>\n<li>An aggregated, preprocessed header file: it should contain C types and exported methods<\/li>\n<li>A constant file containing a list of named constants<\/li>\n<\/ul>\n<h3>Types and public routines<\/h3>\n<p>The aggregated header can be generated by pre-processing a simple C file including your target header file(s).<\/p>\n<p>Example: let&#8217;s say we want to generate types for stdio.h, on Windows ARM64 platform. We can use Microsoft Compiler&#8217;s \/P flag to pre-process a sample file, 1.c including the target headers:<\/p>\n<pre>\/\/ 1.c\r\n#include \"stdio.h\"\r\nint main(void) {return 0;}<\/pre>\n<pre>$ cl.exe\" \/P 1.c \/D \"WIN32\" \/D \"NDEBUG\" \/D \"_CONSOLE\" \/D \"_UNICODE\" \/D \"UNICODE\"\u00a0\/D \"_ARM64_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE=1\"<\/pre>\n<p>The resulting file will be quite large &#8211; and is likely to contain much more than just stdio.h type information (all headers recursively-included by stdio.h would be processed as well).<\/p>\n<p>We can rename that file as hdr.h and feed it to JEB&#8217;s Typelib Generator. (entry: hdrsrc)<\/p>\n<p>Quick reference: To preprocess a file with&#8230;<\/p>\n<ul>\n<li>MSVC: use <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/8z9z0bx6.aspx\">the \/P flag<\/a><\/li>\n<li>GCC\/Clang: use <a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Preprocessor-Options.html#Preprocessor-Options\">the -E flag<\/a><\/li>\n<\/ul>\n<h4>JEB&#8217;s built-in C declaration parser<\/h4>\n<p>Our C parser is C11 based, and supports most standard C declarations, as well as common MSVC and GCC extensions. Two important caveats to remember:<\/p>\n<ul>\n<li>anonymous structure bitfields are not supported: things like &#8220;int :4&#8221; will need to be massaged to, eg, &#8220;int _:4&#8221;<\/li>\n<li>anonymous aliased parameter for single-parameter methods are not supported: things like &#8220;void foo(X)&#8221; will need to be massaged to, eg, &#8220;void foo(X _)&#8221;<\/li>\n<\/ul>\n<h3>Predefined constants<\/h3>\n<p>As seen earlier, typelib files can also contain list of named constants &#8211; generally, they will be those constants that are #DEFINE&#8217;d in header files.<\/p>\n<p>They can be scraped from C\/C++ header files. JEB ships with a handy Python script that will help you do that quickly: see <strong>typelibs\/custom\/collectDefines.py<\/strong> (other tools exist, such as GCC&#8217;s dM flag, but they may not generate all constants, only those that are preprocessed with a given set of precompilation parameters).<\/p>\n<p>Example:<\/p>\n<pre>$ .\/collectDefines.py -r w10ddk\r\nCDF_DVCR_625_50_BLOCK_PERIOD:3276\r\nCDF_DVCR_625_50_BLOCK_PERIOD_REMAINDER:800000000\r\nCDROM_AUDIO_CONTROL_PAGE:14\r\nCDROM_CD_TEXT_PACK_ALBUM_NAME:128\r\nCDROM_CD_TEXT_PACK_ARRANGER:132\r\n...\r\n...<\/pre>\n<p>We can save that file as, eg cst.txt, and feed it to JEB&#8217;s Typelib Generator. (entry: cstsrc)<\/p>\n<h3>Loading custom typelibs<\/h3>\n<p>If your typelib configuration matches your input files (most notably, the <em>groupid<\/em> and <em>processor<\/em> fields), then JEB will load it automatically during analysis of your input file.<\/p>\n<p>Example, with the sample typelib shipping with JEB (groupid=GROUPID_TYPELIB_WIN32, processor=X86):<\/p>\n<p><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/abcb6fd41bc69da967513be9cf0b20b5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2018\/11\/abcb6fd41bc69da967513be9cf0b20b5.png\" alt=\"\" width=\"536\" height=\"308\" \/><\/a><\/p>\n<p>Obviously, you may decide to force-load a type lib by ticking the &#8220;Loaded&#8221; checkbox.<\/p>\n<h2>Programmatic access with JEB API<\/h2>\n<p>Native types, like any other component of JEB, can be accessed with the API. Scripts and plugins can use the API to programmatically retrieve, define, apply types, as well as manipulate type libraries.<\/p>\n<p>The two single most important classes are:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.pnfsoftware.com\/jeb\/apidoc\/reference\/com\/pnfsoftware\/jeb\/core\/units\/code\/asm\/type\/ITypeManager.html\">ITypeManager<\/a>: manager of native types for a given INativeCodeUnit<\/li>\n<li><a href=\"https:\/\/www.pnfsoftware.com\/jeb\/apidoc\/reference\/com\/pnfsoftware\/jeb\/core\/units\/code\/asm\/type\/TypeLibraryService.html\">TypeLibraryService<\/a>: the single entry-point to all typelibs<\/li>\n<\/ul>\n<p>Below is a reference to a sample JEB Python script that will get you started with the API. It shows how to define the following custom type:<\/p>\n<pre>struct MyStruct1 {\r\n  int a;\r\n  unsigned char[3][2] b;\r\n};<\/pre>\n<p>Source: <a href=\"https:\/\/github.com\/pnfsoftware\/jeb2-samplecode\/blob\/master\/scripts\/AddCustomNativeTypes.py\">https:\/\/github.com\/pnfsoftware\/jeb2-samplecode\/blob\/master\/scripts\/AddCustomNativeTypes.py<\/a><\/p>\n<p>We shall upload more sample scripts in the future. Feel free to share your contributions with us as well.<\/p>\n<h2>Conclusion<\/h2>\n<p>If you have questions, comments or suggestions, feel free to:<\/p>\n<ul>\n<li>leave a comment on this post<\/li>\n<li>email <a href=\"mailto:contact@pnfsoftware.com\">contact@pnfsoftware.com<\/a><\/li>\n<li>message us on <a href=\"https:\/\/jebdecompiler.slack.com\">Slack<\/a><\/li>\n<li>or send us a Tweet <a href=\"https:\/\/twitter.com\/jebdec\">@jebdec<\/a><\/li>\n<\/ul>\n<p>JEB3 is still in Beta, for a few more weeks. General availability should be expected during the first or second week of January. If you haven&#8217;t done so, feel free to ask for a Beta build right away.<\/p>\n<p>Once again, thank you to all our users, we are very grateful for your feedback and support. Finally, a special thank you note to our user &#8220;Andy P.&#8221; who pushed JEB&#8217;s boundaries relatively far (!) and allowed us to uncover interesting corner cases when working with large firmware binaries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JEB 3.0.7 ships with our internal type library generation tool. In this post, we will show how to use native types with the client and API, and how power-users can generate custom type libraries. Type libraries (typelibs) Type libraries are *.typelib files stored in the JEB&#8217;s\u00a0typelibs\/ folder. They contain type information for a given component &hellip; <a href=\"https:\/\/www.pnfsoftware.com\/blog\/native-types-and-typelibs-with-jeb\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Native types and type libraries<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,13],"tags":[],"class_list":["post-946","post","type-post","status-publish","format-standard","hentry","category-jeb3","category-native-code"],"_links":{"self":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/posts\/946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/comments?post=946"}],"version-history":[{"count":0,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/posts\/946\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/media?parent=946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/categories?post=946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/tags?post=946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}