diff --git a/Changelog.txt b/Changelog.txt deleted file mode 100644 index 4bc8842..0000000 --- a/Changelog.txt +++ /dev/null @@ -1,82 +0,0 @@ -PatchDiff v2.0.10a: - -* Uses idaq.exe on Windows as idag.exe is not always present anymore (IDA 6.1 Standard?) - -PatchDiff v2.0.10: - -* Fixes idc issue with IDA 6.1 -* Fixes Unix support (tested on Fedora 15 32bit) - -PatchDiff v2.0.9: - -* Improves 'direct jump' detection (better compiler change detection in the next version) -* Fixes tmp file issue with IDA 5.7 - -PatchDiff v2.0.8: - -* Adds Flag/Unflag feature for the 'matched' list to keep track of analyzed functions - - -PatchDiff v2.0.7: - -* Properly fixes an issue where a int -> to unsigned int would not be detected -* Adds dock support to display graphs (IDA >= 5.6) - - -PatchDiff v2.0.6: - -* Switchs to graph call for checksum instead of instruction frequency -* Removes invalid C++ classes/structs flagged as functions - - -PatchDiff v2.0.5: - -* Adds string references to the signature -* Fixes IPC close when option is disabled - - -PatchDiff v2.0.4: - -* Requires at least IDA 5.2 -* Adds save backup results to IDB -* Adds Unmatch/Set match/Switch match submenus -* Adds "pipe" support to keep second IDA instance open - - menu Options/PatchDiff2 to disable/enable it per IDB - - registry HKLM\SOFTWARE\Tenable\PatchDiff2 IPC (DWORD) for the default setting -* Uses demangled function names -* Ignores duplicated names - - -PatchDiff v2.0.3: - -* Adds support for C++ classes in the signature engine (improves results against c++ targets) -* No longer relies on IDA code refs (due to bad references) -* x86: merges inc reg and dec reg to one instruction -* x86: handles jmp $2/$5 -* x86: stops block tracing on int3 -* Bugfix: Does not try to display graphs that IDA can't handle - - -PatchDiff v2.0.2: - -* Adds support for IDA 5.1 -* Excludes data ref with segment reference from crc -* Adds PPC optimization module -* Relies on IDA to identify opcode for non optimized CPUs -* Adds display graph option for unmatched functions -* Adds x86 rep/repe/repne 16 bit compiler change optimization -* Improves edge double click handling - - -PatchDiff v2.0.1: - -* Increases the size of the command buffer (for long IDB paths) -* Synchronizes graphs on edge double-click -* Adds line address in the graph if the option is set in IDA -* Fixes graph block matching to produce much better results -* Uses %a for address - - -PatchDiff v2.0.0: - -* Initial public release diff --git a/LICENSE b/LICENSE deleted file mode 100644 index d159169..0000000 --- a/LICENSE +++ /dev/null @@ -1,339 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -License is intended to guarantee your freedom to share and change free -software--to make sure the software is free for all its users. This -General Public License applies to most of the Free Software -Foundation's software and to any other program whose authors commit to -using it. (Some other Free Software Foundation software is covered by -the GNU Lesser General Public License instead.) You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -this service if you wish), that you receive source code or can get it -if you want it, that you can change the software or use pieces of it -in new free programs; and that you know you can do these things. - - To protect your rights, we need to make restrictions that forbid -anyone to deny you these rights or to ask you to surrender the rights. -These restrictions translate to certain responsibilities for you if you -distribute copies of the software, or if you modify it. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must give the recipients all the rights that -you have. You must make sure that they, too, receive or can get the -source code. And you must show them these terms so they know their -rights. - - We protect your rights with two steps: (1) copyright the software, and -(2) offer you this license which gives you legal permission to copy, -distribute and/or modify the software. - - Also, for each author's protection and ours, we want to make certain -that everyone understands that there is no warranty for this free -software. If the software is modified by someone else and passed on, we -want its recipients to know that what they have is not the original, so -that any problems introduced by others will not reflect on the original -authors' reputations. - - Finally, any free program is threatened constantly by software -patents. We wish to avoid the danger that redistributors of a free -program will individually obtain patent licenses, in effect making the -program proprietary. To prevent this, we have made it clear that any -patent must be licensed for everyone's free use or not licensed at all. - - The precise terms and conditions for copying, distribution and -modification follow. - - GNU GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License applies to any program or other work which contains -a notice placed by the copyright holder saying it may be distributed -under the terms of this General Public License. The "Program", below, -refers to any such program or work, and a "work based on the Program" -means either the Program or any derivative work under copyright law: -that is to say, a work containing the Program or a portion of it, -either verbatim or with modifications and/or translated into another -language. (Hereinafter, translation is included without limitation in -the term "modification".) Each licensee is addressed as "you". - -Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running the Program is not restricted, and the output from the Program -is covered only if its contents constitute a work based on the -Program (independent of having been made by running the Program). -Whether that is true depends on what the Program does. - - 1. You may copy and distribute verbatim copies of the Program's -source code as you receive it, in any medium, provided that you -conspicuously and appropriately publish on each copy an appropriate -copyright notice and disclaimer of warranty; keep intact all the -notices that refer to this License and to the absence of any warranty; -and give any other recipients of the Program a copy of this License -along with the Program. - -You may charge a fee for the physical act of transferring a copy, and -you may at your option offer warranty protection in exchange for a fee. - - 2. You may modify your copy or copies of the Program or any portion -of it, thus forming a work based on the Program, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) You must cause the modified files to carry prominent notices - stating that you changed the files and the date of any change. - - b) You must cause any work that you distribute or publish, that in - whole or in part contains or is derived from the Program or any - part thereof, to be licensed as a whole at no charge to all third - parties under the terms of this License. - - c) If the modified program normally reads commands interactively - when run, you must cause it, when started running for such - interactive use in the most ordinary way, to print or display an - announcement including an appropriate copyright notice and a - notice that there is no warranty (or else, saying that you provide - a warranty) and that users may redistribute the program under - these conditions, and telling the user how to view a copy of this - License. (Exception: if the Program itself is interactive but - does not normally print such an announcement, your work based on - the Program is not required to print an announcement.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Program, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Program, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Program. - -In addition, mere aggregation of another work not based on the Program -with the Program (or with a work based on the Program) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may copy and distribute the Program (or a work based on it, -under Section 2) in object code or executable form under the terms of -Sections 1 and 2 above provided that you also do one of the following: - - a) Accompany it with the complete corresponding machine-readable - source code, which must be distributed under the terms of Sections - 1 and 2 above on a medium customarily used for software interchange; or, - - b) Accompany it with a written offer, valid for at least three - years, to give any third party, for a charge no more than your - cost of physically performing source distribution, a complete - machine-readable copy of the corresponding source code, to be - distributed under the terms of Sections 1 and 2 above on a medium - customarily used for software interchange; or, - - c) Accompany it with the information you received as to the offer - to distribute corresponding source code. (This alternative is - allowed only for noncommercial distribution and only if you - received the program in object code or executable form with such - an offer, in accord with Subsection b above.) - -The source code for a work means the preferred form of the work for -making modifications to it. For an executable work, complete source -code means all the source code for all modules it contains, plus any -associated interface definition files, plus the scripts used to -control compilation and installation of the executable. However, as a -special exception, the source code distributed need not include -anything that is normally distributed (in either source or binary -form) with the major components (compiler, kernel, and so on) of the -operating system on which the executable runs, unless that component -itself accompanies the executable. - -If distribution of executable or object code is made by offering -access to copy from a designated place, then offering equivalent -access to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source along with the object code. - - 4. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt -otherwise to copy, modify, sublicense or distribute the Program is -void, and will automatically terminate your rights under this License. -However, parties who have received copies, or rights, from you under -this License will not have their licenses terminated so long as such -parties remain in full compliance. - - 5. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Program or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Program (or any work based on the -Program), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Program or works based on it. - - 6. Each time you redistribute the Program (or any work based on the -Program), the recipient automatically receives a license from the -original licensor to copy, distribute or modify the Program subject to -these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties to -this License. - - 7. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Program at all. For example, if a patent -license would not permit royalty-free redistribution of the Program by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Program. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system, which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 8. If the distribution and/or use of the Program is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Program under this License -may add an explicit geographical distribution limitation excluding -those countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 9. The Free Software Foundation may publish revised and/or new versions -of the General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - -Each version is given a distinguishing version number. If the Program -specifies a version number of this License which applies to it and "any -later version", you have the option of following the terms and conditions -either of that version or of any later version published by the Free -Software Foundation. If the Program does not specify a version number of -this License, you may choose any version ever published by the Free Software -Foundation. - - 10. If you wish to incorporate parts of the Program into other free -programs whose distribution conditions are different, write to the author -to ask for permission. For software which is copyrighted by the Free -Software Foundation, write to the Free Software Foundation; we sometimes -make exceptions for this. Our decision will be guided by the two goals -of preserving the free status of all derivatives of our free software and -of promoting the sharing and reuse of software generally. - - NO WARRANTY - - 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY -FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN -OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES -PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED -OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS -TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE -PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, -REPAIR OR CORRECTION. - - 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR -REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, -INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING -OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED -TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY -YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER -PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) year name of author - Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - , 1 April 1989 - Ty Coon, President of Vice - -This General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may -consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. diff --git a/ProjectHome.md b/ProjectHome.md new file mode 100644 index 0000000..7478bff --- /dev/null +++ b/ProjectHome.md @@ -0,0 +1,10 @@ +PatchDiff2 is a plugin for the IDA dissassembler that can analyze two IDB files and find the differences between both. PatchDiff2 is free and fully integrates with the latest version of IDA (6.1) on Windows and Linux. +The plugin can perform the following tasks : + + * Display the list of identical functions + * Display the list of matched functions + * Display the list of unmatched functions (with the CRC) + * Display a flow graph for identical and matched functions + +The main purpose of this plugin is to be fast and give accurate results when working on a security patch or a hotfix. Therefore this tool is not made to find similar functions between two different programs. +Patchdiff2 supports all processors that IDA can handle and is available in two versions: 32 bit and a 64 bit. \ No newline at end of file diff --git a/backup.cpp b/backup.cpp deleted file mode 100644 index 3bbcc8c..0000000 --- a/backup.cpp +++ /dev/null @@ -1,625 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "sig.hpp" -#include "diff.hpp" -#include "options.hpp" -#include "backup.hpp" - - -static void buffer_serialize_data(char * buf, size_t blen, size_t * pos, void * data, size_t dsize) -{ - if ( (*pos + dsize) > blen) - { - *pos = blen; - return; - } - - memcpy(buf+*pos, data, dsize); - *pos += dsize; -} - - -static void buffer_unserialize_data(char * buf, size_t blen, size_t * pos, void * data, size_t dsize) -{ - if ( (*pos + dsize) > blen) - { - *pos = blen; - return; - } - - memcpy(data, buf+*pos, dsize); - *pos += dsize; -} - - -static void buffer_serialize_ea(char * buf, size_t blen, size_t * pos, ea_t ea) -{ - buffer_serialize_data(buf, blen, pos, &ea, sizeof(ea)); -} - - -static void buffer_serialize_bool(char * buf, size_t blen, size_t * pos, bool b) -{ - buffer_serialize_data(buf, blen, pos, &b, sizeof(b)); -} - - -static void buffer_serialize_char(char * buf, size_t blen, size_t * pos, char c) -{ - buffer_serialize_data(buf, blen, pos, &c, sizeof(c)); -} - - -static void buffer_serialize_int(char * buf, size_t blen, size_t * pos, int i) -{ - buffer_serialize_data(buf, blen, pos, &i, sizeof(i)); -} - - -static void buffer_serialize_long(char * buf, size_t blen, size_t * pos, unsigned long l) -{ - buffer_serialize_data(buf, blen, pos, &l, sizeof(l)); -} - - -static void buffer_serialize_size(char * buf, size_t blen, size_t * pos, size_t size) -{ - buffer_serialize_data(buf, blen, pos, &size, sizeof(size)); -} - - -static void buffer_unserialize_ea(char * buf, size_t blen, size_t * pos, ea_t * ea) -{ - buffer_unserialize_data(buf, blen, pos, ea, sizeof(*ea)); -} - - -static void buffer_unserialize_bool(char * buf, size_t blen, size_t * pos, bool * b) -{ - buffer_unserialize_data(buf, blen, pos, b, sizeof(*b)); -} - - -static void buffer_unserialize_char(char * buf, size_t blen, size_t * pos, char * c) -{ - buffer_unserialize_data(buf, blen, pos, c, sizeof(*c)); -} - - -static void buffer_unserialize_int(char * buf, size_t blen, size_t * pos, int * i) -{ - buffer_unserialize_data(buf, blen, pos, i, sizeof(*i)); -} - - -static void buffer_unserialize_long(char * buf, size_t blen, size_t * pos, unsigned long * l) -{ - buffer_unserialize_data(buf, blen, pos, l, sizeof(*l)); -} - - -static void buffer_unserialize_size(char * buf, size_t blen, size_t * pos, size_t * size) -{ - buffer_unserialize_data(buf, blen, pos, size, sizeof(*size)); -} - - -/*------------------------------------------------*/ -/* function : buffer_serialize_string */ -/* description: Serializes a string */ -/*------------------------------------------------*/ - -static void buffer_serialize_string(char * buf, size_t blen, size_t * pos, char * s) -{ - size_t len; - - len = strlen(s) + 1; - if ( (*pos + len) > blen) - { - *pos = blen; - return; - } - - memcpy(buf+*pos, s, len); - *pos += len; -} - - -/*------------------------------------------------*/ -/* function : buffer_unserialize_string */ -/* description: Unserializes a string */ -/*------------------------------------------------*/ - -static void buffer_unserialize_string(char * buf, size_t blen, size_t * pos, char ** s) -{ - *s = qstrdup(buf+*pos); - *pos += strlen(*s) + 1; -} - - -/*------------------------------------------------*/ -/* function : singleton_serialize */ -/* description: Serializes a signature */ -/*------------------------------------------------*/ - -static size_t singleton_serialize(char * buf, size_t blen, psig_t * s, int nfile) -{ - size_t pos = 0; - fref_t * fref = NULL; - int num, i; - - buffer_serialize_ea(buf, blen, &pos, s->startEA); - - buffer_serialize_int(buf, blen, &pos, s->mtype); - - buffer_serialize_int(buf, blen, &pos, s->id_crc); - buffer_serialize_int(buf, blen, &pos, nfile); - - buffer_serialize_long(buf, blen, &pos, s->sig); - buffer_serialize_long(buf, blen, &pos, s->hash); - buffer_serialize_long(buf, blen, &pos, s->crc_hash); - buffer_serialize_long(buf, blen, &pos, s->str_hash); - buffer_serialize_int(buf, blen, &pos, s->flag); - buffer_serialize_long(buf, blen, &pos, s->lines); - - num= 0; - if (s->srefs) - { - fref = s->srefs->list; - num = s->srefs->num; - } - - buffer_serialize_int(buf, blen, &pos, num); - for (i=0; iea); - buffer_serialize_int(buf, blen, &pos, fref->type); - - fref = fref->next; - } - - if (nfile == 2) - { - if (!strncmp(s->name, "sub_", 4)) - { - buffer_serialize_char(buf, blen, &pos, 0); - } - else - { - buffer_serialize_char(buf, blen, &pos, 1); - buffer_serialize_string(buf, blen, &pos, s->name); - } - } - - return pos; -} - - -/*------------------------------------------------*/ -/* function : singleton_unserialize */ -/* description: Serializes a signature */ -/*------------------------------------------------*/ - -static size_t singleton_unserialize(char * buf, size_t blen, psig_t ** s, int version) -{ - char tmp[512]; - size_t pos = 0; - char c; - int num, i; - ea_t ea; - int type; - - *s = sig_init(); - if (!(*s)) return 0; - - buffer_unserialize_ea(buf, blen, &pos, &(*s)->startEA); - - buffer_unserialize_int(buf, blen, &pos, &(*s)->mtype); - - buffer_unserialize_int(buf, blen, &pos, &(*s)->id_crc); - buffer_unserialize_int(buf, blen, &pos, &(*s)->nfile); - - buffer_unserialize_long(buf, blen, &pos, &(*s)->sig); - buffer_unserialize_long(buf, blen, &pos, &(*s)->hash); - buffer_unserialize_long(buf, blen, &pos, &(*s)->crc_hash); - - if (version >= 2) - buffer_unserialize_long(buf, blen, &pos, &(*s)->str_hash); - if (version >= 3) - buffer_unserialize_int(buf, blen, &pos, &(*s)->flag); - - buffer_unserialize_long(buf, blen, &pos, &(*s)->lines); - - buffer_unserialize_int(buf, blen, &pos, &num); - for (i=0; infile == 2) - { - buffer_unserialize_char(buf, blen, &pos, &c); - - if (c == 1) - buffer_unserialize_string(buf, blen, &pos, &(*s)->name); - else - { - qsnprintf(tmp, sizeof(tmp), "sub_%a", (*s)->startEA); - sig_set_name((*s), tmp); - } - } - else - { - pget_func_name((*s)->startEA, tmp, sizeof(tmp)); - sig_set_name(*s, tmp); - } - - return pos; -} - - -/*------------------------------------------------*/ -/* function : pair_serialize */ -/* description: Serializes a signature and the */ -/* matched signature */ -/*------------------------------------------------*/ - -static size_t pair_serialize(char * buf, size_t blen, psig_t * s) -{ - size_t len; - - len = singleton_serialize(buf, blen, s, 1); - len += singleton_serialize(buf+len, blen-len, s->msig, 2); - - return len; -} - - -/*------------------------------------------------*/ -/* function : pair_unserialize */ -/* description: Unserializes a signature and the */ -/* matched signature */ -/*------------------------------------------------*/ - -static size_t pair_unserialize(char * buf, size_t blen, psig_t ** s, int version) -{ - size_t len; - - len = singleton_unserialize(buf, blen, s, version); - len += singleton_unserialize(buf+len, blen-len, &(*s)->msig, version); - - (*s)->msig->msig = (*s); - - return len; -} - - -/*------------------------------------------------*/ -/* function : backup_save_list */ -/* description: Backups result list inside a */ -/* netnode */ -/*------------------------------------------------*/ - -static void backup_save_list(char * node_name, slist_t * sl) -{ - char buf[5000]; - size_t i; - size_t len; - netnode node; - - if (!sl) return; - - node.create(node_name); - - for (i=0; inum; i++) - { - nodeidx_t nidx = node.altval(i); - if (nidx != 0) - { - msg("backup failed: netnode already exists !!\n"); - return; - } - - netnode n(nidx); - - n.create(); - node.altset(i, n); - - if (sl->sigs[i]->msig != NULL) - len = pair_serialize(buf, sizeof(buf), sl->sigs[i]); - else - len = singleton_serialize(buf, sizeof(buf), sl->sigs[i], sl->sigs[i]->nfile); - - n.setblob(buf, len, 0, 'P'); - } -} - - -/*------------------------------------------------*/ -/* function : backup_load_list */ -/* description: Loads result list from a netnode */ -/*------------------------------------------------*/ - -static bool backup_load_list(char * node_name, slist_t * sl, int type, int version) -{ - char buf[5000]; - size_t i; - size_t len; - netnode node; - nodeidx_t nidx; - - if (!sl) return true; - - node.create(node_name); - - for (i=0; iorg_num; i++) - { - nidx = node.altval(i); - if (nidx == BADNODE) - { - msg("backup failed: netnode does not exist !!\n"); - return false; - } - - netnode n(nidx); - - len = sizeof(buf); - if (!n.getblob(buf, &len, 0, 'P')) - { - msg("backup failed: netnode blob does not exist !!\n"); - return false; - } - - if (type) - pair_unserialize(buf, len, &sl->sigs[i], version); - else - singleton_unserialize(buf, len, &sl->sigs[i], version); - } - - sl->num = sl->org_num; - - return true; -} - - -/*------------------------------------------------*/ -/* function : backup_free_node */ -/* description: Removes node from the IDB */ -/*------------------------------------------------*/ - -static void backup_free_node(char * node_name, size_t size) -{ - netnode node; - size_t i; - - node.create(node_name); - - for (i=0; imlist ? eng->mlist->org_num : 0); - backup_free_node("$ pdiff2_identical", eng->ilist ? eng->ilist->org_num : 0); - backup_free_node("$ pdiff2_unmatched", eng->ulist ? eng->ulist->org_num : 0); - - backup_free_node("$ pdiff2_eng", 1); -} - - -/*------------------------------------------------*/ -/* function : backup_save_eng */ -/* description:Saves engine data inside a netnode */ -/*------------------------------------------------*/ - -static void backup_save_eng(char * node_name, deng_t * eng) -{ - char buf[1000]; - char * file; - size_t pos = 0; - netnode node; - size_t msize, isize, usize; - - node.create(node_name); - - nodeidx_t nidx = node.altval(0); - if (nidx != 0) - { - backup_cleanup(eng); - node.create(node_name); - - nidx = node.altval(0); - if (nidx != 0) - { - msg("backup eng failed: netnode already exists !!\n"); - return; - } - } - - netnode n(nidx); - - n.create(); - node.altset(0, n); - - msize = isize = usize = 0; - if (eng->mlist) - { - file = eng->mlist->file; - msize = eng->mlist->num; - } - if (eng->ilist) - { - file = eng->ilist->file; - isize = eng->ilist->num; - } - if (eng->ulist) - { - file = eng->ulist->file; - usize = eng->ulist->num; - } - - buffer_serialize_int(buf, sizeof(buf), &pos, PDIFF_BACKUP_VERSION); - buffer_serialize_string(buf, sizeof(buf), &pos, file); - buffer_serialize_size(buf, sizeof(buf), &pos, msize); - buffer_serialize_size(buf, sizeof(buf), &pos, isize); - buffer_serialize_size(buf, sizeof(buf), &pos, usize); - buffer_serialize_bool(buf, sizeof(buf), &pos, eng->opt->ipc); - - n.setblob(buf, pos, 0, 'P'); -} - - -/*------------------------------------------------*/ -/* function : backup_load_file */ -/* description:Loads engine data inside a netnode */ -/*------------------------------------------------*/ - -static deng_t * backup_load_eng(char * node_name, options_t * opt, int * version) -{ - char buf[1000]; - deng_t * eng; - char * file = NULL; - size_t blen, pos = 0; - netnode node; - size_t msize, isize, usize; - - node.create(node_name); - - nodeidx_t nidx = node.altval(0); - if (nidx == 0) - return NULL; - - netnode n(nidx); - - blen = sizeof(buf); - n.getblob(buf, &blen, 0, 'P'); - if (!blen) - return NULL; - - eng = (deng_t *)qalloc(sizeof(*eng)); - if (!eng) - return NULL; - - eng->magic = 0x0BADF00D; - eng->wnum = 0; - eng->opt = opt; - msize = isize = usize = 0; - - buffer_unserialize_int(buf, sizeof(buf), &pos, version); - buffer_unserialize_string(buf, sizeof(buf), &pos, &file); - buffer_unserialize_size(buf, sizeof(buf), &pos, &msize); - buffer_unserialize_size(buf, sizeof(buf), &pos, &isize); - buffer_unserialize_size(buf, sizeof(buf), &pos, &usize); - buffer_unserialize_bool(buf, sizeof(buf), &pos, &eng->opt->ipc); - - // no need to save that in the IDB - eng->opt->save_db = true; - - eng->mlist = siglist_init(msize, file); - eng->ilist = siglist_init(isize, file); - eng->ulist = siglist_init(usize, file); - - return eng; -} - - -/*------------------------------------------------*/ -/* function : backup_save_results */ -/* description: Saves diff results inside the */ -/* IDB */ -/*------------------------------------------------*/ - -void backup_save_results(deng_t * eng) -{ - backup_save_eng("$ pdiff2_eng", eng); - - backup_save_list("$ pdiff2_matched", eng->mlist); - backup_save_list("$ pdiff2_identical", eng->ilist); - backup_save_list("$ pdiff2_unmatched", eng->ulist); -} - - -/*------------------------------------------------*/ -/* function : backup_load_results */ -/* description: Loads diff results from the IDB */ -/*------------------------------------------------*/ - -int backup_load_results(deng_t ** eng, options_t * opt) -{ - int ret; - int version; - - if (*eng) - { - ret = askbuttons_c("Reuse", "Refresh", "Cancel", 1, - "Previous diff results have been found. Please specify the action to perform."); - - if (ret == 0) - { - diff_engine_free(*eng); - *eng = NULL; - } - - return ret; - } - - - *eng = backup_load_eng("$ pdiff2_eng", opt, &version); - if (!(*eng)) return 0; - - ret = askbuttons_c("Reuse", "Refresh", "Cancel", 1, - "Previous diff results have been found. Please specify the action to perform."); - - if (ret != 1) goto error; - - msg("Loading backup results... "); - if (!backup_load_list("$ pdiff2_matched", (*eng)->mlist, 1, version)) goto error; - if (!backup_load_list("$ pdiff2_identical", (*eng)->ilist, 1, version)) goto error; - if (!backup_load_list("$ pdiff2_unmatched", (*eng)->ulist, 0, version)) goto error; - msg("done.\n"); - - return ret; - -error: - diff_engine_free(*eng); - *eng = NULL; - - return ret; -} diff --git a/backup.hpp b/backup.hpp deleted file mode 100644 index 8e77b3c..0000000 --- a/backup.hpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __BACKUP_H__ -#define __BACKUP_H__ - -#include "sig.hpp" - -// 1: first format -// 2: adds str hash -#define PDIFF_BACKUP_VERSION 3 - -void backup_save_results(deng_t *); -int backup_load_results(deng_t **, options_t *); - -#endif \ No newline at end of file diff --git a/clist.cpp b/clist.cpp deleted file mode 100644 index 09d2740..0000000 --- a/clist.cpp +++ /dev/null @@ -1,236 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "sig.hpp" -#include "hash.hpp" - -/*------------------------------------------------*/ -/* function : clist_init */ -/* description: Initializes a chained list of */ -/* signatures */ -/*------------------------------------------------*/ - -clist_t * clist_init(slist_t * l) -{ - clist_t * cl; - dpsig_t * ds, * prev; - size_t i; - - cl = (clist_t *)qalloc(sizeof(*cl)); - if (!cl) return NULL; - - cl->num = l->num; - cl->sigs = NULL; - cl->nmatch = 0; - cl->msigs = NULL; - - prev = NULL; - - for(i=0; inum; i++) - { - ds = (dpsig_t *)qalloc(sizeof(*ds)); - ds->prev = prev; - ds->next = NULL; - ds->removed = false; - ds->sig = l->sigs[i]; - - if (prev) - prev->next = ds; - else - cl->sigs = ds; - - prev = ds; - } - - cl->pos = cl->sigs; - - return cl; -} - - -/*------------------------------------------------*/ -/* function : clist_insert */ -/* description: Inserts sig in sorted list */ -/*------------------------------------------------*/ - -int clist_insert(clist_t * cl, psig_t * s) -{ - dpsig_t * ds, * prev, * cur; - int ret; - - ds = (dpsig_t *)qalloc(sizeof(*ds)); - if (!ds) return -1; - - ds->sig = s; - ds->prev = NULL; - ds->next = NULL; - ds->removed = false; - - prev = NULL; - cur = cl->sigs; - while (cur) - { - // sig_compare is reversed - ret = sig_compare(&s, &cur->sig) ; - if (!ret && cur->sig->startEA == s->startEA) - return -1; - - if (ret <= 0) - break; - - prev = cur; - cur = cur->next; - } - - ds->prev = prev; - ds->next = cur; - - if (!prev) - cl->sigs = ds; - else - prev->next = ds; - - if (cur) cur->prev = ds; - - cl->num++; - - return 0; -} - -/*------------------------------------------------*/ -/* function : clist_insert_dsig */ -/* description: Inserts dsig in matched list */ -/*------------------------------------------------*/ - -static int clist_insert_dsig(clist_t * cl, dpsig_t * ds) -{ - dpsig_t * prev, * cur; - int ret; - - ds->prev = NULL; - ds->next = NULL; - ds->removed = true; - - prev = NULL; - cur = cl->msigs; - while (cur) - { - // sig_compare is reversed - ret = sig_compare(&ds->sig, &cur->sig) ; - if (!ret && cur->sig->startEA == ds->sig->startEA) - return -1; - - if (ret <= 0) - break; - - prev = cur; - cur = cur->next; - } - - ds->prev = prev; - ds->next = cur; - - if (!prev) - cl->msigs= ds; - else - prev->next = ds; - - if (cur) cur->prev = ds; - - cl->nmatch++; - - return 0; -} - - - -/*------------------------------------------------*/ -/* function : clist_init */ -/* description: Initializes a chained list of */ -/* signatures with a list of xrefs */ -/*------------------------------------------------*/ - -clist_t * clist_init_from_refs(hpsig_t * hsig, frefs_t * refs) -{ - clist_t * cl; - fref_t * fl; - psig_t * sig; - - cl = (clist_t *)qalloc(sizeof(*cl)); - if (!cl) return NULL; - - cl->num = 0; - cl->nmatch = 0; - cl->sigs = NULL; - cl->pos = NULL; - cl->msigs = NULL; - - if (!refs) return cl; - - fl = refs->list; - - while(fl) - { - sig = hash_find_ea(hsig, fl->ea); - if (sig && sig_get_matched_type(sig) == DIFF_UNMATCHED) - clist_insert(cl, sig); - - fl = fl->next; - } - - cl->pos = cl->sigs; - - return cl; -} - - -/*------------------------------------------------*/ -/* function : clist_remove */ -/* description: Removes element from list */ -/*------------------------------------------------*/ - -void clist_remove(clist_t * cl, dpsig_t * ds) -{ - if (ds->removed == true) - return; - - if (ds->prev == NULL) - cl->sigs = ds->next; - else - ds->prev->next = ds->next; - - if (ds->next != NULL) - ds->next->prev = ds->prev; - - clist_insert_dsig(cl, ds); -} - - -/*------------------------------------------------*/ -/* function : clist_reset */ -/* description: Resets list position */ -/*------------------------------------------------*/ - -void clist_reset(clist_t * cl) -{ - cl->pos = cl->sigs; -} - diff --git a/clist.hpp b/clist.hpp deleted file mode 100644 index cfca2c2..0000000 --- a/clist.hpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __WIN_FCT_H__ -#define __WIN_FCT_H__ - -#include "sig.hpp" -#include "hash.hpp" - -clist_t * clist_init(slist_t *); -int clist_insert(clist_t *, psig_t *); -clist_t * clist_init_from_refs(hpsig_t *, frefs_t *); -void clist_remove(clist_t *, dpsig_t *); -void clist_reset(clist_t *); - -#endif diff --git a/diff.cpp b/diff.cpp deleted file mode 100644 index 017e375..0000000 --- a/diff.cpp +++ /dev/null @@ -1,681 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "hash.hpp" -#include "sig.hpp" -#include "diff.hpp" -#include "clist.hpp" -#include "display.hpp" -#include "backup.hpp" -#include "options.hpp" - - -/*------------------------------------------------*/ -/* function : diff_init_hash */ -/* description: Initializes a hash structure and */ -/* creates successor xrefs */ -/*------------------------------------------------*/ - -static hpsig_t * diff_init_hash(slist_t * sl) -{ - fref_t * fref; - psig_t * sig; - hpsig_t * h; - size_t i; - - h = hash_init(sl->num); - if (!h) return NULL; - - for(i=0; inum; i++) - { - if (hash_add_ea(h, sl->sigs[i]) < 0) - { - hash_free(h); - return NULL; - } - } - // adds xrefs - for(i=0; inum; i++) - { - if (sl->sigs[i]->srefs) - { - fref = sl->sigs[i]->srefs->list; - while (fref) - { - if (!fref->rtype) - { - sig = hash_find_ea(h, fref->ea); - if (sig) - sig_add_pref(sig, sl->sigs[i]->startEA, fref->type, DO_NOT_CHECK_REF); - } - - fref = fref->next; - } - } - - if (sl->sigs[i]->prefs) - { - fref = sl->sigs[i]->prefs->list; - while (fref) - { - if (!fref->rtype) - { - sig = hash_find_ea(h, fref->ea); - if (sig) - sig_add_sref(sig, sl->sigs[i]->startEA, fref->type, DO_NOT_CHECK_REF); - } - - fref = fref->next; - } - } - } - - return h; -} - - -/*------------------------------------------------*/ -/* function : slist_init_crefs */ -/* description: Initializes slist crefs */ -/*------------------------------------------------*/ - -static int slist_init_crefs(slist_t * l) -{ - hpsig_t * h = NULL; - clist_t * cl1, * cl2; - size_t i; - - h = diff_init_hash(l); - if(!h) return -1; - - for (i=0; inum; i++) - { - cl1 = clist_init_from_refs(h, sig_get_preds(l->sigs[i])); - cl2 = clist_init_from_refs(h, sig_get_succs(l->sigs[i])); - sig_set_crefs(l->sigs[i], SIG_PRED, cl1); - sig_set_crefs(l->sigs[i], SIG_SUCC, cl2); - } - - return 0; -} - - -/*------------------------------------------------*/ -/* function : diff_engine_initialize */ -/* description: Initializes engine structures */ -/*------------------------------------------------*/ - -static deng_t * diff_engine_initialize(slist_t * l1, slist_t * l2, options_t * opt) -{ - deng_t * eng; - - if (slist_init_crefs(l1) != 0) return NULL; - if (slist_init_crefs(l2) != 0) return NULL; - - eng = (deng_t *)qalloc(sizeof(*eng)); - if (!eng) return NULL; - - eng->magic = 0x0BADF00D; - eng->wnum = 0; - - eng->identical = 0; - eng->matched = 0; - eng->unmatched = l1->num + l2->num; - - eng->opt = opt; - - return eng; -} - - -/*------------------------------------------------*/ -/* function : diff_engine_initialize */ -/* description: Initializes engine structures */ -/*------------------------------------------------*/ - -void diff_engine_free(deng_t * eng) -{ - if (eng->ilist) siglist_free(eng->ilist); - if (eng->mlist) siglist_free(eng->mlist); - if (eng->ulist) siglist_free(eng->ulist); - - qfree(eng); -} - - -/*------------------------------------------------*/ -/* function : sig_equal */ -/* description: Checks if 2 sigs are equal */ -/*------------------------------------------------*/ - -bool sig_equal(psig_t * s1, psig_t * s2, int type) -{ - if (s1->sig != s2->sig || s1->hash != s2->hash) - return false; - - if (type == DIFF_EQUAL_SIG_HASH_CRC_STR) - { - if (s1->str_hash != s2->str_hash) - return false; - } - - if (type <= DIFF_EQUAL_SIG_HASH_CRC) - { - if (s1->crc_hash != s2->crc_hash) - return false; - } - - return true; -} - - -/*------------------------------------------------*/ -/* function : sig_name_equal */ -/* description: Checks if 2 sig names are equal */ -/*------------------------------------------------*/ - -static bool sig_name_equal(psig_t * s1, psig_t * s2) -{ - if (!strncmp(s1->name, "sub_", 4) || strcmp(s1->name, s2->name)) - return false; - - return true; -} - -/*------------------------------------------------*/ -/* function : clist_equal_match */ -/* description: Checks if all the elements of a */ -/* clist match */ -/*------------------------------------------------*/ - -static bool clist_equal_match(clist_t * cl1, clist_t * cl2) -{ - dpsig_t * s1, * s2; - size_t i; - - if (!cl1 || !cl2 || cl1->nmatch == 0 || cl2->nmatch == 0) - return false; - - if (cl1->nmatch != cl2->nmatch) - return false; - - s1 = cl1->msigs; - s2 = cl2->msigs; - - for (i=0; inmatch; i++) - { - if ((sig_get_matched_type(s1->sig) == DIFF_UNMATCHED) || (s1->sig->msig->startEA != s2->sig->startEA)) - return false; - - s1 = s1->next; - s2 = s2->next; - } - - return true; -} - - -/*------------------------------------------------*/ -/* function : clist_almost_equal_match */ -/* description: Checks if at lest one element of a*/ -/* clist match */ -/*------------------------------------------------*/ - -static bool clist_almost_equal_match(clist_t * cl1, clist_t * cl2) -{ - dpsig_t * s1, * s2; - size_t i, k; - - if (!cl1 || !cl2 || cl1->nmatch == 0 || cl2->nmatch == 0) - return false; - - if (cl1->nmatch != cl2->nmatch) - return false; - - s1 = cl1->msigs; - - - for (i=0; inmatch; i++) - { - s2 = cl2->msigs; - - for (k=0; knmatch; k++) - { - if (s1->sig->msig->startEA == s2->sig->startEA) - return true; - - s2 = s2->next; - } - - s1 = s1->next; - } - - return false; -} - - -/*------------------------------------------------*/ -/* function : clist_get_unique_sig */ -/* description: Returns first unique signature in */ -/* list starting at ds */ -/* note: changes ds if ds already matched */ -/*------------------------------------------------*/ - -static dpsig_t * clist_get_unique_sig(clist_t * cl, dpsig_t ** ds, int type) -{ - dpsig_t * ptr, * tmp; - - if (!*ds) return NULL; - ptr = *ds; - - // do not keep the current signature if not unique - while (ptr) - { - if (sig_get_matched_type(ptr->sig) != DIFF_UNMATCHED) - { - if (ptr == *ds) - { - *ds = ptr->next; - if (!*ds) return NULL; - } - - tmp = ptr->next; - clist_remove(cl, ptr); - ptr = tmp; - } - else - { - if (!ptr->next) break; - - if (type == DIFF_NEQUAL_SUCC) - { - if (sig_equal(ptr->sig, (*ds)->sig, type) && sig_equal(ptr->next->sig, (*ds)->sig, type)) - return NULL; - - if (( (!sig_equal(ptr->next->sig, (*ds)->sig, type) && (!ptr->prev || !sig_equal(ptr->prev->sig, (*ds)->sig, type))) || !clist_equal_match((*ds)->sig->cs, ptr->next->sig->cs)) && ptr->sig->cs->nmatch > 0 && ptr->sig->cs->num == ptr->sig->cs->nmatch) - break; - } - else if (type == DIFF_NEQUAL_PRED) - { - if (sig_equal(ptr->sig, (*ds)->sig, type) && sig_equal(ptr->next->sig, (*ds)->sig, type)) - return NULL; - - if (( (!sig_equal(ptr->next->sig, (*ds)->sig, type) && (!ptr->prev || !sig_equal(ptr->prev->sig, (*ds)->sig, type))) || !clist_equal_match((*ds)->sig->cp, ptr->next->sig->cp)) && ptr->sig->cp->nmatch > 0 && ptr->sig->cp->num == ptr->sig->cp->nmatch) - break; - } - else if (type == DIFF_EQUAL_NAME) - { - if (!sig_equal(ptr->next->sig, (*ds)->sig, type) || !sig_name_equal((*ds)->sig, ptr->next->sig)) - break; - } - else if (type == DIFF_NEQUAL_STR) - { - bool b = false; - tmp = *ds; - - if (ptr->sig->str_hash != 0) - { - // slow: need to improve - while (tmp) - { - if (tmp->sig->startEA != ptr->sig->startEA && tmp->sig->str_hash == ptr->sig->str_hash) - { - b = true; - break; - } - - tmp = tmp->next; - } - - if (!b) - break; - } - } - else - { - bool b = sig_equal(ptr->next->sig, (*ds)->sig, type); - if (!b) break; - } - - ptr = ptr->next; - } - } - - return ptr; -} - -/*------------------------------------------------*/ -/* function : clist_get_best_sig */ -/* description: Returns best unique signature in */ -/* list */ -/* note: position pointer is incremented to the */ -/* next signature in the list */ -/*------------------------------------------------*/ - -static dpsig_t * clist_get_best_sig(clist_t * cl, int type) -{ - dpsig_t * best, * ptr; - - best = cl->pos; - - ptr = clist_get_unique_sig(cl, &best, type); - - // no more signature - if (!best) return NULL; - - if (ptr == best) - { - cl->pos = best->next; - return best; - } - - cl->pos = ptr; - return clist_get_best_sig(cl, type); -} - - -/*------------------------------------------------*/ -/* function : clist_get_eq_sig */ -/* description: Returns signature if sig presents */ -/* in list and unique */ -/*------------------------------------------------*/ - -static dpsig_t * clist_get_eq_sig(clist_t * cl, dpsig_t * dsig, int type) -{ - dpsig_t * ds, * ptr; - bool b2, b1 = sig_is_class(dsig->sig); - - ds = cl->sigs; - while (ds) - { - if (type == DIFF_NEQUAL_SUCC) - { - ptr = clist_get_unique_sig(cl, &ds, type); - if (!ds || !ptr) return NULL; - - b2 = sig_is_class(ptr->sig); - if (b1 ^ b2) return NULL; - - if (clist_equal_match(ptr->sig->cs, dsig->sig->cs)) - { - if (ptr->next && (ptr->next->sig->sig == ptr->sig->sig || clist_equal_match(ptr->next->sig->cs, dsig->sig->cs))) - return NULL; - - return ptr; - } - } - else if (type == DIFF_NEQUAL_PRED ) - { - ptr = clist_get_unique_sig(cl, &ds, type); - if (!ds || !ptr) return NULL; - - b2 = sig_is_class(ptr->sig); - if (b1 ^ b2) return NULL; - - if (clist_equal_match(ptr->sig->cp, dsig->sig->cp)) - { - if (ptr->next && (ptr->next->sig->sig == ptr->sig->sig || clist_equal_match(ptr->next->sig->cp, dsig->sig->cp))) - return NULL; - - return ptr; - } - } - else if (type == DIFF_EQUAL_NAME) - { - ptr = clist_get_unique_sig(cl, &ds, type); - if (!ds || !ptr) return NULL; - - if (sig_name_equal(ptr->sig, dsig->sig)) - return ptr; - } - else if (type == DIFF_NEQUAL_STR) - { - ptr = clist_get_unique_sig(cl, &ds, type); - if (!ds || !ptr) return NULL; - - if (ptr->sig->str_hash != 0 && ptr->sig->str_hash == dsig->sig->str_hash) - return ptr; - } - else - { - if (sig_equal(ds->sig, dsig->sig, type)) - { - ptr = clist_get_unique_sig(cl, &ds, type); - - if (!ds) return NULL; - - if (ptr != ds || !sig_equal(ds->sig, dsig->sig, type)) - return NULL; - - return ds; - } - else if (ds->sig->sig < dsig->sig->sig) - { - return NULL; - } - } - - ds = ds->next; - } - - return NULL; -} - - -static void clist_update_crefs(clist_t * cl, dpsig_t * ds, int type) -{ - dpsig_t * tmp, * next; - dpsig_t * tmp2, * next2; - clist_t * tcl; - - tmp = cl->sigs; - while(tmp) - { - next = tmp->next; - - if (type == SIG_SUCC) - tcl = tmp->sig->cs; - else - tcl = tmp->sig->cp; - - tmp2 = tcl->sigs; - while(tmp2) - { - next2 = tmp2->next; - - if (tmp2->sig->startEA == ds->sig->startEA) - clist_remove(tcl, tmp2); - - tmp2 = next2; - } - - tmp = next; - } -} - - -static void clist_update_and_remove(clist_t * cl, dpsig_t * ds) -{ - if (ds->removed) - return; - - clist_update_crefs(ds->sig->cp, ds, SIG_SUCC); - clist_update_crefs(ds->sig->cs, ds, SIG_PRED); - - clist_remove(cl, ds); -} - - -/*------------------------------------------------*/ -/* function : diff_run */ -/* description: Runs binary analysis */ -/*------------------------------------------------*/ - -static int diff_run(deng_t * eng, clist_t * cl1, clist_t * cl2, int min_type, int max_type, bool pclass) -{ - dpsig_t * dsig, * dsig2; - int changed = 0; - int type = min_type; - int mtype = max_type; - bool b; - - if (pclass && max_type > DIFF_EQUAL_SIG_HASH) - mtype = DIFF_EQUAL_SIG_HASH; - - do - { - clist_reset(cl1); - clist_reset(cl2); - - changed = 0; - while ( (dsig = clist_get_best_sig(cl1, type)) != NULL) - { - clist_reset(cl2); - dsig2 = clist_get_eq_sig(cl2, dsig, type); - if (dsig2) - { - sig_set_matched_sig(dsig->sig, dsig2->sig, type); - - eng->unmatched -= 2; - if (dsig->sig->hash2 == dsig2->sig->hash2 || sig_equal(dsig->sig, dsig2->sig, DIFF_EQUAL_SIG_HASH)) - eng->identical++; - else - eng->matched++; - - changed = 1; - - clist_update_and_remove(cl1, dsig); - clist_update_and_remove(cl2, dsig2); - - b = sig_is_class(dsig->sig); - - // string matching is not 100% reliable so we only match on crc/hash - if (mtype == DIFF_NEQUAL_STR) - b = true; - - diff_run(eng, sig_get_crefs(dsig->sig, SIG_PRED), sig_get_crefs(dsig2->sig, SIG_PRED), min_type, max_type, b); - diff_run(eng, sig_get_crefs(dsig->sig, SIG_SUCC), sig_get_crefs(dsig2->sig, SIG_SUCC), min_type, max_type, b); - - } - } - - if (changed == 0) - type++; - } while(type <= mtype); - - return 0; -} - - -/*------------------------------------------------*/ -/* function : generate_diff */ -/* description: Generates binary diff */ -/*------------------------------------------------*/ - -int generate_diff(deng_t ** d, slist_t * l1, slist_t * l2, char * file, bool display, options_t * opt) -{ - int ret; - clist_t * cl1, * cl2; - int un1, un2, idf, mf; - deng_t * eng; - - eng = diff_engine_initialize(l1, l2, opt); - if (eng == NULL) - return -1; - - cl1 = clist_init(l1); - cl2 = clist_init(l2); - - if (file) - ret = diff_run(eng, cl1, cl2, DIFF_EQUAL_NAME, DIFF_NEQUAL_STR, false); - else - { - ret = diff_run(eng, cl1, cl2, DIFF_EQUAL_SIG_HASH_CRC, DIFF_EQUAL_SIG_HASH, false); - ret = diff_run(eng, cl1, cl2, DIFF_NEQUAL_PRED, DIFF_NEQUAL_STR, false); - } - - if (display) - { - eng->mlist = siglist_init(eng->matched, file); - eng->ulist = siglist_init(eng->unmatched, file); - eng->ilist = siglist_init(eng->identical, file); - - un1 = un2 = idf = mf = 0; - - for (size_t i=0; inum; i++) - { - if (sig_is_class(l1->sigs[i])) - { - sig_free(l1->sigs[i]); - continue; - } - - if (sig_get_matched_type(l1->sigs[i]) == DIFF_UNMATCHED) - { - sig_set_nfile(l1->sigs[i], 1); - siglist_add(eng->ulist, l1->sigs[i]); - un1++; - } - else - { - if (l1->sigs[i]->hash2 == l1->sigs[i]->msig->hash2 || sig_equal(l1->sigs[i], l1->sigs[i]->msig, DIFF_EQUAL_SIG_HASH)) - { - siglist_add(eng->ilist, l1->sigs[i]); - idf++; - } - else - { - siglist_add(eng->mlist, l1->sigs[i]); - mf++; - } - } - } - - for (size_t i=0; inum; i++) - { - if (sig_is_class(l2->sigs[i])) - { - sig_free(l2->sigs[i]); - continue; - } - - if (sig_get_matched_type(l2->sigs[i]) == DIFF_UNMATCHED) - { - sig_set_nfile(l2->sigs[i], 2); - siglist_add(eng->ulist, l2->sigs[i]); - un2++; - } - } - - - msg("Identical functions: %d\n", idf); - msg("Matched functions: %d\n", mf); - msg("Unmatched functions 1: %d\n", un1); - msg("Unmatched functions 2: %d\n", un2); - display_results(eng); - } - - if (d) - *d = eng; - - return 0; -} - diff --git a/diff.hpp b/diff.hpp deleted file mode 100644 index 2820c0a..0000000 --- a/diff.hpp +++ /dev/null @@ -1,58 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __DIFF_H__ -#define __DIFF_H__ - -#include "sig.hpp" -#include "hash.hpp" -#include "options.hpp" - - -#define DIFF_EQUAL_NAME 0 -#define DIFF_EQUAL_SIG_HASH_CRC 1 -#define DIFF_EQUAL_SIG_HASH_CRC_STR 2 -#define DIFF_EQUAL_SIG_HASH 3 -#define DIFF_NEQUAL_PRED 4 -#define DIFF_NEQUAL_SUCC 5 -#define DIFF_NEQUAL_STR 6 -#define DIFF_MANUAL 7 - - -struct dengine -{ - int magic; - int matched; - int unmatched; - int identical; - slist_t * mlist; - slist_t * ulist; - slist_t * ilist; - options_t * opt; - int wnum; -}; - -typedef struct dengine deng_t; - - -int generate_diff(deng_t **, slist_t *, slist_t *, char *, bool, options_t *); -void diff_engine_free(deng_t *); -bool sig_equal(psig_t *, psig_t *, int); - -#endif \ No newline at end of file diff --git a/display.cpp b/display.cpp deleted file mode 100644 index 909d4a1..0000000 --- a/display.cpp +++ /dev/null @@ -1,723 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "diff.hpp" -#include "display.hpp" -#include "os.hpp" -#include "parser.hpp" -#include "pgraph.hpp" -#include "options.hpp" -#include "system.hpp" - - -static uint32 idaapi sizer_dlist(slist_t *sl) -{ - if (sl) - return sl->num; - - return 0; -} - - -static uint32 idaapi sizer_match(void *obj) -{ - deng_t * d = (deng_t *)obj; - - return sizer_dlist(d ? d->mlist : NULL); -} - - -static uint32 idaapi sizer_identical(void *obj) -{ - deng_t * d = (deng_t *)obj; - - return sizer_dlist(d ? d->ilist : NULL); -} - - -static uint32 idaapi sizer_unmatch(void *obj) -{ - deng_t * d = (deng_t *)obj; - - return sizer_dlist(d ? d->ulist : NULL); -} - - -static void idaapi close_window(void *obj) -{ - deng_t * d = (deng_t *)obj; - - d->wnum--; - if (!d->wnum) - ipc_close(); - - return; -} - - -/*------------------------------------------------*/ -/* function : ui_access_sig */ -/* description: Compensates for the zero index */ -/* indicating the header row and performs */ -/* bounds checking in debug */ -/*------------------------------------------------*/ - -static psig_t *ui_access_sig(slist_t *sl, uint32 n) -{ -#ifdef _DEBUG - if (!sl || n == 0 || n > sl->num) - error("ui attempted to access siglist out-of-bounds: %p %x\n", sl, n - 1); - else -#endif - return sl->sigs[n - 1]; -} - - -static void idaapi desc_dlist(slist_t * sl,uint32 n,char * const *arrptr) -{ - int i; - - /* header */ - if (n == 0) - { - for (i = 0; i < qnumber (header_match); i++) - qsnprintf(arrptr[i], MAXSTR, "%s", header_match[i]); - } - else - { - psig_t *sig = ui_access_sig(sl, n); - qsnprintf(arrptr[0], MAXSTR, "%u", sig->mtype); - qsnprintf(arrptr[1], MAXSTR, "%s", sig->name); - qsnprintf(arrptr[2], MAXSTR, "%s", sig->msig->name); - qsnprintf(arrptr[3], MAXSTR, "%a", sig->startEA); - qsnprintf(arrptr[4], MAXSTR, "%a", sig->msig->startEA); - qsnprintf(arrptr[5], MAXSTR, "%c", sig->id_crc ? '+' : '-'); - qsnprintf(arrptr[6], MAXSTR, "%a", sig->crc_hash); - qsnprintf(arrptr[7], MAXSTR, "%a", sig->msig->crc_hash); - } -} - - -/*------------------------------------------------*/ -/* function : desc_match */ -/* description: Fills matched list */ -/*------------------------------------------------*/ - -static void idaapi desc_match(void *obj,uint32 n,char * const *arrptr) -{ - deng_t * d = (deng_t *)obj; - - desc_dlist(d ? d->mlist : NULL, n, arrptr); -} - - -/*------------------------------------------------*/ -/* function : desc_identical */ -/* description: Fills identical list */ -/*------------------------------------------------*/ - -static void idaapi desc_identical(void *obj,uint32 n,char * const *arrptr) -{ - deng_t * d = (deng_t *)obj; - - desc_dlist(d ? d->ilist : NULL, n, arrptr); -} - - -/*------------------------------------------------*/ -/* function : desc_unmatch */ -/* description: Fills unmatched list */ -/*------------------------------------------------*/ - -static void idaapi desc_unmatch(void *obj,uint32 n,char * const *arrptr) -{ - int i; - - /* header */ - if (n == 0) - { - for (i = 0; i < qnumber (header_unmatch); i++) - qsnprintf(arrptr[i], MAXSTR, "%s", header_unmatch[i]); - } - else - { - psig_t *sig = ui_access_sig(((deng_t *)obj)->ulist, n); - qsnprintf(arrptr[0], MAXSTR, "%u", sig->nfile); - qsnprintf(arrptr[1], MAXSTR, "%s", sig->name); - qsnprintf(arrptr[2], MAXSTR, "%a", sig->startEA); - qsnprintf(arrptr[3], MAXSTR, "%.8X", sig->sig); - qsnprintf(arrptr[4], MAXSTR, "%.8X", sig->hash); - qsnprintf(arrptr[5], MAXSTR, "%.8X", sig->crc_hash); - } -} - - -static void idaapi enter_list(slist_t * sl,uint32 n) -{ - jumpto(ui_access_sig(sl, n)->startEA); - os_copy_to_clipboard(NULL); -} - - -/*------------------------------------------------*/ -/* function : enter_match */ -/* description: Jumps to code for element n in */ -/* matched list */ -/*------------------------------------------------*/ - -static void idaapi enter_match(void *obj,uint32 n) -{ - enter_list(((deng_t *)obj)->mlist, n); -} - - -/*------------------------------------------------*/ -/* function : enter_match */ -/* description: Jumps to code for element n in */ -/* identical list */ -/*------------------------------------------------*/ - -static void idaapi enter_identical(void *obj,uint32 n) -{ - enter_list(((deng_t *)obj)->ilist, n); -} - - -/*------------------------------------------------*/ -/* function : enter_match */ -/* description: Jumps to code for element n in */ -/* unmatched list */ -/*------------------------------------------------*/ - -static void idaapi enter_unmatch(void *obj,uint32 n) -{ - psig_t *sig = ui_access_sig(((deng_t *)obj)->ulist, n); - - if (sig->nfile == 1) - jumpto(sig->startEA); - else - os_copy_to_clipboard(NULL); -} - - -static uint32 idaapi graph_list(slist_t * sl,uint32 n, options_t * opt) -{ - slist_t * sl1 = NULL; - slist_t * sl2 = NULL; - - msg ("parsing second function...\n"); - sl2 = parse_second_fct(ui_access_sig(sl, n)->msig->startEA, sl->file, opt); - if (!sl2) - { - msg("Error: FCT2 parsing failed.\n"); - return 0; - } - - msg ("parsing first function...\n"); - sl1 = parse_fct(ui_access_sig(sl, n)->startEA, dto.graph.s_showpref); - if (!sl1) - { - msg("Error: FCT1 parsing failed.\n"); - siglist_free(sl2); - return 0; - } - - sl1->sigs[0]->nfile = 1; - sl2->sigs[0]->nfile = 2; - - msg ("diffing functions...\n"); - generate_diff(NULL, sl1, sl2, NULL, false, NULL); - - pgraph_display(sl1, sl2); - - msg ("done!\n"); - return 1; -} - - -/*------------------------------------------------*/ -/* function : graph_match */ -/* description: Draws graph from element n in */ -/* matched list */ -/*------------------------------------------------*/ - -static void idaapi graph_match(void *obj,uint32 n) -{ - slist_t * sl = ((deng_t *)obj)->mlist; - options_t * opt = ((deng_t *)obj)->opt; - - graph_list(sl, n, opt); - - return; -} - - -/*------------------------------------------------*/ -/* function : graph_identical */ -/* description: Draws graph from element n in */ -/* identical list */ -/*------------------------------------------------*/ - -static void idaapi graph_identical(void *obj,uint32 n) -{ - slist_t * sl = ((deng_t *)obj)->ilist; - options_t * opt = ((deng_t *)obj)->opt; - - graph_list(sl, n, opt); - - return; -} - - -/*------------------------------------------------*/ -/* function : graph_unmatch */ -/* description: Draws graph from element n in */ -/* unmatched list */ -/*------------------------------------------------*/ - -static void idaapi graph_unmatch(void *obj,uint32 n) -{ - slist_t * sl = NULL, * tmp = ((deng_t *)obj)->ulist; - - if (ui_access_sig(tmp, n)->nfile == 2) - { - msg ("parsing second function...\n"); - sl = parse_second_fct(ui_access_sig(tmp, n)->startEA, tmp->file, ((deng_t *)obj)->opt); - if (!sl) - { - msg("Error: FCT2 parsing failed.\n"); - return; - } - - sl->sigs[0]->nfile = 2; - } - else - { - msg ("parsing first function...\n"); - sl = parse_fct(ui_access_sig(tmp, n)->startEA, dto.graph.s_showpref); - if (!sl) - { - msg("Error: FCT1 parsing failed.\n"); - return; - } - - sl->sigs[0]->nfile = 1; - } - - pgraph_display_one(sl); - - msg ("done!\n"); - return; -} - - -static uint32 idaapi res_unmatch(deng_t * d,uint32 n, int type) -{ - slist_t * sl; - - if (type == 0) - sl = d->ilist; - else - sl = d->mlist; - - psig_t *sig = ui_access_sig(sl, n); - - sig->nfile = 1; - sig->msig->nfile = 2; - - siglist_add(d->ulist, sig); - siglist_add(d->ulist, sig->msig); - - sig->msig->msig = NULL; - sig->msig = NULL; - - siglist_remove(sl, n - 1); - - refresh_chooser(title_unmatch); - - return 1; -} - - -/*------------------------------------------------*/ -/* function : res_iunmatch */ -/* description: Unmatches element n from identical*/ -/* list */ -/*------------------------------------------------*/ - -static uint32 idaapi res_iunmatch(void *obj,uint32 n) -{ - return res_unmatch((deng_t *)obj, n, 0); -} - - -/*------------------------------------------------*/ -/* function : res_munmatch */ -/* description: Unmatches element n from matched */ -/* list */ -/*------------------------------------------------*/ - -static uint32 idaapi res_munmatch(void *obj,uint32 n) -{ - return res_unmatch((deng_t *)obj, n, 1); -} - - -/*------------------------------------------------*/ -/* function : propagate_match */ -/* description: Propagates new matched result if */ -/* option is set in dialog box */ -/*------------------------------------------------*/ - -void propagate_match(deng_t * eng, psig_t * s1, psig_t * s2, int options) -{ - size_t i; - deng_t * d = NULL; - slist_t * l1, * l2; - - if (options) - { - show_wait_box ("PatchDiff is in progress ..."); - - l1 = siglist_init(eng->ulist->num, eng->ulist->file); - l2 = siglist_init(eng->ulist->num, eng->ulist->file); - - for (i=0; iulist->num; i++) - if (!eng->ulist->sigs[i]->msig) - { - if (eng->ulist->sigs[i]->nfile == 1) - siglist_add(l1, eng->ulist->sigs[i]); - else - siglist_add(l2, eng->ulist->sigs[i]); - } - - generate_diff(&d, l1, l2, eng->ulist->file, false, NULL); - - siglist_partial_free(l1); - siglist_partial_free(l2); - - hide_wait_box(); - } - - i = 0; - while (iulist->num) - { - s1 = eng->ulist->sigs[i]; - s2 = s1->msig; - - if (!s2) - i++; - else - { - if (s1->nfile == 1) - { - if (sig_equal(s1, s2, DIFF_EQUAL_SIG_HASH)) - siglist_add(eng->ilist, s1); - else - siglist_add(eng->mlist, s1); - } - - siglist_remove(eng->ulist, i); - } - } -} - - -/*------------------------------------------------*/ -/* function : res_match */ -/* description: Matches 2 elements from unmatched */ -/* list */ -/*------------------------------------------------*/ - -static uint32 idaapi res_match(void *obj,uint32 n) -{ - deng_t * eng = (deng_t *)obj; - psig_t * s1, * s2; - int option; - ea_t ea = BADADDR; - size_t i; - - const char format[] = - "STARTITEM 0\n" - - "Set Match\n" - "\n\n" - - "Options :\n" - ">\n\n" - ; - - option = 1; - if (AskUsingForm_c(format, &ea, &option)) - { - s1 = ui_access_sig(eng->ulist, n); - - for (i=0; iulist->num; i++) - { - s2 = eng->ulist->sigs[i]; - - if (s2->startEA != ea || (s2->nfile == s1->nfile)) - continue; - - sig_set_matched_sig(s1, s2, DIFF_MANUAL); - propagate_match(eng, s1, s2, option); - - refresh_chooser(title_match); - refresh_chooser(title_identical); - - return 1; - } - - warning("Address '%a' is not valid.", ea); - return 0; - } - - return 1; -} - - -/*------------------------------------------------*/ -/* function : res_mtoi */ -/* description: Switches element n from matched */ -/* to identical list */ -/*------------------------------------------------*/ - -static uint32 idaapi res_mtoi(void *obj,uint32 n) -{ - deng_t * d = (deng_t *)obj; - psig_t *sig = ui_access_sig(d->mlist, n); - - sig->mtype = sig->msig->mtype = DIFF_MANUAL; - - siglist_add(d->ilist, sig); - siglist_remove(d->mlist, n - 1); - - refresh_chooser(title_identical); - - return 1; -} - - -/*------------------------------------------------*/ -/* function : res_itom */ -/* description: Switches element n from identical */ -/* to matched list */ -/*------------------------------------------------*/ - -static uint32 idaapi res_itom(void *obj,uint32 n) -{ - deng_t * d = (deng_t *)obj; - psig_t *sig = ui_access_sig(d->ilist, n); - - sig->mtype = sig->msig->mtype = DIFF_MANUAL; - - siglist_add(d->mlist, sig); - siglist_remove(d->ilist, n - 1); - - refresh_chooser(title_match); - - return 1; -} - - -/*------------------------------------------------*/ -/* function : res_flagged */ -/* description: Sets element as flagged/unflagged */ -/*------------------------------------------------*/ - -static uint32 idaapi res_flagged(void *obj,uint32 n) -{ - psig_t *sig = ui_access_sig(((deng_t *)obj)->mlist, n); - - sig->flag = !sig->flag; - - refresh_chooser(title_match); - - return 1; -} - - -static void transfer_sym(psig_t *sig) -{ - psig_t *rhs = sig->msig; - sig_set_name(sig, rhs->name); - set_name(sig->startEA, rhs->name, SN_NOCHECK | SN_NON_AUTO); -} - - -static uint32 idaapi transfer_sym_match(void *obj, uint32 n) -{ - psig_t *sig = ui_access_sig(((deng_t *)obj)->mlist, n); - - transfer_sym(sig); - - return 1; -} - - -static uint32 idaapi transfer_sym_identical(void *obj, uint32 n) -{ - psig_t *sig = ui_access_sig(((deng_t *)obj)->ilist, n); - - transfer_sym(sig); - - return 1; -} - -/*------------------------------------------------*/ -/* function : display_matched */ -/* description: Displays matched list */ -/*------------------------------------------------*/ - -static void display_matched(deng_t * eng) -{ - choose2(CH_ATTRS, - -1, -1, -1, -1, // position is determined by Windows - eng, // pass the created function list to the window - qnumber(header_match),// number of columns - widths_match, // widths of columns - sizer_match, // function that returns number of lines - desc_match, // function that generates a line - title_match, // window title - -1, // use the default icon for the window - 1, // position the cursor on the first line - NULL, // "kill" callback - NULL, // "new" callback - NULL, // "update" callback - graph_match, // "edit" callback - enter_match, // function to call when the user pressed Enter - close_window, // function to call when the window is closed - popup_match, // use default popup menu items - NULL); - - eng->wnum++; - - add_chooser_command(title_match, "Unmatch", res_munmatch, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT); - add_chooser_command(title_match, "Set as identical", res_mtoi, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT); - add_chooser_command(title_match, "Flag/unflag", res_flagged, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT); - add_chooser_command(title_match, "Import Symbol", transfer_sym_match, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT); -} - - -/*------------------------------------------------*/ -/* function : display_identical */ -/* description: Displays identical list */ -/*------------------------------------------------*/ - -static void display_identical(deng_t * eng) -{ - choose2(0, - -1, -1, -1, -1, // position is determined by Windows - eng, // pass the created function list to the window - qnumber(header_match),// number of columns - widths_match, // widths of columns - sizer_identical, // function that returns number of lines - desc_identical, // function that generates a line - title_identical, // window title - -1, // use the default icon for the window - 1, // position the cursor on the first line - NULL, // "kill" callback - NULL, // "new" callback - NULL, // "update" callback - graph_identical, // "edit" callback - enter_identical, // function to call when the user pressed Enter - close_window, // function to call when the window is closed - popup_match, // use default popup menu items - NULL); - - eng->wnum++; - - add_chooser_command(title_identical, "Unmatch", res_iunmatch, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT); - add_chooser_command(title_identical, "Set as matched", res_itom, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT); - add_chooser_command(title_identical, "Import Symbol", transfer_sym_identical, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT); -} - - -/*------------------------------------------------*/ -/* function : display_unmatched */ -/* description: Displays unmatched list */ -/*------------------------------------------------*/ - -static void display_unmatched(deng_t * eng) -{ - choose2(0, - -1, -1, -1, -1, // position is determined by Windows - eng, // pass the created function list to the window - qnumber(header_unmatch),// number of columns - widths_unmatch, // widths of columns - sizer_unmatch, // function that returns number of lines - desc_unmatch, // function that generates a line - title_unmatch, // window title - -1, // use the default icon for the window - 1, // position the cursor on the first line - NULL, // "kill" callback - NULL, // "new" callback - NULL, // "update" callback - graph_unmatch, // "edit" callback - enter_unmatch, // function to call when the user pressed Enter - close_window, // function to call when the window is closed - popup_unmatch, // use default popup menu items - NULL); // use the same icon for all lines - - eng->wnum++; - - add_chooser_command(title_unmatch, "Set match", res_match, 0, -1, CHOOSER_POPUP_MENU | CHOOSER_MENU_EDIT); -} - - -/*------------------------------------------------*/ -/* function : ui_callback */ -/* description: Catchs lists to change bg color */ -/*------------------------------------------------*/ - -int idaapi ui_callback(void * data, int event_id, va_list va) -{ - if ( event_id == ui_get_chooser_item_attrs ) - { - void * co = va_arg(va, void *); - uint32 n = va_arg(va, uint32); - chooser_item_attrs_t *attrs = va_arg(va, chooser_item_attrs_t *); - if (attrs != NULL) - { - deng_t * d = (deng_t *)co; - if (d && d->magic == 0x0BADF00D && n > 0) - if (ui_access_sig(d->mlist, n)->flag == 1) - attrs->color = 0x908070; - } - } - - return 0; -} - - -/*------------------------------------------------*/ -/* function : display_results */ -/* description: Displays diff results */ -/*------------------------------------------------*/ - -void display_results(deng_t * eng) -{ - hook_to_notification_point(HT_UI, ui_callback, NULL); - - display_matched(eng); - display_unmatched(eng); - display_identical(eng); -} diff --git a/display.hpp b/display.hpp deleted file mode 100644 index 729f4b4..0000000 --- a/display.hpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#ifndef __DISPLAY_H__ -#define __DISPLAY_H__ - -#include "diff.hpp" - - -// column widths -static const int widths_match[] = { 5, 32, 32, 16, 16, 4, 16, 16 }; -static const int widths_unmatch[] = { 5, 32, 16, 8, 8, 8}; - -// column headers -static const char *header_match[] = -{ - "Engine", - "Function 1", - "Function 2", - "Address 1", - "Address 2", - "CRC", - "CRC1", - "CRC2" -}; - -static const char *header_unmatch[] = -{ - "File", - "Function name", - "Function address", - "Sig", - "Hash", - "CRC" -}; - -static const char * popup_null = "\0\0\0\0"; - -static const char * popup_match[] = -{ - popup_null, - popup_null, - "Display Graphs", - NULL, -}; - -static const char * popup_unmatch[] = -{ - popup_null, - popup_null, - "Display Graph", - NULL, -}; - - -static const char* title_match = "Matched Functions"; -static const char* title_unmatch = "Unmatched Functions"; -static const char* title_identical = "Identical Functions"; - - -void display_results(deng_t *); - -int idaapi ui_callback(void * data, int event_id, va_list va); - -#endif diff --git a/hash.cpp b/hash.cpp deleted file mode 100644 index 0afcaf9..0000000 --- a/hash.cpp +++ /dev/null @@ -1,159 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "hash.hpp" -#include "sig.hpp" - - -/*------------------------------------------------*/ -/* function : hash_init */ -/* description: Initializes hash table to NULL */ -/*------------------------------------------------*/ - -hpsig_t * hash_init(size_t num) -{ - unsigned int i; - hpsig_t * hsig; - static unsigned int primes[] = { 67, 251, 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071, 262139, 524287, 1048573, 2097143 }; - - for (i=0; i < ((sizeof(primes) / sizeof(unsigned int)) - 1); i++) - if (primes[i] > (num/3)) break; - - hsig = (hpsig_t *)qalloc(sizeof(*hsig)); - if (!hsig) return NULL; - - hsig->max_hash = primes[i]; - hsig->table = (hsignature_t **) qalloc(hsig->max_hash * sizeof(*hsig->table)); - if (!hsig->table) - { - qfree(hsig); - return NULL; - } - - for (i = 0; i < hsig->max_hash; i++) - hsig->table[i] = NULL; - - return hsig; -} - - -/*------------------------------------------------*/ -/* function : hash_mk_ea */ -/* description: Creates hash value */ -/*------------------------------------------------*/ - -unsigned int hash_mk_ea(hpsig_t * htable, ea_t val) -{ - char * ptr; - unsigned int h = 0; - int i; - - ptr = (char *) &val; - - for (i=0; i> 6 ); - } - - h += ( h << 3); - h ^= ( h >> 11 ); - h += ( h >> 15 ); - - return h % htable->max_hash; -} - - -/*------------------------------------------------*/ -/* function : hash_add_ea */ -/* description: Adds element to the hash table */ -/*------------------------------------------------*/ - -int hash_add_ea (hpsig_t * htable, psig_t * sig) -{ - int id = hash_mk_ea(htable, sig->startEA); - hsignature_t * hsig = NULL; - - hsig = (hsignature_t *)qalloc(sizeof(*hsig)); - if (!hsig) return -1; - - hsig->sig = sig; - hsig->next = htable->table[id]; - htable->table[id] = hsig; - - return 0; -} - - -/*------------------------------------------------*/ -/* function : hash_find_ea */ -/* description: Finds element in the hash table */ -/*------------------------------------------------*/ - -psig_t * hash_find_ea (hpsig_t * htable, ea_t ea) -{ - if (ea == BADADDR) - return NULL; - - int id = hash_mk_ea(htable, ea); - hsignature_t * hsig; - - hsig = htable->table[id]; - - while (hsig != NULL) - { - if (hsig->sig->startEA == ea) - return hsig->sig; - - hsig = hsig->next; - } - - return NULL; -} - - -/*------------------------------------------------*/ -/* function : hash_free */ -/* description: Frees hash table */ -/*------------------------------------------------*/ - -void hash_free (hpsig_t * htable) -{ - unsigned int i; - hsignature_t * hsig, * tmp; - - for (i = 0; i < htable->max_hash; i++) - { - hsig = htable->table[i]; - - while (hsig != NULL) - { - tmp = hsig->next; - qfree (hsig); - - hsig = tmp; - } - } - - qfree(htable); -} - diff --git a/hash.hpp b/hash.hpp deleted file mode 100644 index 5149bd0..0000000 --- a/hash.hpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __HASH_H__ -#define __HASH_H__ - -#include "sig.hpp" - - -typedef struct hsignature * phsignature; - -struct hsignature -{ - psig_t * sig; - phsignature next; -}; - -typedef struct hsignature hsignature_t; - -struct hsig -{ - unsigned int max_hash; - hsignature_t ** table ; -}; - -typedef struct hsig hpsig_t; - - -hpsig_t * hash_init(size_t); -unsigned int hash_mk_ea(hpsig_t *, ea_t); -int hash_add_ea (hpsig_t *, psig_t *); -psig_t * hash_find_ea (hpsig_t *, ea_t); -void hash_free (hpsig_t *); - -#endif diff --git a/makefile b/makefile deleted file mode 100644 index 80613cf..0000000 --- a/makefile +++ /dev/null @@ -1,61 +0,0 @@ -PROC=patchdiff -O1=backup -O2=clist -O3=hash -O4=diff -O5=ppc -O6=x86 -O7=options -O8=parser -O9=pchart -O10=pgraph -O11=sig -O12=system -O13=display -O14=unix_fct - -__CFLAGS=-m32 -__LFLAGS=-m32 -__PIC__ = 1 - -include ../plugin.unx - -# MAKEDEP dependency list ------------------ -$(F)backup$(O) : $(I)ida.hpp sig.hpp diff.hpp options.hpp backup.hpp \ - backup.cpp -$(F)clist$(O) : $(I)ida.hpp sig.hpp hash.hpp clist.hpp clist.cpp -$(F)diff$(O) : $(I)ida.hpp $(I)idp.hpp $(I)graph.hpp $(I)kernwin.hpp \ - diff.hpp sig.hpp os.hpp clist.hpp display.hpp backup.hpp \ - options.hpp hash.hpp diff.cpp -$(F)display$(O) : $(I)ida.hpp $(I)idp.hpp $(I)graph.hpp $(I)kernwin.hpp \ - diff.hpp system.hpp os.hpp parser.hpp pgraph.hpp \ - options.hpp display.hpp display.cpp -$(F)hash$(O) : $(I)ida.hpp sig.hpp hash.hpp hash.cpp -$(F)options$(O) : $(I)ida.hpp $(I)kernwin.hpp system.hpp options.hpp \ - options.cpp -$(F)parser$(O) : $(I)ida.hpp $(I)pro.h $(I)funcs.hpp $(I)fpro.h \ - $(I)diskio.hpp $(I)kernwin.hpp parser.hpp sig.hpp \ - os.hpp pchart.hpp system.hpp display.cpp -$(F)patchdiff$(O) : $(I)ida.hpp $(I)loader.hpp $(I)kernwin.hpp \ - $(I)diskio.hpp $(I)idp.hpp $(I)auto.hpp parser.hpp sig.hpp\ - patchdiff.hpp diff.hpp backup.hpp display.hpp options.hpp \ - system.hpp patchdiff.cpp -$(F)pchart$(O) : $(I)ida.hpp $(I)pro.h $(I)funcs.hpp \ - $(I)gdl.hpp $(I)xref.hpp pchart.hpp patchdiff.hpp \ - x86.hpp pchart.cpp -$(F)pgraph$(O) : $(I)ida.hpp $(I)idp.hpp $(I)graph.hpp $(I)kernwin.hpp \ - sig.hpp diff.hpp pgraph.hpp pgraph.cpp -$(F)ppc$(O) : $(I)ida.hpp $(I)bytes.hpp $(I)kernwin.hpp patchdiff.hpp \ - ppc.hpp ppc.cpp -$(F)sig$(O) : $(I)pro.h $(I)ida.hpp $(I)xref.hpp $(I)gdl.hpp \ - $(I)bytes.hpp $(I)funcs.hpp $(I)kernwin.hpp $(I)fpro.h \ - $(I)diskio.hpp $(I)name.hpp $(I)ua.hpp $(I)demangle.hpp \ - $(I)graph.hpp sig.hpp x86.hpp ppc.hpp patchdiff.hpp \ - pchart.hpp sig.cpp -$(F)system$(O) : $(I)ida.hpp $(I)pro.h $(I)fpro.h $(I)diskio.hpp \ - $(I)kernwin.hpp sig.hpp system.hpp options.hpp os.hpp \ - system.cpp -$(F)unix_fct(O) : $(I)ida.hpp $(I)kernwin.hpp $(I)system.hpp \ - unix_fct.hpp unix_fct.cpp -$(F)x86$(O) : $(I)ida.hpp $(I)bytes.hpp $(I)kernwin.hpp patchdiff.hpp \ - x86.hpp x86.cpp diff --git a/options.cpp b/options.cpp deleted file mode 100644 index 92486a3..0000000 --- a/options.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "options.hpp" -#include "system.hpp" - - -static bool idaapi pdiff_menu_callback(void *ud) -{ - ushort option = 0, prev = 0; - options_t * opt = (options_t *)ud; - - const char format[] = - "STARTITEM 0\n" - - "PatchDiff2 options\n" - "<##Settings #>\n" - "<#Uses 'pipe' with the second IDA instance to speed up graph display#Keep second IDB open :C>\n" - "<#Saves PatchDiff2 results into the current IDB#Save results to IDB :C>>\n\n" - ; - - option |= opt->ipc ? 1 : 0; - option |= opt->save_db ? 2 : 0; - prev = opt->ipc; - - if (AskUsingForm_c(format, &option)) - { - opt->ipc = !!(option & 1); - opt->save_db = !!(option & 2); - - if (prev && !option) - ipc_close(); - } - - return true; -} - - -options_t * options_init() -{ - options_t * opt; - int ipc, db; - - opt = (options_t *)qalloc(sizeof(*opt)); - if (!opt) return NULL; - - if (system_get_pref("IPC", (void *)&ipc, SPREF_INT)) - opt->ipc = !!ipc; - else - opt->ipc = true; - - if (system_get_pref("DB", (void *)&db, SPREF_INT)) - opt->save_db = !!db; - else - opt->save_db = true; - - add_menu_item("Options/", "PatchDiff2", NULL, SETMENU_APP, pdiff_menu_callback, opt); - - return opt; -} - - -void options_close(options_t * opt) -{ - del_menu_item("Options/PatchDiff2"); - if (opt) qfree(opt); -} - - -bool options_use_ipc(options_t * opt) -{ - return opt->ipc; -} - - -bool options_save_db(options_t * opt) -{ - return opt->save_db; -} diff --git a/options.hpp b/options.hpp deleted file mode 100644 index 35ec734..0000000 --- a/options.hpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __OPTIONS_H__ -#define __OPTIONS_H__ - - -struct p_options -{ - bool ipc; // inter process communication - bool save_db; -}; - -typedef struct p_options options_t; - - -options_t * options_init(); -bool options_use_ipc(options_t *); -bool options_save_db(options_t *); -void options_close(options_t *); - -#endif \ No newline at end of file diff --git a/os.hpp b/os.hpp deleted file mode 100644 index 0462a2e..0000000 --- a/os.hpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#ifndef __OS_H__ -#define __OS_H__ - -#ifdef _WINDOWS -#include "win_fct.hpp" -#else -#include "unix_fct.hpp" -#endif - -#endif diff --git a/parser.cpp b/parser.cpp deleted file mode 100644 index 30e72a6..0000000 --- a/parser.cpp +++ /dev/null @@ -1,164 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "parser.hpp" -#include "sig.hpp" -#include "os.hpp" -#include "pchart.hpp" -#include "system.hpp" - -/*------------------------------------------------*/ -/* function : parse_idb */ -/* description: generates a list of signatures for*/ -/* the current idb */ -/*------------------------------------------------*/ - -slist_t * parse_idb() -{ - slist_t * sl; - psig_t * sig; - size_t fct_num, i; - qvector class_l; - - fct_num = get_func_qty(); - - sl = siglist_init(fct_num, NULL); - if (!sl) return NULL; - - for (i=0; isig == 0 || sig->lines <= 1) - sig_free(sig); - else - siglist_add(sl, sig); - } - } - - if (!siglist_realloc(sl, class_l.size())) - { - siglist_free(sl); - return NULL; - } - - for (i=0; inproper, NULL); - if (!sl) return NULL; - - for (i=0; inproper; i++) - { - memset(opcodes, '\0', sizeof(opcodes)); - sig = sig_init(); - if (!sig) - { - siglist_free(sl); - delete fchart; - return NULL; - } - - sig_set_start(sig, fchart->blocks[i].startEA); - sig_set_name(sig, buf); - - for (k=0; knsucc(i); k++) - sig_add_sref(sig, fchart->blocks[i].succ[k].ea, fchart->blocks[i].succ[k].type, CHECK_REF); - - sig_add_block(sig, opcodes, fchart->blocks[i].startEA, fchart->blocks[i].endEA, 1, options); - - sig_calc_sighash(sig, opcodes, 1); - - siglist_add(sl, sig); - } - - siglist_sort(sl); - delete fchart; - - return sl; -} - - -/*------------------------------------------------*/ -/* function : parse_second_idb */ -/* description: generates a list of signatures for*/ -/* another idb */ -/*------------------------------------------------*/ - -slist_t * parse_second_idb(char ** file, options_t * opt) -{ - char ext[10]; - - qsnprintf(ext, sizeof(ext), ".%s", IDB_EXT); - - *file = askfile_c(0, ext, "IDA Database"); - if (!*file) return NULL; - - return system_parse_idb(BADADDR, *file, opt); -} - - -/*------------------------------------------------*/ -/* function : parse_second_fct */ -/* description: generates a list of signatures for*/ -/* another fct */ -/*------------------------------------------------*/ - -slist_t * parse_second_fct(ea_t ea, char * file, options_t * opt) -{ - return system_parse_idb(ea, file, opt); -} diff --git a/parser.hpp b/parser.hpp deleted file mode 100644 index b46f97e..0000000 --- a/parser.hpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __PARSER_H__ -#define __PARSER_H__ - - -#include "sig.hpp" -#include "options.hpp" - -slist_t * parse_idb(); -slist_t * parse_second_idb(char **, options_t *); -slist_t * parse_fct(ea_t, char); -slist_t * parse_second_fct(ea_t, char *, options_t *); - -#endif \ No newline at end of file diff --git a/patchdiff.cpp b/patchdiff.cpp deleted file mode 100644 index 0a8f222..0000000 --- a/patchdiff.cpp +++ /dev/null @@ -1,219 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "sig.hpp" -#include "parser.hpp" -#include "patchdiff.hpp" -#include "diff.hpp" -#include "backup.hpp" -#include "display.hpp" -#include "options.hpp" -#include "system.hpp" - -extern plugin_t PLUGIN; -extern char *exename; - -deng_t * d_engine; -cpu_t patchdiff_cpu; -options_t * d_opt; - - -static int idaapi init(void) -{ - if (!strcmp(inf.procName, "metapc")) - { - if (inf.is_64bit()) - patchdiff_cpu = CPU_X8664; - else - patchdiff_cpu = CPU_X8632; - } - else if (!strcmp(inf.procName, "PPC")) - { - patchdiff_cpu = CPU_PPC; - } - else - patchdiff_cpu = CPU_DEFAULT; - - d_engine = NULL; - - // handle IPC - ipc_init(NULL, 0, 0); - - d_opt = options_init(); - if (!d_opt) - return PLUGIN_SKIP; - - return PLUGIN_OK; -} - -static void idaapi term(void) -{ - if (d_engine) - { - if (options_save_db(d_opt)) - backup_save_results(d_engine); - diff_engine_free(d_engine); - unhook_from_notification_point(HT_UI, ui_callback); - } - - ipc_close(); - options_close(d_opt); -} - - -static void run_first_instance() -{ - char * file; - slist_t * sl1 = NULL; - slist_t * sl2 = NULL; - int ret; - - msg ("\n---------------------------------------------------\n" - "PatchDiff Plugin v2.0.10\n" - "Copyright (c) 2010-2011, Nicolas Pouvesle\n" - "Copyright (C) 2007-2009, Tenable Network Security, Inc\n" - "---------------------------------------------------\n\n"); - - ret = backup_load_results(&d_engine, d_opt); - if (ret == 1) - { - display_results(d_engine); - return; - } - else if (ret == -1) - { - return; - } - - show_wait_box ("PatchDiff is in progress ..."); - - msg ("Scanning for functions ...\n"); - - msg ("parsing second idb...\n"); - sl2 = parse_second_idb(&file, d_opt); - if (!sl2) - { - msg("Error: IDB2 parsing cancelled or failed.\n"); - hide_wait_box(); - return; - } - - msg ("parsing first idb...\n"); - sl1 = parse_idb (); - if (!sl1) - { - msg("Error: IDB1 parsing failed.\n"); - siglist_free(sl2); - hide_wait_box(); - return; - } - - msg ("diffing...\n"); - generate_diff(&d_engine, sl1, sl2, file, true, d_opt); - - msg ("done!\n"); - hide_wait_box(); - - if (sl1) siglist_partial_free(sl1); - if (sl2) siglist_partial_free(sl2); -} - - -static void run_second_instance(const char * options) -{ - slist_t * sl; - char file[QMAXPATH]; - ea_t ea = BADADDR; - unsigned char opt = 0; - long id; - unsigned int v; - bool cont; - char tmp[QMAXPATH*4]; - - qsscanf(options, "%u:%a:%u:%s", &id, &ea, &v, file); - opt = (unsigned char)v; - - if (id) - { - if (ipc_init(file, 2, id)) - { - do - { - cont = ipc_recv_cmd(tmp, sizeof(tmp)); - if (cont) - { - run_second_instance(tmp); - ipc_recv_cmd_end(); - } - - }while(cont); - } - } - else - { - if (ea == BADADDR) - { - sl = parse_idb (); - } - else - sl = parse_fct(ea, opt); - - if (!sl) return; - - siglist_save(sl, file); - - siglist_free(sl); - } -} - - -static void idaapi run(int arg) -{ - const char * options = NULL; - - autoWait(); - - options = get_plugin_options("patchdiff2"); - - if (options == NULL) - run_first_instance(); - else - run_second_instance(options); -} - - -char comment[] = "w00t"; -char help[] = "A Binary Difference Analysis plugin module\n"; -char wanted_name[] = "PatchDiff2"; -char wanted_hotkey[] = "Ctrl-8"; - -plugin_t PLUGIN = -{ - IDP_INTERFACE_VERSION, - PLUGIN_MOD | PLUGIN_FIX, - init, - term, - run, - comment, - help, - wanted_name, - wanted_hotkey -}; diff --git a/patchdiff.hpp b/patchdiff.hpp deleted file mode 100644 index 57f11b9..0000000 --- a/patchdiff.hpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __PATCHDIFF_H__ -#define __PATCHDIFF_H__ - - -enum pcpu_type -{ - CPU_DEFAULT, - CPU_X8632, - CPU_X8664, - CPU_PPC -}; - -typedef enum pcpu_type cpu_t; - - -#endif \ No newline at end of file diff --git a/pchart.cpp b/pchart.cpp deleted file mode 100644 index be2d6a0..0000000 --- a/pchart.cpp +++ /dev/null @@ -1,272 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "pchart.hpp" -#include "patchdiff.hpp" -#include "x86.hpp" - -using namespace std; - -extern cpu_t patchdiff_cpu; - - -ea_t get_fake_jump(ea_t ea) -{ - switch(patchdiff_cpu) - { - case CPU_X8632: - case CPU_X8664: - return x86_get_fake_jump(ea); - default: - return BADADDR; - } -} - - -bool is_end_block(ea_t ea) -{ - switch(patchdiff_cpu) - { - case CPU_X8632: - case CPU_X8664: - return x86_is_end_block(ea); - default: - return false; - } -} - -ea_t get_direct_jump(ea_t ea) -{ - xrefblk_t xb; - cref_t cr; - flags_t f = getFlags(ea); - bool b = xb.first_from(ea, XREF_FAR); - if (!b) return BADADDR; - - cr = (cref_t)xb.type; - if (!xb.iscode || !(cr == fl_JF || cr == fl_JN || cr == fl_F) || (f & FF_JUMP)) return BADADDR; - - switch(patchdiff_cpu) - { - case CPU_X8632: - case CPU_X8664: - if (x86_is_direct_jump(ea)) return xb.to; - default: - return BADADDR; - } -} - - -bool pflow_chart_t::getJump(func_t * fct, qvector & list, pbasic_block_t & bl) -{ - xrefblk_t xb; - cref_t cr; - bool b, j, flow; - qvector tmp; - qvector::iterator pos; - ea_t tea, ea = bl.endEA, end, jaddr; - flags_t f; - size_t k; - int type = 0; - int cond; - - j = flow = false; - - end = get_item_end(ea); - - b = xb.first_from(ea, XREF_ALL); - f = getFlags(ea); - cond = x86_is_cond_jump_pos(ea); - - while (b) - { - cr = (cref_t)xb.type; - if (xb.iscode && (cr == fl_JF || cr == fl_JN || cr == fl_F)) - { - pedge_t ed; - - if (cr == fl_JF || cr == fl_JN) - { - j = true; - type = 1; - } - else if (! (f & FF_JUMP)) - { - flow = true; - type = 2; - } - - if (patchdiff_cpu == CPU_X8632 || patchdiff_cpu == CPU_X8664 || get_func_chunknum(fct, xb.to) >= 0) - { - jaddr = get_direct_jump(xb.to); - if (jaddr == BADADDR) - ed.ea = xb.to; - else - ed.ea = jaddr; - - ed.type = type; - - pos = tmp.end(); - - if (patchdiff_cpu == CPU_X8632 || patchdiff_cpu == CPU_X8664) - { - if ( (cond == 1 && cr == fl_F) || (cond == 2 && cr != fl_F) ) - pos = tmp.begin(); - } - else if (ed.ea == end) - pos = tmp.begin(); - - assert(ed.type != 0); - - tmp.insert(pos, ed); - } - } - - b = xb.next_from(); - } - - tea = get_fake_jump(ea); - if (tea != BADADDR) - { - pedge_t ed; - - j = true; - ed.ea = tea; - ed.type = 1; - } - - if (j) - { - for (k=0; k::iterator it; - - for (it=blocks.begin(); itstartEA == ea) - return true; - - if (ea > it->startEA && ea < it->endEA) - { - pbasic_block_t bl; - pedge_t ed; - - bl.startEA = ea; - bl.endEA = it->endEA; - bl.succ = it->succ; - - it->endEA = ea; - it->succ.clear(); - - ed.ea = ea; - ed.type = 3; - it->succ.push_back(ed); - - blocks.push_back(bl); - - return true; - } - } - - - return false; -} - - -pflow_chart_t::pflow_chart_t(func_t * fct) -{ - ea_t ea; - qvector to_trace; - bool cont; - flags_t f; - - to_trace.push_back(fct->startEA); - - while (!to_trace.empty()) - { - ea = to_trace.front(); - to_trace.erase(to_trace.begin()); - - if (check_address(ea)) - continue; - - pbasic_block_t bl; - - bl.startEA = ea; - bl.endEA = ea; - cont = true; - - while(cont) - { - ea = bl.endEA; - f = getFlags(ea); - - if ( (!isFlow(f) && (ea != bl.startEA)) || !isCode(f) ) - break; - - if ( check_address(ea) ) - { - pedge_t ed; - - ed.ea = ea; - ed.type = 3; - - bl.succ.push_back(ed); - break; - } - - if ( getJump(fct, to_trace, bl) ) - cont = false; - - if ( is_end_block(ea) ) - break; - - bl.endEA = get_item_end(ea); - } - - blocks.push_back(bl); - } - - nproper = blocks.size(); -} diff --git a/pchart.hpp b/pchart.hpp deleted file mode 100644 index 55982c0..0000000 --- a/pchart.hpp +++ /dev/null @@ -1,79 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __PCHART_H__ -#define __PCHART_H__ - -#include "precomp.hpp" - -struct pedge_t -{ - ea_t ea; - int type; -}; - -struct pbasic_block_t : public area_t -{ - qvector succ; // list of node successors - qvector pred; // list of node predecessors -}; - -class pflow_chart_t -{ -private: - bool check_address(ea_t ea); - bool getJump(func_t * fct, qvector & list, pbasic_block_t & bl); - -public: - typedef qvector blocks_t; - blocks_t blocks; - int nproper; - - idaapi pflow_chart_t(func_t *_pfn); - - int idaapi nsucc(int node) const { return int(blocks[node].succ.size()); } - int idaapi succ(int node, int i) const - { - int k; - - for (k=0; k. -*/ - - -#include "precomp.hpp" - -#include "pgraph.hpp" -#include "sig.hpp" -#include "diff.hpp" - - -static int find_node(slist_t * sl, ea_t ea) -{ - size_t i; - - for(i=0; inum; i++) - if (sl->sigs[i]->startEA == ea) - return i; - - return -1; -} - - -/*------------------------------------------------*/ -/* function : menu_callback */ -/* description: Menu callback */ -/*------------------------------------------------*/ - -static bool idaapi menu_callback(void *ud) -{ - int node; - slist_t * sl = (slist_t *)ud; - - if (sl && sl->sigs && sl->sigs[0]->nfile == 1) - { - node = viewer_get_curnode(sl->gv); - if (node >= 0) - jumpto(sl->sigs[node]->startEA); - } - - return true; -} - - -/*------------------------------------------------*/ -/* function : graph_callback */ -/* description: Graph callback */ -/*------------------------------------------------*/ - -static int idaapi graph_callback(void * ud, int code, va_list va) -{ - int result = 0; - - switch ( code ) - { - case grcode_layout_calculated: - { - mutable_graph_t *g = va_arg(va, mutable_graph_t *); - slist_t * sl = (slist_t *)ud; - - if (sl->num != g->size()) - { - warning("Graph layout is too complex to be displayed.\n"); - g->reset(); - } - } - break; - - case grcode_changed_current: - { - graph_viewer_t *v = va_arg (va, graph_viewer_t *); - int node = va_argi(va, int); - - if (node != -1) - { - slist_t * sl = (slist_t *)ud; - sl->dclk = true; - } - } - break; - - case grcode_clicked: - { - slist_t * sl = (slist_t *)ud; - sl->dclk = false; - } - break; - - case grcode_dblclicked: - { - graph_viewer_t *v = va_arg(va, graph_viewer_t *); - selection_item_t *s = va_arg(va, selection_item_t *); - slist_t * sl = (slist_t *)ud; - - if ( s && s->is_node) - { - viewer_center_on(v, s->node); - - if (sl->sigs[s->node]->msig != NULL && sl->msl->gv != NULL) - viewer_center_on(sl->msl->gv, find_node(sl->msl, sl->sigs[s->node]->matchedEA)); - - sl->dclk = false; - } - else if ( sl->dclk || s ) - { - int node; - - node = viewer_get_curnode(v); - - if (sl->sigs[node]->msig != NULL && sl->msl->gv != NULL) - viewer_center_on(sl->msl->gv, find_node(sl->msl, sl->sigs[node]->matchedEA)); - - sl->dclk = false; - } - } - break; - - case grcode_user_refresh: - { - mutable_graph_t *g = va_arg(va, mutable_graph_t *); - slist_t * sl = (slist_t *)ud; - - if ( g->empty() ) - g->resize(sl->num); - - for(size_t i = 0; i< sl->num; i++) - { - fref_t * fref; - - if (sl->sigs[i]->srefs) - { - fref = sl->sigs[i]->srefs->list; - while(fref) - { - int pos = find_node(sl, fref->ea); - - if (pos != -1) - { - edge_info_t ed; - if (fref->type == 3) - ed.color = 0xff0000; - else if (fref->type == 2) - ed.color = 0x0000ff; - else - ed.color = 0x006400; - - g->add_edge(i, pos, &ed); - } - - fref = fref->next; - } - } - } - - result = 1; - } - break; - - case grcode_user_text: - { - mutable_graph_t *g = va_arg(va, mutable_graph_t *); - int node = va_arg(va, int); - const char **text = va_arg(va, const char **); - bgcolor_t *bgcolor = va_arg(va, bgcolor_t *); - - slist_t * sl = (slist_t *)ud; - - *text = sl->sigs[node]->dl.lines; - - if ( bgcolor != NULL ) - { - *bgcolor = 0xFFFFFFFF; - - if (!sl->unique) - { - if (sl->sigs[node]->mtype == DIFF_UNMATCHED) - *bgcolor = 0xcccccc; - else if (sl->sigs[node]->sig != sl->sigs[node]->msig->sig) - *bgcolor = 0x33cc; - else if (sl->sigs[node]->id_crc) - *bgcolor = 0x8cb4d2; - } - } - result = 1; - qnotused(g); - } - break; - - case grcode_destroyed: - { - slist_t * sl = (slist_t *)ud; - - sl->gv = NULL; - } - break; - } - - return result; -} - - -template -void create_form_name(char (&dst)[len], slist_t *sl, int num) -{ - qsnprintf(dst, len, "IDB%d: %s", num, sl->sigs[0]->name); -} - - -/*------------------------------------------------*/ -/* function : pgraph_create */ -/* description: Creates s function graph */ -/* returns: true if the form was newly created */ -/*------------------------------------------------*/ - -static bool pgraph_create(slist_t *sl, int num) -{ - HWND hwnd = NULL; - char form_name[512]; - char node_name[512]; - TForm *form; - bool form_is_new = true; - - create_form_name(form_name, sl, num); - qsnprintf(node_name, sizeof(node_name), "$ %s", form_name); - - form = find_tform(form_name); - - netnode id; - bool already_existed = !id.create(node_name); - if (form && already_existed) - { - form_is_new = false; - switchto_tform(form, true); - sl->gv = get_graph_viewer(form); - } - else - { - form = create_tform(form_name, &hwnd); - if (hwnd) - { - sl->gv = create_graph_viewer(form, id, graph_callback, sl, 0); - open_tform(form, FORM_TAB|FORM_MENU|FORM_QWIDGET); - if (sl->gv) - { - viewer_fit_window(sl->gv); - viewer_add_menu_item(sl->gv, "Jump to code", menu_callback, sl, NULL, 0); - } - } - } - - return form_is_new; -} - - -/*------------------------------------------------*/ -/* function : pgraph_display */ -/* description: Displays function graph */ -/*------------------------------------------------*/ - -void pgraph_display(slist_t * sl1, slist_t * sl2) -{ - graph_viewer_t *gv = NULL; - - sl1->msl = sl2; - sl2->msl = sl1; - - sl1->unique = sl2->unique = false; - - bool sl1_new_form = pgraph_create(sl1, 1); - bool sl2_new_form = pgraph_create(sl2, 2); - if ((sl1_new_form || sl2_new_form) && - sl1->gv && sl2->gv) - { - // Only perform the docking for forms that were just created - char buf[512], buf2[512]; - create_form_name(buf, sl1, 1); - create_form_name(buf2, sl2, 2); - set_dock_pos(buf2, buf, DP_RIGHT); - } -} - - -void pgraph_display_one(slist_t * sl) -{ - sl->msl = NULL; - sl->unique = true; - - pgraph_create(sl, sl->sigs[0]->nfile); -} diff --git a/pgraph.hpp b/pgraph.hpp deleted file mode 100644 index 9ff8fcf..0000000 --- a/pgraph.hpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __PGRAPH_H__ -#define __PGRAPH_H__ - -#include "sig.hpp" - -typedef struct node_list * pnode_list; - -struct node_list -{ - uval_t id; - slist_t * sl; - pnode_list next; -}; - -typedef struct node_list nlist_t; - - -struct graph_list -{ - int num; - nlist_t * sl; - -}; - -typedef struct graph_list glist_t; - - -void pgraph_display(slist_t *, slist_t *); -void pgraph_display_one(slist_t *); - -#endif \ No newline at end of file diff --git a/ppc.cpp b/ppc.cpp deleted file mode 100644 index 022fe16..0000000 --- a/ppc.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "ppc.hpp" -#include "patchdiff.hpp" - -extern cpu_t patchdiff_cpu; - - -/*------------------------------------------------*/ -/* function : ppc_is_nop */ -/* arguments: unsigned char _byte */ -/* description: detects if instruction is a nop */ -/* (mr rA, rA) */ -/*------------------------------------------------*/ - -bool ppc_is_nop (unsigned char _byte, ea_t ea) -{ - unsigned char s, a, b, rc; - unsigned short v; - unsigned long l; - - _byte = get_byte(ea) >> 2; - - // or rS, rA, rB - if (_byte == 31) - { - v = get_word(ea); - s = (v >> 5) & 0x1F; - a = v & 0x1F; - - if (s == a) - { - v = get_word(ea+2); - - b = v >> 11; - rc = v & 1; - v = (v >> 1) & 0x3FF; - - if (b == s && v == 444 && !rc) - return true; - } - } - // nop: ori 0,0,0 - else if (_byte == 24) - { - l = get_long(ea); - if (l == 0x60000000) - return true; - } - - return false; -} - - -/*------------------------------------------------*/ -/* function : ppc_remove_instr */ -/* arguments: unsigned char byte, ea_t ea */ -/* description: Returns true is the instruction */ -/* must be ignored */ -/*------------------------------------------------*/ - -bool ppc_remove_instr(unsigned char byte, ea_t ea) -{ - if (ppc_is_nop(byte, ea)) - return true; - - /* - // if not addi (li) - if (byte != 14) - return false; - - // removes li, rD, 0 (addi rD, rA, 0 with rA == 0) - b = get_byte(ea+1) & 0x1F; - if (!b) - { - s = get_word(ea+2); - if (s == 0) - return true; - } - */ - - return false; -} - -/*------------------------------------------------*/ -/* function : ppc_get_byte */ -/* arguments: unsigned char _byte */ -/* description: Returns opcode */ -/*------------------------------------------------*/ - -unsigned char ppc_get_byte(ea_t ea) -{ - unsigned char byte; - unsigned short v; - - byte = get_byte(ea) >> 2; - - if (byte == 31) - { - v = get_word(ea+2); - v = (v >> 1) & 0xFF; - if (v < 65) v += 65; - - byte = (unsigned char)v; - } - - - return byte; -} diff --git a/ppc.hpp b/ppc.hpp deleted file mode 100644 index b32cebb..0000000 --- a/ppc.hpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __PPC_H__ -#define __PPC_H__ - -#include "precomp.hpp" - -unsigned char ppc_get_byte(ea_t); -bool ppc_remove_instr(unsigned char, ea_t); - -#endif diff --git a/precomp.cpp b/precomp.cpp deleted file mode 100644 index 0c7062f..0000000 --- a/precomp.cpp +++ /dev/null @@ -1,20 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" diff --git a/precomp.hpp b/precomp.hpp deleted file mode 100644 index 6063a4f..0000000 --- a/precomp.hpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#pragma once - -#include -#include - -#define NO_OBSOLETE_FUNCS - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// The graph API spams "forcing value to bool" warnings... -#pragma warning(push) -#pragma warning(disable: 4800) -#include -#pragma warning(pop) - -static_assert(IDA_SDK_VERSION == 630, "This plugin expects IDA SDK 6.3"); diff --git a/sig.cpp b/sig.cpp deleted file mode 100644 index 92be257..0000000 --- a/sig.cpp +++ /dev/null @@ -1,1297 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "sig.hpp" -#include "x86.hpp" -#include "ppc.hpp" -#include "patchdiff.hpp" -#include "pchart.hpp" -#include "os.hpp" - -extern cpu_t patchdiff_cpu; - - -/*------------------------------------------------*/ -/* function : pget_func_name */ -/* description: Gets function name */ -/*------------------------------------------------*/ - -char * pget_func_name(ea_t ea, char * buffer, size_t blen) -{ - char * pos; - char tmp[512]; - - if (!get_func_name(ea, buffer, blen)) - return NULL; - - // make sure this is not a c++ class/struct badly defined as a function - demangle_name(tmp, blen, buffer, inf.long_demnames); - if ( (strstr(tmp, "public: static") || strstr(tmp, "private: static")) && - (!strstr(tmp, "(") || strstr(tmp, "public: static long (__stdcall")) ) - return NULL; - - demangle_name(buffer, blen, buffer, inf.short_demnames); - - // remove duplicates of the same name - pos = strstr(buffer, "Z$0"); - if (pos) - pos[0] = '\0'; - - return buffer; -} - - -/*------------------------------------------------*/ -/* function : sig_init */ -/* description: Allocates and initializes a new */ -/* function signature */ -/*------------------------------------------------*/ - -psig_t * sig_init() -{ - psig_t * sig; - - sig = (psig_t *)qalloc(sizeof(*sig)); - if (!sig) - return NULL; - - memset(sig, 0, sizeof(*sig)); - - sig->mtype = DIFF_UNMATCHED; - sig->msig = NULL; - - return sig; -} - - -/*------------------------------------------------*/ -/* function : frefs_free */ -/* description: Frees chained list */ -/*------------------------------------------------*/ - -void frefs_free(frefs_t * frefs) -{ - fref_t * fref, * next; - - fref = frefs->list; - while (fref) - { - next = fref->next; - qfree(fref); - fref = next; - } - - qfree(frefs); -} - - -/*------------------------------------------------*/ -/* function : dsig_free */ -/* description: Frees chained list */ -/*------------------------------------------------*/ - -void dsig_free(dpsig_t * ds) -{ - dpsig_t * next; - - while (ds) - { - next = ds->next; - qfree(ds); - ds = next; - } - - qfree(ds); -} - - -/*------------------------------------------------*/ -/* function : clist_free */ -/* description: Frees clist_t structure */ -/*------------------------------------------------*/ - -void clist_free(clist_t * cl) -{ - dsig_free(cl->sigs); - dsig_free(cl->msigs); - - qfree(cl); -} - - -/*------------------------------------------------*/ -/* function : sig_free */ -/* description: Frees signature */ -/*------------------------------------------------*/ - -void sig_free(psig_t * sig) -{ - if (sig->name) - { - qfree(sig->name); - sig->name = NULL; - } - - if (sig->dl.lines != NULL) - qfree(sig->dl.lines); - - - if (sig->prefs) frefs_free(sig->prefs); - if (sig->srefs) frefs_free(sig->srefs); - if (sig->cp) clist_free(sig->cp); - if (sig->cs) clist_free(sig->cs); - - qfree(sig); -} - - -/*------------------------------------------------*/ -/* function : sig_set_name */ -/* description: Sets function signature name */ -/*------------------------------------------------*/ - -void sig_set_name(psig_t * sig, const char * name) -{ - sig->name = qstrdup(name); -} - - -/*------------------------------------------------*/ -/* function : sig_set_start */ -/* description: Sets function start address */ -/*------------------------------------------------*/ - -void sig_set_start(psig_t * sig, ea_t ea) -{ - sig->startEA = ea; -} - - -/*------------------------------------------------*/ -/* function : sig_get_start */ -/* description: Returns function start address */ -/*------------------------------------------------*/ - -ea_t sig_get_start(psig_t * sig) -{ - return sig->startEA; -} - - -/*------------------------------------------------*/ -/* function : sig_get_preds */ -/* description: Returns signature pred xrefs */ -/*------------------------------------------------*/ - -frefs_t * sig_get_preds(psig_t * sig) -{ - return sig->prefs; -} - - -/*------------------------------------------------*/ -/* function : sig_get_succs */ -/* description: Returns signature succ xrefs */ -/*------------------------------------------------*/ - -frefs_t * sig_get_succs(psig_t * sig) -{ - return sig->srefs; -} - - -/*------------------------------------------------*/ -/* function : sig_get_crefs */ -/* description: Returns signature cxrefs */ -/*------------------------------------------------*/ - -clist_t * sig_get_crefs(psig_t * sig, int type) -{ - if (type == SIG_PRED) - return sig->cp; - - if (type == SIG_SUCC) - return sig->cs; - - return NULL; -} - - -/*------------------------------------------------*/ -/* function : sig_set_crefs */ -/* description: Sets signature cxrefs */ -/*------------------------------------------------*/ - -void sig_set_crefs(psig_t * sig, int type, clist_t * cl) -{ - if (type == SIG_PRED) - sig->cp = cl; - - else if (type == SIG_SUCC) - sig->cs = cl; -} - - -/*------------------------------------------------*/ -/* function : sig_set_nfile */ -/* description: Sets file number */ -/*------------------------------------------------*/ - -void sig_set_nfile(psig_t * sig, int num) -{ - sig->nfile = num; -} - - -/*------------------------------------------------*/ -/* function : sig_set_matched_sig */ -/* description: Sets matched address */ -/*------------------------------------------------*/ - -void sig_set_matched_sig(psig_t * sig, psig_t * sig2, int type) -{ - sig->msig = sig2; - sig->matchedEA = sig2->startEA; - - sig2->msig = sig; - sig2->matchedEA = sig->startEA; - - sig->mtype = sig2->mtype = type; - - if (sig->crc_hash != sig2->crc_hash) - sig->id_crc = sig2->id_crc = 1; -} - - -/*------------------------------------------------*/ -/* function : sig_get_matched_sig */ -/* description: Returns matched address */ -/*------------------------------------------------*/ - -psig_t * sig_get_matched_sig(psig_t * sig) -{ - return sig->msig; -} - - -/*------------------------------------------------*/ -/* function : sig_get_matched_type */ -/* description: Returns matched type */ -/*------------------------------------------------*/ - -int sig_get_matched_type(psig_t * sig) -{ - return sig->mtype; -} - - -/*------------------------------------------------*/ -/* function : sig_add_fref */ -/* description: Adds a function reference to the */ -/* signature */ -/*------------------------------------------------*/ - -int sig_add_fref(frefs_t ** frefs, ea_t ea, int type, char rtype) -{ - fref_t * ref, * next; - - if (!*frefs) - { - *frefs = (frefs_t *)qalloc(sizeof(**frefs)); - if (!*frefs) return -1; - - memset(*frefs, 0, sizeof(**frefs)); - } - else - { - //don't add duplicates - next = (*frefs)->list; - while(next) - { - if (next->ea == ea) - return -1; - - next = next->next; - } - } - - ref = (fref_t *)qalloc(sizeof(*ref)); - if (!ref) return -1; - - ref->ea = ea; - ref->type = type; - ref->rtype = rtype; - ref->next = (*frefs)->list; - - (*frefs)->num++; - (*frefs)->list = ref; - - return 0; -} - - -/*------------------------------------------------*/ -/* function : sig_add_pref */ -/* description: Adds a function reference to the */ -/* signature */ -/*------------------------------------------------*/ - -int sig_add_pref(psig_t * sig, ea_t ea, int type, char rtype) -{ - return sig_add_fref(&sig->prefs, ea, type, rtype); -} - - -/*------------------------------------------------*/ -/* function : sig_add_sref */ -/* description: Adds a function reference to the */ -/* signature */ -/*------------------------------------------------*/ - -int sig_add_sref(psig_t * sig, ea_t ea, int type, char rtype) -{ - return sig_add_fref(&sig->srefs, ea, type, rtype); -} - - -/*------------------------------------------------*/ -/* function : is_fake_jump */ -/* description: Returns TRUE if the instruction at*/ -/* ea is a jump */ -/*------------------------------------------------*/ - -bool is_fake_jump(ea_t ea) -{ - switch(patchdiff_cpu) - { - case CPU_X8632: - case CPU_X8664: - if (x86_get_fake_jump(ea) != BADADDR) - return true; - default: - return false; - } -} - -/*------------------------------------------------*/ -/* function : ignore_jump */ -/* description: Returns TRUE if the instruction at*/ -/* ea is a jump that must be ignored */ -/* in the signature */ -/*------------------------------------------------*/ - -bool ignore_jump(ea_t ea) -{ - switch(patchdiff_cpu) - { - case CPU_X8632: - case CPU_X8664: - if (!x86_is_direct_jump(ea)) - return false; - default: - return true; - } -} - - -/*------------------------------------------------*/ -/* function : is_jump */ -/* description: Returns TRUE if the instruction at*/ -/* ea is a jump */ -/*------------------------------------------------*/ - -bool is_jump(psig_t * sig, ea_t ea, bool * call, bool * cj) -{ - xrefblk_t xb; - cref_t cr; - - *call = false; - *cj = false; - - if (xb.first_from(ea, XREF_FAR)) - { - cr = (cref_t)xb.type; - if (xb.iscode && (cr == fl_JF || cr == fl_JN)) - if (ignore_jump(ea)) - return true; - else - *cj = true; - - if (xb.iscode && (cr == fl_CF || cr == fl_CN)) - { - if (sig->type == 1) - sig_add_sref(sig, xb.to, 0, CHECK_REF); - - *call = true; - } - } - else - return is_fake_jump(ea); - - return false; -} - - -/*------------------------------------------------*/ -/* function : remove_instr */ -/* description: Returns TRUE if the instruction at*/ -/* ea must not be added to the sig */ -/*------------------------------------------------*/ - -bool remove_instr(unsigned char byte, ea_t ea) -{ - switch (patchdiff_cpu) - { - case CPU_X8632: - case CPU_X8664: - return x86_remove_instr(byte, ea); - case CPU_PPC: - return ppc_remove_instr(byte, ea); - default: - return false; - } -} - - -/*------------------------------------------------*/ -/* function : get_byte_with_optimization */ -/* description: Returns byte at address ea */ -/* note: Uses the processor optimized function if */ -/* available */ -/*------------------------------------------------*/ - -char get_byte_with_optimization(ea_t ea) -{ - switch (patchdiff_cpu) - { - case CPU_X8632: - case CPU_X8664: - return x86_get_byte(ea); - case CPU_PPC: - return ppc_get_byte(ea); - default: - { - decode_insn(ea); - return (char)cmd.itype; - } - } -} - - -unsigned long ror(unsigned long val, int r) -{ - return (val >> r) | (val << (32-r)); -} - - -/*------------------------------------------------*/ -/* function : dline_add */ -/* description: Adds a disassembled line to the */ -/* signature */ -/*------------------------------------------------*/ - -int dline_add(dline_t * dl, ea_t ea, char options) -{ - char buf[256]; - char tmp[256]; - char dis[256]; - char addr[30]; - char * dll; - int len; - flags_t f; - - buf[0] = '\0'; - - f = getFlags(ea); - generate_disasm_line(ea, dis, sizeof(dis)); - - decode_insn(ea); - init_output_buffer(buf, sizeof(buf)); - - // Adds block label - if (has_dummy_name(f)) - { - get_nice_colored_name(ea,tmp,sizeof(tmp),GNCN_NOSEG|GNCN_NOFUNC); - out_snprintf("%s", tmp); - out_line(":\n", COLOR_DATNAME); - } - - if (options) - { - qsnprintf(addr, sizeof(addr), "%a", ea); - out_snprintf("%s ", addr); - } - - out_insert(get_output_ptr(), dis); - term_output_buffer(); - - len = strlen(buf); - - if (dl->available < (len+3)) - { - dll = (char *)qrealloc(dl->lines, sizeof(char*) * (dl->num+len+256)); - if (!dll) return -1; - - dl->available = len+256; - dl->lines = dll; - } - - if (dl->num) - { - dl->lines[dl->num] = '\n'; - dl->num++; - } - - memcpy(&dl->lines[dl->num], buf, len); - - dl->available -= len+1; - dl->num += len; - - dl->lines[dl->num] = '\0'; - - return 0; -} - - -/*------------------------------------------------*/ -/* function : sig_add_address */ -/* description: Adds an address to the signature */ -/*------------------------------------------------*/ - -int sig_add_address(psig_t * sig, short opcodes[256], ea_t ea, bool b, bool line, char options) -{ - unsigned char byte; - unsigned char buf[200]; - uint32 s, i; - bool call; - bool cj; - ea_t tea; - flags_t f; - - if (line) - dline_add(&sig->dl, ea, options); - - if (is_jump(sig, ea, &call, &cj)) - return -1; - - byte = get_byte_with_optimization(ea); - - if (remove_instr(byte, ea)) - return -1; - - sig->lines++; - opcodes[byte]++; - - if (!b && !call) - { - if (cj) - { - buf[0] = byte; - s = 1; - } - else - { - s = (uint32)get_item_size(ea); - if (s > sizeof(buf)) s = sizeof(buf); - get_many_bytes(ea, buf, s); - } - - for (i=0; icrc_hash += buf[i]; - sig->crc_hash += ( sig->crc_hash << 10 ); - sig->crc_hash ^= ( sig->crc_hash >> 6 ); - } - } - else if (b) - { - tea = get_first_dref_from(ea); - if (tea != BADADDR) - { - f = getFlags(tea); - if (isASCII(f)) - { - opinfo_t op_info; - get_opinfo(tea, 0, f, &op_info); - s = get_max_ascii_length(tea, op_info.strtype); - if (!get_ascii_contents2(tea, s, op_info.strtype, buf, sizeof(buf))) - s = sizeof(buf); - - for (i=0; istr_hash += buf[i]*i; - } - } - } - - return 0; -} - - -/*------------------------------------------------*/ -/* function : sig_add_block */ -/* description: Adds a block to the signature */ -/*------------------------------------------------*/ - -int sig_add_block(psig_t * sig, short opcodes[256], ea_t startEA, ea_t endEA, bool line, char options) -{ - ea_t ea; - flags_t flags; - bool b; - - ea = startEA; - while (ea < endEA) - { - flags = getFlags (ea); - if (!isCode (flags)) - return -1; - - b = get_first_dref_from(ea) != BADADDR ? true : false; - sig_add_address(sig, opcodes, ea, isOff(flags, OPND_ALL) || b, line, options); - - ea += get_item_size(ea); - } - - return 0; -} - - -int OS_CDECL compare(const void *arg1, const void *arg2) -{ - return *((short *)arg1) - *((short *)arg2); -} - - -/*------------------------------------------------*/ -/* function : sig_calc_sighash */ -/* description: generates a sig/hash for the */ -/* signature opcodes */ -/*------------------------------------------------*/ - -int sig_calc_sighash(psig_t * sig, short _opcodes[256], int do_sig) -{ - short tmp; - short opcodes[256]; - int i, j; - - memcpy(opcodes, _opcodes, sizeof(opcodes)); - qsort(opcodes, 256, sizeof(short), compare); - - for (i=0; i<256; i++) - for (j=0; j<255; j++) - if (opcodes[j] > opcodes[j+1]) - { - tmp = opcodes[j+1]; - opcodes[j+1] = opcodes[j]; - opcodes[j] = tmp; - } - - sig->hash2 = 0; - if (do_sig) sig->sig = 0; - - for (i=0; i<256; i++) - { - if (do_sig) sig->sig += opcodes[i] * i; - sig->hash2 = ror(sig->hash2, 13); - sig->hash2 += _opcodes[i]; - } - - return 0; -} - - -/*------------------------------------------------*/ -/* function : sig_parse_dref_list */ -/* description: checks if the data ref is a class */ -/* like structure. Returns class ea */ -/* on success */ -/*------------------------------------------------*/ - -ea_t sig_parse_dref_list(psig_t * sig, ea_t ea) -{ - ea_t fref; - flags_t f; - - // scan up - do - { - fref = get_first_dref_from(ea); - if (fref == BADADDR) - return BADADDR; - - f = getFlags(fref); - if (!isCode(f)) - return BADADDR; - - fref = get_first_dref_to(ea); - if (fref != BADADDR) - { - f = getFlags(fref); - if (!isCode(f)) - return BADADDR; - - return ea; - } - - ea = prev_visea(ea); - } while(ea != BADADDR); - - return ea; -} - - -/*------------------------------------------------*/ -/* function : sig_is_class */ -/* description: Returns true is the signature is */ -/* a class */ -/*------------------------------------------------*/ - -bool sig_is_class(psig_t * sig) -{ - if (sig->sig == CLASS_SIG && sig->hash == CLASS_SIG && sig->crc_hash == CLASS_SIG) - return true; - - return false; -} - - -/*------------------------------------------------*/ -/* function : sig_class_generate */ -/* description: generates a signature for the */ -/* class structure */ -/*------------------------------------------------*/ - -psig_t * sig_class_generate(ea_t ea) -{ - func_t * xfct; - psig_t * sig; - ea_t fref; - char buf[512]; - - sig = sig_init(); - if (!sig) - return NULL; - - // Adds function start address - sig_set_start(sig, ea); - - // Adds function name - qsnprintf(buf, sizeof(buf), "sub_%a", ea); - sig_set_name(sig, buf); - - // Adds class references - fref = get_first_dref_to(ea); - while (fref != BADADDR) - { - xfct = get_func(fref); - if (xfct) - sig_add_sref(sig, xfct->startEA, 0, CHECK_REF); - - fref = get_next_dref_to(ea, fref); - } - - sig->hash = sig->crc_hash = sig->sig = CLASS_SIG; - - return sig; -} - - -/*------------------------------------------------*/ -/* function : sig_generate */ -/* description: generates a signature for the */ -/* given function */ -/*------------------------------------------------*/ - -psig_t * sig_generate(size_t fct_num, qvector & class_l) -{ - func_t * fct, * xfct; - pflow_chart_t * fchart; - psig_t * sig; - ea_t fref, ea; - int bnum, i; - char buf[512]; - short opcodes[256]; - qvector call_list; - flags_t f; - - fct = getn_func(fct_num); - - memset(opcodes, '\0', sizeof(opcodes)); - fchart = new pflow_chart_t(fct); - sig = sig_init(); - if (!sig) - { - delete fchart; - return NULL; - } - - sig->type = 1; - - // Adds function start address - sig_set_start(sig, fct->startEA); - - // Adds function name - if (pget_func_name(fct->startEA, buf, sizeof(buf))) - sig_set_name(sig, buf); - else return NULL; - - // Adds function references - - fref = get_first_dref_to(fct->startEA); - - while (fref != BADADDR) - { - f = getFlags(fref); - if (isCode(f)) - { - xfct = get_func(fref); - if (xfct && xfct->startEA != fct->startEA) - sig_add_pref(sig, xfct->startEA, 0, CHECK_REF); - } - else - { - ea = sig_parse_dref_list(sig, fref); - if (ea != BADADDR) - { - sig_add_pref(sig, ea, 0, CHECK_REF); - class_l.add_unique(ea); - } - } - - fref = get_next_dref_to(fct->startEA, fref); - } - - - // Adds each block to the signature - bnum = fchart->nproper; - - sig->hash = 0; - sig->sig = 0; - - - for (i=0; insucc(i); - sig->sig += (i+1) + smax*i; - - sig_add_block(sig, opcodes, fchart->blocks[i].startEA, fchart->blocks[i].endEA, 0, 0); - for(j=0; jhash = ror(sig->hash, 13); - ttype = fchart->blocks[i].succ[j].type; - if (ttype == 2) ttype--; - sig->hash += ttype; - } - } - - sig_calc_sighash(sig, opcodes, 0); - - delete fchart; - - return sig; -} - - -/*------------------------------------------------*/ -/* function : sig_save */ -/* description: Saves signature refs to disk */ -/*------------------------------------------------*/ - -void sig_save_refs(FILE * fp, frefs_t * refs) -{ - int num, i; - fref_t * tmp; - - if (refs) - { - num = refs->num; - qfwrite(fp, &num, sizeof(num)); - tmp = refs->list; - for (i=0; iea, sizeof(tmp->ea)); - qfwrite(fp, &tmp->type, sizeof(tmp->type)); - tmp = tmp->next; - } - } - else - { - num = 0; - qfwrite(fp, &num, sizeof(num)); - } -} - - -/*------------------------------------------------*/ -/* function : sig_save */ -/* description: Saves signature to disk */ -/*------------------------------------------------*/ - -int sig_save(psig_t * sig, FILE * fp) -{ - size_t len; - - // saves function name - len = strlen(sig->name); - qfwrite(fp, &len, sizeof(len)); - qfwrite(fp, sig->name, len); - - // saves function start address - qfwrite(fp, &sig->startEA, sizeof(sig->startEA)); - - // saves function lines - qfwrite(fp, &sig->dl.num, sizeof(sig->dl.num)); - qfwrite(fp, sig->dl.lines, sig->dl.num); - - // saves sig/hash - qfwrite(fp, &sig->sig, sizeof(sig->sig)); - qfwrite(fp, &sig->hash, sizeof(sig->hash)); - qfwrite(fp, &sig->hash2, sizeof(sig->hash2)); - qfwrite(fp, &sig->crc_hash, sizeof(sig->crc_hash)); - qfwrite(fp, &sig->str_hash, sizeof(sig->str_hash)); - - // saves function refs - sig_save_refs(fp, sig->prefs); - sig_save_refs(fp, sig->srefs); - - return 0; -} - - -/*------------------------------------------------*/ -/* function : sig_load_prefs */ -/* description: Loads signature refs from disk */ -/*------------------------------------------------*/ - -void sig_load_prefs(psig_t * sig, FILE * fp, int type) -{ - int num, i; - pedge_t * eatab; - - // loads function refs in reverse order - qfread(fp, &num, sizeof(num)); - eatab = (pedge_t *)qalloc(num * sizeof(*eatab)); - - for (i=0; i0; i--) - { - if (type == SIG_PRED) - sig_add_pref(sig, eatab[i-1].ea, eatab[i-1].type, CHECK_REF); - else - sig_add_sref(sig, eatab[i-1].ea, eatab[i-1].type, CHECK_REF); - } - - qfree(eatab); -} - - -/*------------------------------------------------*/ -/* function : sig_load */ -/* description: Loads signature from disk */ -/*------------------------------------------------*/ - -psig_t * sig_load(FILE * fp) -{ - size_t len; - psig_t * sig; - char buf[512]; - - sig = sig_init(); - if (!sig) return NULL; - - // loads function name - qfread(fp, &len, sizeof(len)); - qfread(fp, buf, len); - buf[len] = '\0'; - - sig_set_name(sig, buf); - - // loads function start address - qfread(fp, &sig->startEA, sizeof(sig->startEA)); - - // loads function line - qfread(fp, &sig->dl.num, sizeof(sig->dl.num)); - sig->dl.lines = (char *)qalloc((sig->dl.num+1) * sizeof(char)); - if (sig->dl.lines) - { - qfread(fp, sig->dl.lines, sig->dl.num); - sig->dl.lines[sig->dl.num] = '\0'; - } - else - sig->dl.num = 0; - - - // loads sig/hash - qfread(fp, &sig->sig, sizeof(sig->sig)); - qfread(fp, &sig->hash, sizeof(sig->hash)); - qfread(fp, &sig->hash2, sizeof(sig->hash2)); - qfread(fp, &sig->crc_hash, sizeof(sig->crc_hash)); - qfread(fp, &sig->str_hash, sizeof(sig->str_hash)); - - // loads sig refs - sig_load_prefs(sig, fp, SIG_PRED); - sig_load_prefs(sig, fp, SIG_SUCC); - - return sig; -} - - -/*------------------------------------------------*/ -/* function : siglist_init */ -/* description: Initializes a new signature list */ -/*------------------------------------------------*/ - -slist_t * siglist_init(size_t num, char * file) -{ - slist_t * l; - - l = (slist_t *)qalloc(sizeof(*l)); - if (!l) return NULL; - - l->file = file; - l->num = 0; - l->org_num = num; - l->sigs = (psig_t **)qalloc(num * sizeof(*l->sigs)); - - if (!l->sigs && l->org_num != 0) - { - qfree(l); - return NULL; - } - - return l; -} - - -/*------------------------------------------------*/ -/* function : siglist_realloc */ -/* description: Realloc a signature list */ -/*------------------------------------------------*/ - -bool siglist_realloc(slist_t * sl, size_t num) -{ - psig_t ** sigs; - - sigs = (psig_t **)qrealloc(sl->sigs, (sl->org_num + num) * sizeof(*sl->sigs)); - if (!sigs) - return false; - - sl->org_num += num; - sl->sigs = sigs; - - return true; -} - - -/*------------------------------------------------*/ -/* function : sig_compare */ -/* description: Compares two signature */ -/*------------------------------------------------*/ - -int OS_CDECL sig_compare(const void *arg1, const void *arg2) -{ - unsigned long v1, v2; - - v1 = (*(psig_t **)arg1)->sig; - v2 = (*(psig_t **)arg2)->sig; - - if (v2 > v1) return 1; - if (v2 < v1) return -1; - - v1 = (*(psig_t **)arg1)->hash; - v2 = (*(psig_t **)arg2)->hash; - - if (v2 > v1) return 1; - if (v2 < v1) return -1; - - v1 = (*(psig_t **)arg1)->crc_hash; - v2 = (*(psig_t **)arg2)->crc_hash; - - if (v2 > v1) return 1; - if (v2 < v1) return -1; - - v1 = (*(psig_t **)arg1)->str_hash; - v2 = (*(psig_t **)arg2)->str_hash; - - if (v2 > v1) return 1; - if (v2 < v1) return -1; - - return 0; -} - - -/*------------------------------------------------*/ -/* function : siglist_sort */ -/* description: Sorts the signature to the list */ -/*------------------------------------------------*/ - -void siglist_sort(slist_t * sl) -{ - qsort(sl->sigs, sl->num, sizeof(*sl->sigs), sig_compare); -} - - -/*------------------------------------------------*/ -/* function : siglist_add */ -/* description: Adds a new signature to the list */ -/*------------------------------------------------*/ - -void siglist_add(slist_t * sl, psig_t * sig) -{ - if (sl->num >= sl->org_num) - { - if (!siglist_realloc(sl, 32)) - return; - } - - sig->node = sl->num; - sl->sigs[sl->num++] = sig; -} - - -/*------------------------------------------------*/ -/* function : siglist_remove */ -/* description: Removes a new signature to the */ -/* list */ -/*------------------------------------------------*/ - -void siglist_remove(slist_t * sl, size_t n) -{ - if ( (n+1) < sl->num ) - memmove(&sl->sigs[n], &sl->sigs[n+1], ((sl->num - 1) - n) * sizeof(*(sl->sigs))); - - sl->num--; -} - - -/*------------------------------------------------*/ -/* function : siglist_free */ -/* description: Frees a new signature list */ -/*------------------------------------------------*/ - -void siglist_free(slist_t * sl) -{ - size_t i; - - for (i=0; inum; i++) - sig_free(sl->sigs[i]); - - qfree(sl->sigs); - qfree(sl); -} - - -/*------------------------------------------------*/ -/* function : siglist_partial_free */ -/* description: Frees a new signature list */ -/*------------------------------------------------*/ - -void siglist_partial_free(slist_t * sl) -{ - qfree(sl->sigs); - qfree(sl); -} - - -/*------------------------------------------------*/ -/* function : siglist_getnum */ -/* description: Returns number of signature in */ -/* list */ -/*------------------------------------------------*/ - -size_t siglist_getnum(slist_t * sl) -{ - return sl->num; -} - - -/*------------------------------------------------*/ -/* function : siglist_save */ -/* description: Saves signature list to disk */ -/*------------------------------------------------*/ - -int siglist_save(slist_t * sl, const char * filename) -{ - FILE * fp; - size_t num, i; - - fp = qfopen(filename, "wb+"); - if (fp == 0) return -1; - - num = siglist_getnum(sl); - qfwrite(fp, &num, sizeof(num)); - - for (i=0; isigs[i], fp); - - qfclose(fp); - - return 0; -} - - -/*------------------------------------------------*/ -/* function : siglist_load */ -/* description: Loads signature list from disk */ -/*------------------------------------------------*/ - -slist_t * siglist_load(const char * filename) -{ - FILE * fp; - slist_t * sl; - psig_t * sig; - size_t num, i; - - fp = qfopen(filename, "rb"); - if (fp < 0) return NULL; - - if (qfread(fp, &num, sizeof(num)) != sizeof(num)) - { - qfclose(fp); - return NULL; - } - - sl = siglist_init(num, NULL); - if (!sl) - { - qfclose(fp); - return NULL; - } - - for (i=0; i. -*/ - - -#ifndef __SIG_H__ -#define __SIG_H__ - -#include "precomp.hpp" - - -#define DIFF_UNMATCHED -1 - -#define CLASS_SIG 0xACDCACDC - -#define SIG_PRED 1 -#define SIG_SUCC 2 - -#define CHECK_REF 0 -#define DO_NOT_CHECK_REF 1 - -#ifdef _WINDOWS -#define OS_CDECL __cdecl -#else -#define OS_CDECL -#endif - -typedef struct signature psig_t; -typedef struct dc_sig * pdc_sig; - -struct dc_sig -{ - psig_t * sig; - bool removed; - pdc_sig prev; - pdc_sig next; -}; - -typedef struct dc_sig dpsig_t; - -struct c_list -{ - size_t num; - dpsig_t * pos; // position in sigs list - dpsig_t * sigs; // chained list - - size_t nmatch; // number of matched element - dpsig_t * msigs; // matched list -}; - -typedef struct c_list clist_t; - - - -typedef struct fct_ref * pfct_ref; - -struct fct_ref -{ - ea_t ea; - int type; - char rtype; - pfct_ref next; -}; - -typedef struct fct_ref fref_t; - -struct fct_refs -{ - int num; - fref_t * list; -}; - -typedef struct fct_refs frefs_t; - -struct dline -{ - int num; - int available; - char * lines; -}; - -typedef struct dline dline_t; - -typedef struct signature * psignature; - -struct signature -{ - char * name; - ea_t startEA; - ea_t matchedEA; - int mtype; - psignature msig; - int node; - int id_crc; - int nfile; - int type; - int flag; - unsigned long sig; - unsigned long hash; - unsigned long hash2; - unsigned long crc_hash; - unsigned long str_hash; - unsigned long lines; - frefs_t * prefs; - frefs_t * srefs; - clist_t * cp; - clist_t * cs; - dline_t dl; -}; - - -typedef struct sig_list * psig_list; - -struct sig_list -{ - size_t num; - size_t org_num; - char * file; - bool dclk; - graph_viewer_t *gv; - bool unique; - psig_list msl; - psig_t ** sigs; -}; - -typedef struct sig_list slist_t; - - -void siglist_free(slist_t *); -void siglist_partial_free(slist_t *); -int siglist_save(slist_t *, const char *); -slist_t * siglist_load(const char *); -slist_t * siglist_init(size_t, char *); -psig_t * sig_generate(size_t, qvector &); -psig_t * sig_class_generate(ea_t); -void clist_free(clist_t *); -void siglist_free(slist_t **); -bool siglist_realloc(slist_t *, size_t); -void siglist_add(slist_t *, psig_t *); -void siglist_remove(slist_t *, size_t); -void siglist_sort(slist_t *); -ea_t sig_get_start(psig_t * ); -void sig_set_nfile(psig_t *, int); -void sig_set_matched_sig(psig_t *, psig_t *, int); -psig_t * sig_get_matched_sig(psig_t *); -void sig_set_matched_ea(psig_t *, ea_t); -ea_t sig_get_matched_ea(psig_t *); -int sig_get_matched_type(psig_t *); -frefs_t * sig_get_preds(psig_t *); -frefs_t * sig_get_succs(psig_t *); -int sig_add_pref(psig_t *, ea_t, int, char); -int sig_add_sref(psig_t *, ea_t, int, char); -clist_t * sig_get_crefs(psig_t *, int); -void sig_set_crefs(psig_t *, int, clist_t *); -int OS_CDECL sig_compare(const void *, const void *); -psig_t * sig_init(); -int sig_add_block(psig_t *, short *, ea_t, ea_t, bool, char); -void sig_set_start(psig_t *, ea_t); -void sig_set_name(psig_t *, const char *); -int sig_calc_sighash(psig_t *, short *, int); -void sig_free(psig_t *); -bool sig_is_class(psig_t *); -char * pget_func_name(ea_t, char *, size_t); - -#endif diff --git a/system.cpp b/system.cpp deleted file mode 100644 index 8f0b133..0000000 --- a/system.cpp +++ /dev/null @@ -1,280 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "sig.hpp" -#include "system.hpp" -#include "options.hpp" -#include "os.hpp" - - -// global variable to keep IPC state -ipc_config_t ipcc; - - -/*------------------------------------------------*/ -/* function : generate_idc_file */ -/* description: generates an idc file to launch */ -/* second plugin instance in batch */ -/* mode */ -/*------------------------------------------------*/ - -static int generate_idc_file(char * file) -{ - FILE * fp; - - fp = qfopen(file, "w+"); - if (!fp) return -1; - - qfwrite(fp, PATCHDIFF_IDC, strlen(PATCHDIFF_IDC)); - qfclose(fp); - - return 0; -} - - -/*------------------------------------------------*/ -/* function : system_execute_second_instance */ -/* description: Executes another IDA instance */ -/*------------------------------------------------*/ - -static int system_execute_second_instance(char * idc, ea_t ea, char * file, bool close, long id, void * data) -{ - char path[QMAXPATH*4]; - char cmd[QMAXPATH*4]; - - if (!getsysfile(path, sizeof(path), IDA_EXEC, NULL)) - return -1; - - if (generate_idc_file(idc)) - return -1; - - qsnprintf(cmd, sizeof(cmd), "%s -A -S\"%s\" -Opatchdiff2:%u:%a:%u:\"%s\" \"%s\"", - path, - idc, - id, - ea, - dto.graph.s_showpref, - idc, - file - ); - - return os_execute_command(cmd, close, data); -} - - -/*------------------------------------------------*/ -/* function : ipc_init */ -/* description: Inits interprocess communication */ -/* between 2 IDA instances */ -/*------------------------------------------------*/ - -bool ipc_init(char * file, int type, long id) -{ - bool ret; - long pid; - char tmpname[QMAXPATH]; - - if (type == 0) - { - ipcc.init = false; - ipcc.data = NULL; - } - else if (!ipcc.init) - { - if (type == 1) - { - - pid = os_get_pid(); - ret = os_ipc_init(&ipcc.data, pid, IPC_SERVER); - if (!ret) - return false; - - os_tempnam(tmpname, sizeof(tmpname), ".idc"); - if (system_execute_second_instance(tmpname, BADADDR, file, false, pid, ipcc.data) != 0) - { - ipc_close(); - return false; - } - } - else - { - ret = os_ipc_init(&ipcc.data, id, IPC_CLIENT); - if (!ret) - return false; - } - - ipcc.init = true; - } - - return true; -} - - -/*------------------------------------------------*/ -/* function : ipc_close */ -/* description: Closes interprocess communication */ -/* between 2 IDA instances */ -/*------------------------------------------------*/ - -void ipc_close() -{ - if (!ipcc.init) return; - - os_ipc_close(ipcc.data); - - ipcc.data = NULL; - ipcc.init = false; -} - - -/*------------------------------------------------*/ -/* function : ipc_send_cmd */ -/* description: Executes command on the remote */ -/* IDA instance */ -/*------------------------------------------------*/ - -static bool ipc_send_cmd(char * cmd) -{ - idata_t d; - - d.cmd = IPC_DATA; - qstrncpy(d.data, cmd, sizeof(d.data)); - - if (!os_ipc_send(ipcc.data, IPC_SERVER, &d)) - return false; - - if (!os_ipc_recv(ipcc.data, IPC_SERVER, &d) || d.cmd != IPC_DONE) - return false; - - return true; -} - - - -/*------------------------------------------------*/ -/* function : ipc_recv_cmd */ -/* description: Receives command to execute */ -/*------------------------------------------------*/ - -bool ipc_recv_cmd(char * buf, size_t blen) -{ - bool ret; - idata_t d; - size_t len; - - memset(d.data, '\0', sizeof(d.data)); - - ret = os_ipc_recv(ipcc.data, IPC_CLIENT, &d); - if (!ret) - return false; - - if (d.cmd != IPC_DATA) - return false; - - len = strlen(d.data) + 1; - if (len > blen) - len = blen; - - memcpy(buf, d.data, len); - - return true; -} - - -/*------------------------------------------------*/ -/* function : ipc_recv_cmd_end */ -/* description: Acknowledges end of command */ -/*------------------------------------------------*/ - -bool ipc_recv_cmd_end() -{ - idata_t d; - - d.cmd = IPC_DONE; - - return os_ipc_send(ipcc.data, IPC_CLIENT, &d); -} - - -/*------------------------------------------------*/ -/* function : ipc_execute_second_instance */ -/* description: Sends command to the second IDA */ -/* instance */ -/*------------------------------------------------*/ - -static void ipc_execute_second_instance(char * idc, ea_t ea, char * file) -{ - char cmd[QMAXPATH*4]; - - if (!ipc_init(file, 1, 0)) - return; - - qsnprintf(cmd, sizeof(cmd), "%u:%a:%u:%s", - 0, - ea, - dto.graph.s_showpref, - idc - ); - - ipc_send_cmd(cmd); -} - - -/*------------------------------------------------*/ -/* function : system_parse_idb */ -/* description: generates a list of signatures for*/ -/* another idb */ -/*------------------------------------------------*/ - -slist_t * system_parse_idb(ea_t ea, char * file, options_t * opt) -{ - slist_t * sl = NULL; - char tmpname[QMAXPATH]; - - os_tempnam(tmpname, sizeof(tmpname), ".idc"); - - if (!options_use_ipc(opt)) - system_execute_second_instance(tmpname, ea, file, true, 0, NULL); - else - ipc_execute_second_instance(tmpname, ea, file); - - sl = siglist_load(tmpname); - os_unlink(tmpname); - - return sl; -} - - -/*------------------------------------------------*/ -/* function : system_get_pref */ -/* description: Gets global system preference */ -/*------------------------------------------------*/ - -bool system_get_pref(char * name, void * data, int type) -{ - if (type == SPREF_INT) - { - return os_get_pref_int(name, (int *)data); - } - - return false; -} - diff --git a/system.hpp b/system.hpp deleted file mode 100644 index 32b96bc..0000000 --- a/system.hpp +++ /dev/null @@ -1,63 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __SYSTEM_HPP__ -#define __SYSTEM_HPP__ - -#include "sig.hpp" -#include "options.hpp" - -#define PATCHDIFF_IDC "#include \n\nstatic main (void)\n{\n RunPlugin (\"patchdiff2\", 1);\n Exit(1);\n}\n" - -#define SPREF_INT 1 - -#define IPC_DATA 0 -#define IPC_DONE 1 -#define IPC_END 3 - -#define IPC_SERVER 1 -#define IPC_CLIENT 2 - - -struct ipc_config -{ - long init; - void * data; -}; - -typedef struct ipc_config ipc_config_t; - -struct idata -{ - long cmd; - char data[256]; -}; - -typedef struct idata idata_t; - - -bool system_get_pref(char *, void *, int); -slist_t * system_parse_idb(ea_t, char *, options_t *); - -bool ipc_init(char *, int, long); -void ipc_close(); -bool ipc_recv_cmd(char *, size_t); -bool ipc_recv_cmd_end(); - -#endif diff --git a/unix_fct.cpp b/unix_fct.cpp deleted file mode 100644 index e82309b..0000000 --- a/unix_fct.cpp +++ /dev/null @@ -1,307 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include -#include -#include - -#include -#include - -#include "unix_fct.hpp" -#include "system.hpp" - - -struct ipc_data -{ - int spipe; - int rpipe; - char *sname; - char *rname; - pid_t pid; -}; - -typedef struct ipc_data ipc_data_t; - - -/*------------------------------------------------*/ -/* function : create_process */ -/* description: fork/exec */ -/*------------------------------------------------*/ - -pid_t create_process(char * cmd) -{ - pid_t pid; - char * argv[4]; - - pid = fork(); - if (pid == 0) - { - argv[0] = "/bin/sh"; - argv[1] = "-c"; - argv[2] = cmd; - argv[3] = NULL; - - execvp(argv[0], argv); - exit(EXIT_FAILURE); - } - - return pid; -} - - -/*------------------------------------------------*/ -/* function : os_execute_command */ -/* description: Executes a command by creating a */ -/* new process */ -/*------------------------------------------------*/ - -int os_execute_command(char * cmd, bool close, void * data) -{ - ipc_data_t * id = (ipc_data_t *)data; - int ret = -1; - int status; - pid_t pid; - - pid = create_process(cmd); - if (pid == -1) return -1; - - if (close) - { - /* we wait until the process finished (IE: sig file is generated) */ - if (waitpid(pid, &status, 0) == -1) - return -1; - } - else - id->pid = pid; - - return 0; -} - - -/*------------------------------------------------*/ -/* function : os_check_process */ -/* description: checks process state */ -/*------------------------------------------------*/ - -bool os_check_process(pid_t pid) -{ - if (kill(pid, 0) == 0) - return true; - - return false; -} - - -/*------------------------------------------------*/ -/* function : os_copy_to_clipboard */ -/* description: Copies data to clipboard */ -/*------------------------------------------------*/ - -void os_copy_to_clipboard(char * data) -{ -} - - -/*------------------------------------------------*/ -/* function : os_get_pid */ -/* description: Returns process ID */ -/*------------------------------------------------*/ - -long os_get_pid() -{ - return (long)getpid(); -} - - -/*------------------------------------------------*/ -/* function : os_unlink */ -/* description: removes a link to a file */ -/*------------------------------------------------*/ - -int os_unlink(const char * path) -{ - return unlink(path); -} - - -/*------------------------------------------------*/ -/* function : os_tempnam */ -/* description: returns a temporary file name */ -/*------------------------------------------------*/ - -void os_tempnam(char * data, size_t size, char * suffix) -{ - char * str; - - str = tempnam(NULL, NULL); - qsnprintf(data, size, "%s%s", str, suffix); -} - - -/*------------------------------------------------*/ -/* function : os_ipc_send */ -/* description: Sends data on pipe */ -/*------------------------------------------------*/ - -bool os_ipc_send(void * data, int type, idata_t * d) -{ - ipc_data_t * id = (ipc_data_t *)data; - ssize_t num; - - num = write(id->spipe, d, sizeof(*d)); - if (num > 0) return true; - - return false; -} - - -/*------------------------------------------------*/ -/* function : os_ipc_recv */ -/* description: Receives data on pipe */ -/*------------------------------------------------*/ - -bool os_ipc_recv(void * data, int type, idata_t * d) -{ - ipc_data_t * id = (ipc_data_t *)data; - fd_set rfds, efds; - struct timeval tv; - int ret; - - tv.tv_sec = 0; - tv.tv_usec = 1000; - - if (id->pid) - { - while (1) - { - FD_ZERO(&rfds); - FD_SET(id->rpipe, &rfds); - - FD_ZERO(&efds); - FD_SET(id->rpipe, &efds); - - ret = select(id->rpipe+1, &rfds, NULL, &efds, &tv); - if (ret > 0 && FD_ISSET(id->rpipe, &rfds)) - break; - - if (ret < 0 || (ret > 0 && FD_ISSET(id->rpipe, &efds)) || !os_check_process(id->pid)) - return false; - } - } - else - { - FD_ZERO(&rfds); - FD_SET(id->rpipe, &rfds); - - FD_ZERO(&efds); - FD_SET(id->rpipe, &efds); - - ret = select(id->rpipe+1, &rfds, NULL, &efds, NULL); - if (!(ret > 0 && FD_ISSET(id->rpipe, &rfds))) - return false; - } - - read(id->rpipe, d, sizeof(*d)); - - return true; -} - - -/*------------------------------------------------*/ -/* function : os_ipc_init */ -/* description: Inits interprocess communication */ -/*------------------------------------------------*/ - -bool os_ipc_init(void ** data, long pid, int type) -{ - char sname[512]; - char rname[512]; - ipc_data_t * id; - - id = (ipc_data_t *)qalloc(sizeof(*id)); - if (!id) - return false; - - memset(id, '\0', sizeof(*id)); - - qsnprintf(sname, sizeof(sname), "/tmp/pdiff2spipe%u", pid); - qsnprintf(rname, sizeof(rname), "/tmp/pdiff2rpipe%u", pid); - - id->sname = qstrdup(sname); - id->rname = qstrdup(rname); - - if (type == IPC_SERVER) - { - mkfifo(sname, 0666); - id->spipe = open(sname, O_RDWR|O_NONBLOCK); - if (id->spipe == -1) goto error; - - mkfifo(rname, 0666); - id->rpipe = open(rname, O_RDWR|O_NONBLOCK); - if (id->rpipe == -1) goto error; - } - else - { - id->spipe = open(rname, O_WRONLY); - if (id->spipe == -1) goto error; - - id->rpipe = open(sname, O_RDONLY); - if (id->rpipe == -1) goto error; - } - - *data = (void *)id; - return true; - -error: - if (id->spipe) close(id->spipe); - if (id->rpipe) close(id->rpipe); - qfree(id); - return false; -} - - -/*------------------------------------------------*/ -/* function : os_ipc_close */ -/* description: Closes IPC */ -/*------------------------------------------------*/ - -bool os_ipc_close(void * data) -{ - ipc_data_t * id = (ipc_data_t *)data; - - if (id->spipe) close(id->spipe); - if (id->rpipe) close(id->rpipe); - - os_unlink(id->sname); - os_unlink(id->rname); - return true; -} - - -/*------------------------------------------------*/ -/* function : os_get_pref_int */ -/* description: Gets system preferences (integer) */ -/*------------------------------------------------*/ - -bool os_get_pref_int(char * name, int * i) -{ - *i = 0; - return false; -} diff --git a/unix_fct.hpp b/unix_fct.hpp deleted file mode 100644 index 397d228..0000000 --- a/unix_fct.hpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __UNIX_FCT_H__ -#define __UNIX_FCT_H__ - -#include "system.hpp" - -#ifdef __EA64__ -#define IDA_EXEC "idaq64" -#else -#define IDA_EXEC "idaq" -#endif - - -// Preference functions -bool os_get_pref_int(char *, int *); - -// System functions -int os_execute_command(char *, bool, void *); -void os_copy_to_clipboard(char *); -long os_get_pid(); -int os_unlink(const char *path); -void os_tempnam(char *, size_t, char *); - -// Shared memory functions -bool os_ipc_send(void *, int, idata_t *); -bool os_ipc_recv(void *, int, idata_t *); -bool os_ipc_init(void **, long, int); -bool os_ipc_close(void *); - -#endif diff --git a/vs/ida_64_override.props b/vs/ida_64_override.props deleted file mode 100644 index 5850d63..0000000 --- a/vs/ida_64_override.props +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - .p64 - - - - __EA64__;%(PreprocessorDefinitions) - - - $(IDASDK_DIR)lib\x86_win_vc_64 - - - - \ No newline at end of file diff --git a/vs/ida_base.props b/vs/ida_base.props deleted file mode 100644 index e783173..0000000 --- a/vs/ida_base.props +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - .plw - <_PropertySheetDisplayName>ida_base - - - - $(IDASDK_DIR)include;%(AdditionalIncludeDirectories) - __NT__;__IDP__;%(PreprocessorDefinitions) - MultiThreadedDebug - Use - precomp.hpp - - - $(IDASDK_DIR)lib\x86_win_vc_32;%(AdditionalLibraryDirectories) - ida.lib;%(AdditionalDependencies) - /EXPORT:PLUGIN %(AdditionalOptions) - - - xcopy $(TargetPath) "C:\Program Files (x86)\IDA 6.3\plugins\" /d /y - - - - \ No newline at end of file diff --git a/vs/patchdiff2.sln b/vs/patchdiff2.sln deleted file mode 100644 index da1df13..0000000 --- a/vs/patchdiff2.sln +++ /dev/null @@ -1,26 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "patchdiff2", "patchdiff2.vcxproj", "{CD825DE7-06F6-482D-82FE-3D20B1B04228}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Win32 = Debug|Win32 - Debug64|Win32 = Debug64|Win32 - Release|Win32 = Release|Win32 - Release64|Win32 = Release64|Win32 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {CD825DE7-06F6-482D-82FE-3D20B1B04228}.Debug|Win32.ActiveCfg = Debug|Win32 - {CD825DE7-06F6-482D-82FE-3D20B1B04228}.Debug|Win32.Build.0 = Debug|Win32 - {CD825DE7-06F6-482D-82FE-3D20B1B04228}.Debug64|Win32.ActiveCfg = Debug64|Win32 - {CD825DE7-06F6-482D-82FE-3D20B1B04228}.Debug64|Win32.Build.0 = Debug64|Win32 - {CD825DE7-06F6-482D-82FE-3D20B1B04228}.Release|Win32.ActiveCfg = Release|Win32 - {CD825DE7-06F6-482D-82FE-3D20B1B04228}.Release|Win32.Build.0 = Release|Win32 - {CD825DE7-06F6-482D-82FE-3D20B1B04228}.Release64|Win32.ActiveCfg = Release64|Win32 - {CD825DE7-06F6-482D-82FE-3D20B1B04228}.Release64|Win32.Build.0 = Release64|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/vs/patchdiff2.vcxproj b/vs/patchdiff2.vcxproj deleted file mode 100644 index 40acd1e..0000000 --- a/vs/patchdiff2.vcxproj +++ /dev/null @@ -1,210 +0,0 @@ - - - - - Debug64 - Win32 - - - Debug - Win32 - - - Release64 - Win32 - - - Release - Win32 - - - - {CD825DE7-06F6-482D-82FE-3D20B1B04228} - Win32Proj - patchdiff2 - - - - DynamicLibrary - true - v110 - MultiByte - - - DynamicLibrary - true - v110 - MultiByte - - - DynamicLibrary - false - v110 - true - MultiByte - - - DynamicLibrary - false - v110 - true - MultiByte - - - - - - - - - - - - - - - - - - - - - - - - - true - - - true - - - false - - - false - - - - - - Level3 - Disabled - WIN32;_DEBUG;_WINDOWS;_USRDLL;PATCHDIFF2_EXPORTS;%(PreprocessorDefinitions) - - - Windows - true - - - - - - - Level3 - Disabled - WIN32;_DEBUG;_WINDOWS;_USRDLL;PATCHDIFF2_EXPORTS;%(PreprocessorDefinitions) - - - Windows - true - - - - - Level3 - - - MaxSpeed - true - true - WIN32;NDEBUG;_WINDOWS;_USRDLL;PATCHDIFF2_EXPORTS;%(PreprocessorDefinitions) - - - Windows - true - true - true - - - - - Level3 - - - MaxSpeed - true - true - WIN32;NDEBUG;_WINDOWS;_USRDLL;PATCHDIFF2_EXPORTS;%(PreprocessorDefinitions) - - - Windows - true - true - true - - - - - - - - - - - - - - - - - - - - true - true - true - true - - - - - - - - - - - - - - - - - - Create - Create - Create - Create - - - - - true - true - true - true - - - - - - - - - - - - - - \ No newline at end of file diff --git a/vs/patchdiff2.vcxproj.filters b/vs/patchdiff2.vcxproj.filters deleted file mode 100644 index 543197b..0000000 --- a/vs/patchdiff2.vcxproj.filters +++ /dev/null @@ -1,136 +0,0 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hpp;hxx;hm;inl;inc;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - - - Resource Files - - - - - Resource Files - - - \ No newline at end of file diff --git a/win_fct.cpp b/win_fct.cpp deleted file mode 100644 index 309a40f..0000000 --- a/win_fct.cpp +++ /dev/null @@ -1,315 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -// Must be included before idp.hpp -#include -#include "precomp.hpp" - -#include "win_fct.hpp" -#include "system.hpp" - - -struct ipc_data -{ - HANDLE shared; - idata_t * memory; - HANDLE slock; - HANDLE rlock; - HANDLE process; -}; - -typedef struct ipc_data ipc_data_t; - - - -/*------------------------------------------------*/ -/* function : os_execute_command */ -/* description: Executes a command by creating a */ -/* new process */ -/*------------------------------------------------*/ - -int os_execute_command(char * cmd, bool close, void * data) -{ - STARTUPINFO si; - PROCESS_INFORMATION pi; - int ret = -1; - ipc_data_t * id = (ipc_data_t *)data; - - ZeroMemory( &si, sizeof(STARTUPINFO) ); - si.cb = sizeof(STARTUPINFO); - si.hStdError = GetStdHandle(STD_ERROR_HANDLE); - si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); - si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); - si.dwFlags |= STARTF_USESTDHANDLES; - - if (CreateProcess( - NULL, - cmd, // command line - NULL, // process security - NULL, // thread security - FALSE, // inherit handles-yes - 0, // creation flags - NULL, // environment block - NULL, // current directory - &si, // startup info - &pi)) // process info (out) - { - if (close) - { - /* we wait until the process finished (IE: sig file is generated) */ - if (WaitForSingleObject( pi.hProcess, INFINITE ) == WAIT_OBJECT_0) - ret = 0; - - CloseHandle(pi.hProcess); - } - else - { - id->process = pi.hProcess; - ret = 0; - } - - CloseHandle(pi.hThread); - } - - return ret; -} - - -/*------------------------------------------------*/ -/* function : os_check_process */ -/* description: checks process state */ -/*------------------------------------------------*/ - -static bool os_check_process(void * handle) -{ - DWORD exitcode; - - if (GetExitCodeProcess((HANDLE)handle, &exitcode) && exitcode == STILL_ACTIVE) - return true; - - return false; -} - - -/*------------------------------------------------*/ -/* function : os_copy_to_clipboard */ -/* description: Copies data to clipboard */ -/*------------------------------------------------*/ - -void os_copy_to_clipboard(char * data) -{ -} - - -/*------------------------------------------------*/ -/* function : os_get_pid */ -/* description: Returns process ID */ -/*------------------------------------------------*/ - -long os_get_pid() -{ - return (long)GetCurrentProcessId(); -} - - -/*------------------------------------------------*/ -/* function : os_unlink */ -/* description: removes a link to a file */ -/*------------------------------------------------*/ - -int os_unlink(const char * path) -{ - return _unlink(path); -} - - -/*------------------------------------------------*/ -/* function : os_tempnam */ -/* description: returns a temporary file name */ -/*------------------------------------------------*/ - -void os_tempnam(char * data, size_t size, char * suffix) -{ - char tmp[MAX_PATH]; - char name[MAX_PATH]; - - GetTempPath(sizeof(tmp), tmp); - GetTempFileName(tmp, NULL, 0, name); - qsnprintf(data, size, "%s%s", name, (suffix) ? suffix: ""); -} - - -/*------------------------------------------------*/ -/* function : os_ipc_send */ -/* description: Sends data on pipe */ -/*------------------------------------------------*/ - -bool os_ipc_send(void * data, int type, idata_t * d) -{ - ipc_data_t * id = (ipc_data_t *)data; - HANDLE lock; - - memcpy(id->memory, d, sizeof(*id->memory)); - - lock = (type == IPC_SERVER) ? id->slock : id->rlock; - - return SetEvent(lock) == TRUE; -} - - -/*------------------------------------------------*/ -/* function : os_ipc_recv */ -/* description: Receives data on pipe */ -/*------------------------------------------------*/ - -bool os_ipc_recv(void * data, int type, idata_t * d) -{ - ipc_data_t * id = (ipc_data_t *)data; - HANDLE lock; - DWORD ret; - - lock = (type == IPC_SERVER) ? id->rlock : id->slock; - - if (id->process) - { - while (1) - { - ret = WaitForSingleObject((HANDLE)lock, 1000); - - if (ret == WAIT_OBJECT_0) - break; - - if (ret != WAIT_TIMEOUT || !os_check_process(id->process)) - return false; - } - } - else - { - if (WaitForSingleObject((HANDLE)lock, INFINITE) != WAIT_OBJECT_0) - return false; - } - - memcpy(d, id->memory, sizeof(*d)); - - return true; -} - - -/*------------------------------------------------*/ -/* function : os_ipc_init */ -/* description: Inits interprocess communication */ -/*------------------------------------------------*/ - -bool os_ipc_init(void ** data, long pid, int type) -{ - char name[512]; - ipc_data_t * id; - - id = (ipc_data_t *)qalloc(sizeof(*id)); - if (!id) - return false; - - memset(id, '\0', sizeof(*id)); - - qsnprintf(name, sizeof(name), "pdiff2_slock%u", pid); - id->slock = CreateEvent(NULL, false, false, name); - if (!id->slock) - goto error; - - qsnprintf(name, sizeof(name), "pdiff2_rlock%u", pid); - id->rlock = CreateEvent(NULL, false, false, name); - if (!id->rlock) - goto error; - - qsnprintf(name, sizeof(name), "pdiff2_sharedmemory%u", pid); - if (type == IPC_SERVER) - { - id->shared = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(idata_t), name); - if (!id->shared) - goto error; - } - else - { - id->shared = OpenFileMapping(FILE_MAP_ALL_ACCESS,false,name); - if (!id->shared) - goto error; - } - - id->memory = (idata_t *)MapViewOfFile(id->shared, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(idata_t)); - if (!id->memory) - goto error; - - *data = (void *)id; - return true; - -error: - if (id->slock) CloseHandle(id->slock); - if (id->rlock) CloseHandle(id->rlock); - if (id->shared) CloseHandle(id->shared); - qfree(id); - return false; -} - - -/*------------------------------------------------*/ -/* function : os_ipc_close */ -/* description: Closes IPC */ -/*------------------------------------------------*/ - -bool os_ipc_close(void * data) -{ - ipc_data_t * id = (ipc_data_t *)data; - - if (id->slock) {SetEvent(id->slock); CloseHandle(id->slock);} - if (id->rlock) {SetEvent(id->rlock); CloseHandle(id->rlock);} - if (id->memory) UnmapViewOfFile((void *)id->memory); - if (id->shared) CloseHandle(id->shared); - if (id->process) CloseHandle(id->process); - - return true; -} - - -/*------------------------------------------------*/ -/* function : os_get_pref_int */ -/* description: Gets system preferences (integer) */ -/*------------------------------------------------*/ - -bool os_get_pref_int(char * name, int * i) -{ - HKEY key; - DWORD type; - DWORD size; - int tmp; - long ret; - - ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Tenable\\PatchDiff2", 0, KEY_READ, &key); - if (ret != ERROR_SUCCESS) - return false; - - size = sizeof(tmp); - ret = RegQueryValueEx(key, name, NULL, &type, (LPBYTE)&tmp, &size); - RegCloseKey(key); - - if (ret != ERROR_SUCCESS || type != REG_DWORD) - return false; - - *i = tmp; - return true; -} diff --git a/win_fct.hpp b/win_fct.hpp deleted file mode 100644 index 922d750..0000000 --- a/win_fct.hpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifndef __WIN_FCT_H__ -#define __WIN_FCT_H__ - -#include "system.hpp" - -#ifdef __EA64__ -#define IDA_EXEC "idaq64.exe" -#else -#define IDA_EXEC "idaq.exe" -#endif - - -// Preference functions -bool os_get_pref_int(char *, int *); - -// System functions -int os_execute_command(char *, bool, void *); -void os_copy_to_clipboard(char *); -long os_get_pid(); -int os_unlink(const char *path); -void os_tempnam(char *, size_t, char *); - -// Shared memory functions -bool os_ipc_send(void *, int, idata_t *); -bool os_ipc_recv(void *, int, idata_t *); -bool os_ipc_init(void **, long, int); -bool os_ipc_close(void *); - -#endif diff --git a/x86.cpp b/x86.cpp deleted file mode 100644 index f3e933f..0000000 --- a/x86.cpp +++ /dev/null @@ -1,499 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#include "precomp.hpp" - -#include "x86.hpp" -#include "patchdiff.hpp" - -extern cpu_t patchdiff_cpu; - - -/*------------------------------------------------*/ -/* function : x86_is_rex_prefix */ -/* arguments: unsigned char val */ -/* description: detects if instruction is a Rex */ -/* prefix */ -/*------------------------------------------------*/ - -bool x86_is_rex_prefix(unsigned char val) -{ - if (val >= 0x40 && val <= 0x4F) - return true; - - return false; -} - - -/*------------------------------------------------*/ -/* function : x86_is_push_register */ -/* arguments: unsigned char val */ -/* description: detects if instruction is push reg*/ -/*------------------------------------------------*/ - -bool x86_is_push_register (unsigned char val) -{ - switch (val) - { - case 0x50: // push eax - case 0x51: // push ecx - case 0x52: // push edx - case 0x53: // push ebx - case 0x54: // push esp - case 0x55: // push ebp - case 0x56: // push esi - case 0x57: // push edi - return 1; - - default: - return 0; - } -} - -/*------------------------------------------------*/ -/* function : x86_is_pop_register */ -/* arguments: unsigned char val */ -/* description: detects if instruction is pop reg */ -/*------------------------------------------------*/ - -bool x86_is_pop_register (unsigned char val) -{ - switch (val) - { - case 0x58: // pop eax - case 0x59: // pop ecx - case 0x5A: // pop edx - case 0x5B: // pop ebx - case 0x5C: // pop esp - case 0x5D: // pop ebp - case 0x5E: // pop esi - case 0x5F: // pop edi - return 1; - - default: - return 0; - } -} - - -/*------------------------------------------------*/ -/* function : x86_is_inc_register */ -/* arguments: unsigned char val */ -/* description: detects if instruction is inc reg */ -/*------------------------------------------------*/ - -bool x86_is_inc_register (unsigned char val) -{ - switch (val) - { - case 0x40: // inc eax - case 0x41: // inc ecx - case 0x42: // inc edx - case 0x43: // inc ebx - case 0x44: // inc esp - case 0x45: // inc ebp - case 0x46: // inc esi - case 0x47: // inc edi - return 1; - - default: - return 0; - } -} - -/*------------------------------------------------*/ -/* function : x86_is_dec_register */ -/* arguments: unsigned char val */ -/* description: detects if instruction is dec reg */ -/*------------------------------------------------*/ - -bool x86_is_dec_register (unsigned char val) -{ - switch (val) - { - case 0x48: // dec eax - case 0x49: // dec ecx - case 0x4A: // dec edx - case 0x4B: // dec ebx - case 0x4C: // dec esp - case 0x4D: // dec ebp - case 0x4E: // dec esi - case 0x4F: // dec edi - return 1; - - default: - return 0; - } -} - - - - -/*------------------------------------------------*/ -/* function : is_nop */ -/* arguments: unsigned char _byte */ -/* description: detect if instruction is nop */ -/* (nop, mov reg, reg, ...) */ -/*------------------------------------------------*/ - -bool x86_is_nop (unsigned char _byte, ea_t ea) -{ - unsigned char val; - unsigned short val2; - unsigned long val3; - - if (patchdiff_cpu == CPU_X8664 && x86_is_rex_prefix(_byte)) - { - ea++; - _byte = get_byte(ea+1); - } - - // mov reg, reg - xchg reg, reg - if (_byte == 0x8A || _byte == 0x8B || _byte == 0x87 || _byte == 0x86) - { - val = get_byte(ea+1); - if ( (val == 0xC0) || // mov eax, eax (al, al) - (val == 0xC9) || // mov ecx, ecx - (val == 0xDB) || // mov ebx, ebx - (val == 0xD2) || // mov edx, edx - (val == 0xF6) || // mov esi, esi - (val == 0xFF) || // mov edi, edi - (val == 0xE4) || // mov esp, esp - (val == 0xED) ) // mov ebp, ebp - return true; - } - if (_byte == 0x8D) - { - val = get_byte(ea+1); - if ( (val == 0x00) || // lea eax, [eax] - (val == 0x09) || // lea ecx, [ecx] - (val == 0x42) || // lea edx, [edx] - (val == 0x4b) || // lea ebx, [ebx] - (val == 0x36) || // lea esi, [esi] - (val == 0x3f) ) // lea edi, [edi] - { - return true; - } - else if ( (val == 0x40) || // lea eax, [eax+0] - (val == 0x49) || // lea ecx, [ecx+0] - (val == 0x52) || // lea edx, [edx+0] - (val == 0x5b) || // lea ebx, [ebx+0] - (val == 0x6d) || // lea ebp, [ebp+0] - (val == 0x76) || // lea esi, [esi+0] - (val == 0x7f) ) // lea edi, [edi+0] - { - val = get_byte(ea+2); - if (val == 0x00) - return true; - } - else if ( (val == 0x80) || // lea eax, [eax+0x00000000] - (val == 0x89) || // lea ecx, [ecx+0x00000000] - (val == 0x92) || // lea edx, [edx+0x00000000] - (val == 0x9b) || // lea ebx, [ebx+0x00000000] - (val == 0xad) || // lea ebp, [ebp+0x00000000] - (val == 0xB6) || // lea esi, [esi+0x00000000] - (val == 0xBf) ) // lea edi, [edi+0x00000000] - { - val3 = get_long(ea+2); - if (val3 == 0x00) - return true; - } - else if (val == 0xb4) - { - val = get_byte(ea+2); - if (val == 0x26) - { - val3 = get_long(ea+3); - if (val3 == 0x00) // lea esi, [esi+0x00000000] - return true; - } - } - else if (val == 0x24) - { - val = get_byte(ea+2); - if (val == 0x24) // lea esp, [esp] - return true; - } - else if (val == 0x64) - { - val2 = get_word(ea+2); - if (val2 == 0x24) // lea esp, [esp+0] - return true; - } - else if (val == 0xa4) - { - val = get_byte(ea+2); - if (val == 0x24) - { - val3 = get_long(ea+3); - if (val3 == 0x00) // lea esp, [esp+0x00000000] - return true; - } - } - } - - // nop - if (_byte == 0x90) - return true; - - return false; -} - - -/*------------------------------------------------*/ -/* function : x86_remove_instr */ -/* arguments: unsigned char byte, ea_t ea */ -/* description: Returns true is the instruction */ -/* must be ignored */ -/*------------------------------------------------*/ - -bool x86_remove_instr(unsigned char byte, ea_t ea) -{ - // removes nop - if (x86_is_nop(byte, ea)) - return true; - - return false; -} - - -/*------------------------------------------------*/ -/* function : x86_convert_16bit_rep */ -/* arguments: unsigned char val, instr address */ -/* description: converts byte if instruction is a */ -/* 16 bit rep/repe/repz/repne/repnz */ -/* notes: detects changes like 66 F3 -> F3 66 */ -/*------------------------------------------------*/ - -bool x86_convert_16bit_rep(unsigned char * byte, ea_t ea) -{ - unsigned char byte2; - - if (*byte == 0x66) - { - byte2 = get_byte(ea+1); - if (byte2 == 0xF3 || byte2 == 0xF2) - { - *byte = byte2; - return true; - } - } - - return false; -} - - -/*------------------------------------------------*/ -/* function : x86_convert_cond_jump */ -/* arguments: unsigned char val, instr address */ -/* description: converts byte if instruction is a */ -/* conditionnal jump. If jnz returns */ -/* jz, if jne return je, ... */ -/*------------------------------------------------*/ - -bool x86_convert_cond_jump(unsigned char * byte, ea_t ea) -{ - unsigned char byte2 = *byte; - - if (byte2 == 0x0F) - { - byte2 = get_byte(ea+1) - 0x10; - } - - if (byte2 >= 0x70 && byte2 <= 0x7F) - { - switch (byte2) - { - case 0x77: // ja -> jb - *byte = 0x72; - break; - case 0x73: // jae -> jbe - *byte = 0x76; - break; - case 0x75: // jnz -> jz - *byte = 0x74; - break; - case 0x7F: // jg -> jl - *byte = 0x7C; - break; - case 0x7D: // jge -> jle - *byte = 0x7E; - break; - case 0x71: // jno -> jo - *byte = 0x70; - break; - case 0x7B: // jnp -> jp - *byte = 0x7A; - break; - case 0x79: // jns -> js - *byte = 0x78; - break; - default: - *byte = byte2; - } - - return true; - } - - return false; -} - - -/*------------------------------------------------*/ -/* function : x86_convert_cond_jump */ -/* arguments: unsigned char val, instr address */ -/* description: converts byte if instruction is a */ -/* conditionnal jump. If jnz returns */ -/* jz, if jne return je, ... */ -/*------------------------------------------------*/ - -int x86_is_cond_jump_pos(ea_t ea) -{ - unsigned char byte2 = get_byte(ea); - - if (byte2 == 0x0F) - { - byte2 = get_byte(ea+1) - 0x10; - } - - if (byte2 >= 0x70 && byte2 <= 0x7F) - { - switch (byte2) - { - case 0x77: //ja - case 0x72: //jb - case 0x74: //jz - case 0x7F: //jg - case 0x7C: //jl - case 0x70: //jo - case 0x7A: //jp - case 0x78: //js - return 1; - default: - return 2; - } - } - - return 0; -} - - -/*------------------------------------------------*/ -/* function : x86_get_fake_jump */ -/* arguments: ea_t ea */ -/* description: Returns jump for jump $5/$2 */ -/*------------------------------------------------*/ - -ea_t x86_get_fake_jump(ea_t ea) -{ - unsigned char byte; - unsigned long l; - - byte = get_byte(ea); - if (byte == 0xE9) - { - l = get_long(ea+1); - if (l == 0) - return get_item_end(ea); - } - else if (byte == 0xeb) - { - byte = get_byte(ea+1); - if (byte == 0) - return get_item_end(ea); - } - - return BADADDR; -} - -/*------------------------------------------------*/ -/* function : x86_is_direct_jump */ -/* arguments: ea_t ea */ -/* description: Returns TRUE if a direct jump */ -/*------------------------------------------------*/ - -bool x86_is_direct_jump(ea_t ea) -{ - unsigned char byte; - - byte = get_byte(ea); - switch (byte) - { - case 0xE9: - case 0xEA: - case 0xEB: - case 0xFF: - return true; - default: - return false; - } - - return false; -} - - -/*------------------------------------------------*/ -/* function : x86_is_end_block */ -/* arguments: ea_t ea */ -/* description: Returns true on int 3 */ -/*------------------------------------------------*/ - -bool x86_is_end_block(ea_t ea) -{ - if (get_byte(ea) == 0xCC) - return true; - - return false; -} - - -/*------------------------------------------------*/ -/* function : x86_get_byte */ -/* arguments: unsigned char _byte */ -/* description: Returns opcode */ -/* note: convert push/pop registers and remove rex*/ -/* prefix */ -/*------------------------------------------------*/ - -unsigned char x86_get_byte(ea_t ea) -{ - unsigned char byte; - - byte = get_byte(ea); - - if (patchdiff_cpu == CPU_X8664 && x86_is_rex_prefix(byte)) - { - ea++; - byte = get_byte(ea); - } - - if (x86_is_push_register (byte)) - byte = 0x50; // push eax - else if (x86_is_pop_register (byte)) - byte = 0x58; // pop eax - else if (x86_is_inc_register(byte)) - byte = 0x40; // inc eax - else if (x86_is_dec_register(byte)) - byte = 0x48; // dec eax - - x86_convert_16bit_rep(&byte, ea); - x86_convert_cond_jump(&byte, ea); - - return byte; -} \ No newline at end of file diff --git a/x86.hpp b/x86.hpp deleted file mode 100644 index b0d9f09..0000000 --- a/x86.hpp +++ /dev/null @@ -1,31 +0,0 @@ -/* - Patchdiff2 - Portions (C) 2010 - 2011 Nicolas Pouvesle - Portions (C) 2007 - 2009 Tenable Network Security, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#ifndef __PX86_H__ -#define __PX86_H__ - -#include "precomp.hpp" - -unsigned char x86_get_byte(ea_t); -bool x86_remove_instr(unsigned char, ea_t); -bool x86_is_end_block(ea_t); -bool x86_is_direct_jump(ea_t ea); -ea_t x86_get_fake_jump(ea_t); -int x86_is_cond_jump_pos(ea_t); - -#endif \ No newline at end of file