2019-05-13 12:44:06 +00:00
<!DOCTYPE html>
2019-08-09 03:54:43 +00:00
< html class = "theme-next gemini use-motion" lang = "zh-Hans" >
2019-05-13 12:44:06 +00:00
< head > < meta name = "generator" content = "Hexo 3.8.0" >
< meta charset = "UTF-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" >
< meta name = "viewport" content = "width=device-width, initial-scale=1, maximum-scale=1" >
< meta name = "theme-color" content = "#222" >
< meta http-equiv = "Cache-Control" content = "no-transform" >
< meta http-equiv = "Cache-Control" content = "no-siteapp" >
< link href = "/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel = "stylesheet" type = "text/css" >
< link href = "/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel = "stylesheet" type = "text/css" >
< link href = "/css/main.css?v=5.1.4" rel = "stylesheet" type = "text/css" >
< link rel = "apple-touch-icon" sizes = "180x180" href = "/images/hackerrank.png?v=5.1.4" >
< link rel = "icon" type = "image/png" sizes = "32x32" href = "/images/hackerrank.png?v=5.1.4" >
< link rel = "icon" type = "image/png" sizes = "16x16" href = "/images/hackerrank.png?v=5.1.4" >
< link rel = "mask-icon" href = "/images/logo.svg?v=5.1.4" color = "#222" >
< meta name = "keywords" content = "文件格式,PE," >
< meta name = "description" content = "PE文件格式PE(Portable Executable)是Win32平台下可执行文件遵守的数据格式。常见的可执行文件( 如exe和dll) 都是典型的PE文件。PE文件格式其实是一种数据结构, 包含Windows操作系统加载管理可执行代码时所必要的信息, 如二进制机器代码、字符串、菜单、图标、位图、字体等。PE文件格式规定了所有这些信息在可执行文件中如何组织。在程序被执行时, 操作系统会按照PE文件格式的" >
< meta name = "keywords" content = "文件格式,PE" >
< meta property = "og:type" content = "article" >
< meta property = "og:title" content = "PE文件格式学习" >
< meta property = "og:url" content = "https://cool-y.github.io/2019/05/13/PE-file/index.html" >
< meta property = "og:site_name" content = "混元霹雳手" >
< meta property = "og:description" content = "PE文件格式PE(Portable Executable)是Win32平台下可执行文件遵守的数据格式。常见的可执行文件( 如exe和dll) 都是典型的PE文件。PE文件格式其实是一种数据结构, 包含Windows操作系统加载管理可执行代码时所必要的信息, 如二进制机器代码、字符串、菜单、图标、位图、字体等。PE文件格式规定了所有这些信息在可执行文件中如何组织。在程序被执行时, 操作系统会按照PE文件格式的" >
< meta property = "og:locale" content = "zh-Hans" >
< meta property = "og:image" content = "https://res.cloudinary.com/dozyfkbg3/image/upload/v1556519313/1506049226526485.jpg" >
< meta property = "og:updated_time" content = "2019-05-13T12:43:24.447Z" >
< meta name = "twitter:card" content = "summary" >
< meta name = "twitter:title" content = "PE文件格式学习" >
< meta name = "twitter:description" content = "PE文件格式PE(Portable Executable)是Win32平台下可执行文件遵守的数据格式。常见的可执行文件( 如exe和dll) 都是典型的PE文件。PE文件格式其实是一种数据结构, 包含Windows操作系统加载管理可执行代码时所必要的信息, 如二进制机器代码、字符串、菜单、图标、位图、字体等。PE文件格式规定了所有这些信息在可执行文件中如何组织。在程序被执行时, 操作系统会按照PE文件格式的" >
< meta name = "twitter:image" content = "https://res.cloudinary.com/dozyfkbg3/image/upload/v1556519313/1506049226526485.jpg" >
< script type = "text/javascript" id = "hexo.configurations" >
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
2019-08-09 03:54:43 +00:00
scheme: 'Gemini',
2019-05-13 12:44:06 +00:00
version: '5.1.4',
sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
fancybox: true,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
< / script >
< link rel = "canonical" href = "https://cool-y.github.io/2019/05/13/PE-file/" >
< title > PE文件格式学习 | 混元霹雳手< / title >
< / head >
< body itemscope itemtype = "http://schema.org/WebPage" lang = "zh-Hans" >
< div class = "container sidebar-position-left page-post-detail" >
< div class = "headband" > < / div >
< header id = "header" class = "header" itemscope itemtype = "http://schema.org/WPHeader" >
< div class = "header-inner" > < div class = "site-brand-wrapper" >
< div class = "site-meta " >
< div class = "custom-logo-site-title" >
< a href = "/" class = "brand" rel = "start" >
< span class = "logo-line-before" > < i > < / i > < / span >
< span class = "site-title" > 混元霹雳手< / span >
< span class = "logo-line-after" > < i > < / i > < / span >
< / a >
< / div >
2019-07-24 03:51:42 +00:00
< p class = "site-subtitle" > < / p >
2019-05-13 12:44:06 +00:00
< / div >
< div class = "site-nav-toggle" >
< button >
< span class = "btn-bar" > < / span >
< span class = "btn-bar" > < / span >
< span class = "btn-bar" > < / span >
< / button >
< / div >
< / div >
< nav class = "site-nav" >
< ul id = "menu" class = "menu" >
< li class = "menu-item menu-item-home" >
< a href = "/" rel = "section" >
< i class = "menu-item-icon fa fa-fw fa-home" > < / i > < br >
首页
< / a >
< / li >
< li class = "menu-item menu-item-about" >
< a href = "/about/" rel = "section" >
< i class = "menu-item-icon fa fa-fw fa-user" > < / i > < br >
关于
< / a >
< / li >
< li class = "menu-item menu-item-tags" >
< a href = "/tags/" rel = "section" >
< i class = "menu-item-icon fa fa-fw fa-tags" > < / i > < br >
标签
< / a >
< / li >
< li class = "menu-item menu-item-categories" >
< a href = "/categories/" rel = "section" >
< i class = "menu-item-icon fa fa-fw fa-th" > < / i > < br >
分类
< / a >
< / li >
< li class = "menu-item menu-item-archives" >
< a href = "/archives/" rel = "section" >
< i class = "menu-item-icon fa fa-fw fa-archive" > < / i > < br >
归档
< / a >
< / li >
< li class = "menu-item menu-item-bookmarks" >
< a href = "/bookmarks/" rel = "section" >
< i class = "menu-item-icon fa fa-fw fa-map" > < / i > < br >
书签
< / a >
< / li >
2019-08-08 12:42:56 +00:00
2019-08-08 12:47:43 +00:00
< li class = "menu-item menu-item-hack之外" >
2019-08-08 12:42:56 +00:00
< a href = "/hack之外/" rel = "section" >
2019-08-08 12:52:19 +00:00
< i class = "menu-item-icon fa fa-fw fa-heartbeat" > < / i > < br >
2019-08-08 12:42:56 +00:00
2019-08-08 12:52:19 +00:00
HACK之外
2019-08-08 12:42:56 +00:00
< / a >
< / li >
2019-05-13 12:44:06 +00:00
< li class = "menu-item menu-item-search" >
< a href = "javascript:;" class = "popup-trigger" >
< i class = "menu-item-icon fa fa-search fa-fw" > < / i > < br >
搜索
< / a >
< / li >
< / ul >
< div class = "site-search" >
< div class = "popup search-popup local-search-popup" >
< div class = "local-search-header clearfix" >
< span class = "search-icon" >
< i class = "fa fa-search" > < / i >
< / span >
< span class = "popup-btn-close" >
< i class = "fa fa-times-circle" > < / i >
< / span >
< div class = "local-search-input-wrapper" >
< input autocomplete = "off" placeholder = "搜索..." spellcheck = "false" type = "text" id = "local-search-input" >
< / div >
< / div >
< div id = "local-search-result" > < / div >
< / div >
< / div >
< / nav >
< / div >
< / header >
< main id = "main" class = "main" >
< div class = "main-inner" >
< div class = "content-wrap" >
< div id = "content" class = "content" >
< div id = "posts" class = "posts-expand" >
< article class = "post post-type-normal" itemscope itemtype = "http://schema.org/Article" >
< div class = "post-block" >
< link itemprop = "mainEntityOfPage" href = "https://cool-y.github.io/2019/05/13/PE-file/" >
< span hidden itemprop = "author" itemscope itemtype = "http://schema.org/Person" >
< meta itemprop = "name" content = "Cool-Y" >
< meta itemprop = "description" content >
< meta itemprop = "image" content = "/images/avatar.png" >
< / span >
< span hidden itemprop = "publisher" itemscope itemtype = "http://schema.org/Organization" >
< meta itemprop = "name" content = "混元霹雳手" >
< / span >
< header class = "post-header" >
< h1 class = "post-title" itemprop = "name headline" > PE文件格式学习< / h1 >
< div class = "post-meta" >
< span class = "post-time" >
< span class = "post-meta-item-icon" >
< i class = "fa fa-calendar-o" > < / i >
< / span >
< span class = "post-meta-item-text" > 发表于< / span >
< time title = "创建于" itemprop = "dateCreated datePublished" datetime = "2019-05-13T20:37:00+08:00" >
2019-05-13
< / time >
< / span >
< span class = "post-category" >
< span class = "post-meta-divider" > |< / span >
< span class = "post-meta-item-icon" >
< i class = "fa fa-folder-o" > < / i >
< / span >
< span class = "post-meta-item-text" > 分类于< / span >
< span itemprop = "about" itemscope itemtype = "http://schema.org/Thing" >
< a href = "/categories/二进制/" itemprop = "url" rel = "index" >
< span itemprop = "name" > 二进制< / span >
< / a >
< / span >
< / span >
< span id = "/2019/05/13/PE-file/" class = "leancloud_visitors" data-flag-title = "PE文件格式学习" >
< span class = "post-meta-divider" > |< / span >
< span class = "post-meta-item-icon" >
< i class = "fa fa-eye" > < / i >
< / span >
< span class = "post-meta-item-text" > 阅读次数: < / span >
< span class = "leancloud-visitors-count" > < / span >
< / span >
< div class = "post-wordcount" >
< span class = "post-meta-item-icon" >
< i class = "fa fa-file-word-o" > < / i >
< / span >
< span title = "字数统计" >
2.8k 字
< / span >
< span class = "post-meta-divider" > |< / span >
< span class = "post-meta-item-icon" >
< i class = "fa fa-clock-o" > < / i >
< / span >
< span title = "阅读时长" >
10 分钟
< / span >
< / div >
< / div >
< / header >
< div class = "post-body" itemprop = "articleBody" >
< h1 id = "PE文件格式" > < a href = "#PE文件格式" class = "headerlink" title = "PE文件格式" > < / a > PE文件格式< / h1 > < p > PE(Portable Executable)是Win32平台下可执行文件遵守的数据格式。常见的可执行文件( 如exe和dll) 都是典型的PE文件。PE文件格式其实是一种数据结构, 包含Windows操作系统加载管理可执行代码时所必要的信息, 如二进制机器代码、字符串、菜单、图标、位图、字体等。PE文件格式规定了所有这些信息在可执行文件中如何组织。< strong > 在程序被执行时, 操作系统会按照PE文件格式的约定去相应地方准确定位各种类型的资源, 并分别装入内存的不同区域。< / strong > < br > PE文件格式把可执行文件分成若干个数据节( section) , 不同资源被存放在不同的节中, 一个典型的PE文件中包含的节如下: < / p >
< ul >
< li > < code > .text< / code > 由编译器产生,存放着二进制的机器代码,也是反汇编和调试的对象< / li >
< li > < code > .data< / code > 初始化的数据块,如宏定义、全局变量、静态变量等< / li >
< li > < code > .idata< / code > 可执行文件所使用的动态链接库等外来函数与文件信息< / li >
< li > < code > .rsrc< / code > 存放程序的资源,如图标、菜单等< br > 除此之外,还有可能有< code > .reloc< / code > ,< code > .edata< / code > ,< code > .tls< / code > ,< code > .rdata< / code > < / li >
< / ul >
< h1 id = "0x01-PE文件与虚拟内存之间的映射" > < a href = "#0x01-PE文件与虚拟内存之间的映射" class = "headerlink" title = "0x01 PE文件与虚拟内存之间的映射" > < / a > 0x01 PE文件与虚拟内存之间的映射< / h1 > < h2 id = "虚拟内存" > < a href = "#虚拟内存" class = "headerlink" title = "虚拟内存" > < / a > 虚拟内存< / h2 > < p > Windows的内存可以被分为两个层面: 物理内存和虚拟内存。其中, 物理内存比较复杂, 需要进入Windows内核级别ring0才能看到。通常, 在用户模式下, 我们用调试器看到的都是虚拟内存。< br > 如果我们把这看成银行,那么就很好理解了。< / p >
< ul >
< li > 进程相当于储户< / li >
< li > 内存管理器相当于银行< / li >
< li > 物理内存相当于钞票< / li >
< li > 虚拟内存相当于存款< / li >
< / ul >
< h2 id = "映射关系" > < a href = "#映射关系" class = "headerlink" title = "映射关系" > < / a > 映射关系< / h2 > < ol >
< li > 在漏洞挖掘中,经常需要的两种操作:< / li >
< / ol >
< ul >
< li > 静态反编译工具看到的是PE文件中某条指令的位置是相对与磁盘而言的, 就是所谓的 < strong > 文件偏移< / strong > ,我们可能还需要知道这条指令在内存中的位置,这个位置就是虚拟内存地址(VA)< / li >
< li > 反过来,在调试时看到的某条指令的地址是 < strong > 虚拟内存地址( VA) < / strong > , 也就是我们需要回到PE文件中找到这条指令对应的机器码< / li >
< / ul >
< ol start = "2" >
< li > 几个重要概念< / li >
< / ol >
< ul >
< li > 文件偏移地址(File Offset): < br > 数据在PE文件中的地址叫做文件偏移地址,可以理解为就是文件地址。这是文件在磁盘上存放相对与文件开头的偏移。< / li >
< li > 装载基址(Image Base):< br > PE装入内存时的基地址。默认情况下, EXE文件在内存中对应的基地址是< code > 0x00400000< / code > ,DLL文件是< code > 0x10000000< / code > 。这些位置可能通过编译选项修改< / li >
< li > 虚拟内存地址(Virtual Address,VA ): < br > PE文件中的指令装入内存后的地址。< / li >
< li > 相对虚拟地址(Relative Virtual Address, RVA): < br > 相对虚拟地址是内存地址相对于映射基址的偏移量。< / li >
< / ul >
< ol start = "3" >
< li > < p > 虚拟内存地址,装载基址,相对虚拟内存地址三者之间的关系:< / p >
< blockquote >
< p > VA = Image Base + RVA< / p >
< / blockquote >
< / li >
< li > < p > 文件偏移地址与相对虚拟地址:< br > 文件偏移地址是相对于文件开始处0字节的偏移,RVA(相对虚拟地址)则是相对于装载基址0x00400000处的偏移.由于操作系统在装载时“基本”上保持PE中的数据结构, 所以文件偏移地址和RVA有很大的一致性。( 不是全部相同) < br > PE文件中的数据按照磁盘数据标准存放, 以0x200为基本单位进行组织。当一个数据节(stction)不足0x200字节时, 不足的地方将用0x00填充, 当一个数据节超过0x200时, 下一个0x200块将分配给这个节使用。所以PE数据节大小永远是0x200的整数倍< br > 当代码装入后, 将按照内存数据标准存放, 并以0x1000字节为基本的存储单位进行组织, 不足和超过的情况类似上面。因此, 内存中的节总是0x1000的整倍数。< br > 由于内存中数据节相对于装载基址的偏移量和文件中数据节的偏移量有上述差异,所以进行文件偏移到内存地址之间的换算时,还要看所转换的地址位于第几个节内:< / p >
< blockquote >
< p > 文件偏移地址 = 虚拟内存地址(VA) - 装载基址(Image Base) - 节偏移< br > = RVA - 节偏移< / p >
< / blockquote >
< / li >
< li > < p > 工具< br > < a href = "https://tools.pediy.com/win/PE_tools/Lordpe/LPE-DLX.rar" target = "_blank" rel = "noopener" > LordPE DLX增强版(2017..6.08)< / a > < br > < a href = "https://tools.pediy.com/win/Resource/Resource%20Hacker/reshhack3.4.zip" target = "_blank" rel = "noopener" > Resource Hacker 3.4.0< / a > < br > < a href = "https://download.cnet.com/PE-Viewer/3000-2352_4-10966763.html" target = "_blank" rel = "noopener" > PE viewer< / a > < / p >
< / li >
< / ol >
< h1 id = "0x02-链接库与函数" > < a href = "#0x02-链接库与函数" class = "headerlink" title = "0x02 链接库与函数" > < / a > 0x02 链接库与函数< / h1 > < p > 对于一个可执行程序,可以收集到最有用的信息就是导入表。导入函数是程序所使用的但存储在另一程序中的那些函数。通过导入函数连接,使得不必重新在多个程序中重复实现特定功能。< / p >
< ol >
< li > 静态链接、运行时链接与动态链接。< br > 静态链接:当一个库被静态链接到可执行程序时,所有这个库中的代码都会被复制到可执行程序中,这使得可执行程序增大许多,而且在分析代码时,很难区分静态链接的代码和自身代码。< br > 运行时链接:在恶意代码中常用(加壳或混淆时),只有当需要使用函数时,才链接到库。< br > 动态链接:当代码被动态链接时,宿主操作系统会在程序装载时搜索所需代码库,如果程序调用了被链接的库函数,这个函数会在代码库中执行。< br > LoadLibrary和GetProcAddress允许一个程序访问系统上任何库中的函数, 因此当它们被使用时, 无法静态分析出程序会链接哪些函数。< br > PE文件头存储了每个将被装载的库文件, 以及每个会被程序使用的函数信息。< / li >
< li > 工具Dependency Walker< br > < a href = "http://www.dependencywalker.com/" target = "_blank" rel = "noopener" > Dependency Walker< / a > < / li >
< li > < p > 常见dll程序< br > < strong > < em > kernel32.dll< / em > < / strong > < br > kernel32.dll是Windows 9x/Me中非常重要的32位动态链接库文件, 属于内核级文件。它控制着系统的内存管理、数据的输入输出操作和中断处理, 当Windows启动时, kernel32.dll就驻留在内存中特定的写保护区域, 使别的程序无法占用这个内存区域。< br > < strong > < em > user32.dll< / em > < / strong > < br > user32.dll是Windows用户界面相关应用程序接口, 用于包括Windows处理, 基本用户界面等特性, 如创建窗口和发送消息。< br > 在早期32-bit 版本的Windows中, 用户控件是在ComCtl32中实现的, 但是一些控件的显示功能是在User32.dll中实现的。例如在一个窗口中非客户区域( 边框和菜单) 的绘制就是由User32.dll来完成的。User32.dll 是操作系统的一个核心控件, 它和操作系统是紧密联系在一起的。也就是说, 不同版本的Windows中User32.dll 是不同。因此, 应用程序在不同版本的Windows中运行的时候, 由于User32.dll的不同, 会导致应用程序的界面通常会有微小的不同。< br > < strong > < em > gdi32.dll< / em > < / strong > < br > gdi32.dll是Windows GDI图形用户界面相关程序, 包含的函数用来绘制图像和显示文字< br > < strong > < em > comdlg32.dll< / em > < / strong > < br > comdlg32.dll是Windows应用程序公用对话框模块, 用于例如打开文件对话框。< br > < strong > < em > advapi32.dll< / em > < / strong > < br > advapi32.dll是一个高级API应用程序接口服务库的一部分, 包含的函数与对象的安全性, 注册表的操控以及事件日志有关。< br > < strong > < em > shell32.dll< / em > < / strong > < br > shell32.dll是Windows的32位外壳动态链接库文件, 用于打开网页和文件, 建立文件时的默认文件名的设置等大量功能。< br > 严格来讲, 它只是代码的合集, 真正执行这些功能的是操作系统的相关程序, dll文件只是根据设置调用这些程序的相关功能罢了。< br > < strong > < em > ole32.dll< / em > < / strong > < br > ole32.dll是对象链接和嵌入相关模块。< br > < strong > < em > odbc32.dll< / em > < / strong > < br > odbc32.dll是ODBC数据库查询相关文件。< / p >
< / li >
< li > < p > 导入函数与导出函数< br > 导入函数和导出函数都是用来和其他程序和代码进行交互时使用的, 通常一个DLL会实现一个或多个功能函数, 然后将他们导出, 使得别的程序可以导入并使用这些函数, 导出函数在DLL文件中是最常见的。< / p >
< / li >
< / ol >
< h1 id = "0x03-PE文件的结构" > < a href = "#0x03-PE文件的结构" class = "headerlink" title = "0x03 PE文件的结构" > < / a > 0x03 PE文件的结构< / h1 > < table >
< thead >
< tr >
< th > PE文件结构< / th >
< / tr >
< / thead >
< tbody >
< tr >
< td > MZ文件头< / td >
< / tr >
< tr >
< td > DOS插桩程序< / td >
< / tr >
< tr >
< td > 字串“PE\0\0”(4字节)< / td >
< / tr >
< tr >
< td > 映像文件头< / td >
< / tr >
< tr >
< td > 可选映像头< / td >
< / tr >
< tr >
< td > Section table(节表)< / td >
< / tr >
< tr >
< td > Section 1< / td >
< / tr >
< tr >
< td > Section 2< / td >
< / tr >
< tr >
< td > …..< / td >
< / tr >
< / tbody >
< / table >
< ol >
< li > DOS程序头(4H字节)< br > 包括MZ文件头和DOS插桩程序。MZ文件头开始两个字节为4D5A。< / li >
< li > NT映像头(14H字节)< br > 存放PE整个文件信息分布的重要字段。包括: < / li >
< / ol >
< ul >
< li > 签名( signature) : 值为’ 50450000h’ , 字串为’ PE\0\0’ , 可以在DOS程序头的3CH处的四个字节找到该字串的偏移位置< / li >
2019-07-16 09:15:34 +00:00
< li > 映像文件头( FileHeader) : 是映像头的主要部分, 包含PE文件最基本的信息。结构体为: < figure class = "highlight plain" > < table > < tr > < td class = "gutter" > < pre > < span class = "line" > 1< / span > < br > < span class = "line" > 2< / span > < br > < span class = "line" > 3< / span > < br > < span class = "line" > 4< / span > < br > < span class = "line" > 5< / span > < br > < span class = "line" > 6< / span > < br > < span class = "line" > 7< / span > < br > < span class = "line" > 8< / span > < br > < span class = "line" > 9< / span > < br > < / pre > < / td > < td class = "code" > < pre > < span class = "line" > typedef struct _IMAGE_FILE_HEADER { < / span > < br > < span class = "line" > WORD Machine; //运行平台< / span > < br > < span class = "line" > WORD NumberOfSections; //节(section)数目 < / span > < br > < span class = "line" > DWORD TimeDateStamp; //时间日期标记 < / span > < br > < span class = "line" > DWORD PointerToSymbolTable; //COFF符号指针, 这是程序调试信息 < / span > < br > < span class = "line" > DWORD NumberOfSymbols; //符号数 < / span > < br > < span class = "line" > WORD SizeOfOptionalHeader; //可选部首长度, 是IMAGE_OPTIONAL_HEADER的长度 < / span > < br > < span class = "line" > WORD Characteristics; //文件属性< / span > < br > < span class = "line" > } < / span > < br > < / pre > < / td > < / tr > < / table > < / figure >
2019-05-13 12:44:06 +00:00
< / li >
< / ul >
< ol start = "3" >
< li > < p > 可选映像头(OptionalHeader)< br > 包含PE文件的逻辑分布信息, 共有13个域。具体结构为: < / p >
2019-07-16 09:15:34 +00:00
< figure class = "highlight plain" > < table > < tr > < td class = "gutter" > < pre > < span class = "line" > 1< / span > < br > < span class = "line" > 2< / span > < br > < span class = "line" > 3< / span > < br > < span class = "line" > 4< / span > < br > < span class = "line" > 5< / span > < br > < span class = "line" > 6< / span > < br > < span class = "line" > 7< / span > < br > < span class = "line" > 8< / span > < br > < span class = "line" > 9< / span > < br > < span class = "line" > 10< / span > < br > < span class = "line" > 11< / span > < br > < span class = "line" > 12< / span > < br > < span class = "line" > 13< / span > < br > < span class = "line" > 14< / span > < br > < span class = "line" > 15< / span > < br > < span class = "line" > 16< / span > < br > < span class = "line" > 17< / span > < br > < span class = "line" > 18< / span > < br > < span class = "line" > 19< / span > < br > < span class = "line" > 20< / span > < br > < span class = "line" > 21< / span > < br > < span class = "line" > 22< / span > < br > < span class = "line" > 23< / span > < br > < span class = "line" > 24< / span > < br > < span class = "line" > 25< / span > < br > < span class = "line" > 26< / span > < br > < span class = "line" > 27< / span > < br > < span class = "line" > 28< / span > < br > < span class = "line" > 29< / span > < br > < span class = "line" > 30< / span > < br > < span class = "line" > 31< / span > < br > < span class = "line" > 32< / span > < br > < span class = "line" > 33< / span > < br > < / pre > < / td > < td class = "code" > < pre > < span class = "line" > typedef struct _IMAGE_OPTIONAL_HEADER { < / span > < br > < span class = "line" > WORD Magic; //代表的是文件的格式< / span > < br > < span class = "line" > BYTE MajorLinkerVersion;< / span > < br > < span class = "line" > BYTE MinorLinkerVersion;< / span > < br > < span class = "line" > DWORD SizeOfCode;< / span > < br > < span class = "line" > DWORD SizeOfInitializedData;< / span > < br > < span class = "line" > DWORD SizeOfUninitializedData;< / span > < br > < span class = "line" > DWORD AddressOfEntryPoint; //保存着EP的RVA。也就是最先执行代码起始地址。< / span > < br > < span class = "line" > DWORD BaseOfCode; //表示代码段起始RVA先看他的值, 是1000< / span > < br > < span class = "line" > DWORD BaseOfData;< / span > < br > < span class = "line" > DWORD ImageBase; //默认装入基地址< / span > < br > < span class = "line" > DWORD SectionAlignment; //节区在内存中的最下单位< / span > < br > < span class = "line" > DWORD FileAlignment; //节区在磁盘中的最小单位< / span > < br > < span class = "line" > WORD MajorOperatingSystemVersion;< / span > < br > < span class = "line" > WORD MinorOperatingSystemVersion;< / span > < br > < span class = "line" > WORD MajorImageVersion;< / span > < br > < span class = "line" > WORD MinorImageVersion;< / span > < br > < span class = "line" > WORD MajorSubsystemVersion;< / span > < br > < span class = "line" > WORD MinorSubsystemVersion;< / span > < br > < span class = "line" > DWORD Win32VersionValue;< / span > < br > < span class = "line" > DWORD SizeOfImage; //装入内存后的总尺寸< / span > < br > < span class = "line" > DWORD SizeOfHeaders; //头尺寸=NT映像头+节表< / span > < br > < span class = "line" > DWORD CheckSum;< / span > < br > < span class = "line" > WORD Subsystem;< / span > < br > < span class = "line" > WORD DllCharacteristics;< / span > < br > < span class = "line" > DWORD SizeOfStackReserve;< / span > < br > < span class = "line" > DWORD SizeOfStackCommit;< / span > < br > < span class = "line" > DWORD SizeOfHeapReserve;< / span > < br > < span class = "line" > DWORD SizeOfHeapCommit;< / span > < br > < span class = "line" > DWORD LoaderFlags;< / span > < br > < span class = "line" > DWORD NumberOfRvaAndSizes;< / span > < br > < span class = "line" > IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];< / span > < br > < span class = "line" > } IMAGE_OPTIONAL_HEADER, *PIMAGE_OPTIONAL_HEADER;< / span > < br > < / pre > < / td > < / tr > < / table > < / figure >
2019-05-13 12:44:06 +00:00
< / li >
< li > < p > 节表< br > 实际上是一个结构数组, 其中每个结构包含了一个节的具体信息( 每个结构占用28H字节) < / p >
2019-07-16 09:15:34 +00:00
< figure class = "highlight plain" > < table > < tr > < td class = "gutter" > < pre > < span class = "line" > 1< / span > < br > < span class = "line" > 2< / span > < br > < span class = "line" > 3< / span > < br > < span class = "line" > 4< / span > < br > < span class = "line" > 5< / span > < br > < span class = "line" > 6< / span > < br > < span class = "line" > 7< / span > < br > < span class = "line" > 8< / span > < br > < span class = "line" > 9< / span > < br > < span class = "line" > 10< / span > < br > < span class = "line" > 11< / span > < br > < span class = "line" > 12< / span > < br > < span class = "line" > 13< / span > < br > < span class = "line" > 14< / span > < br > < span class = "line" > 15< / span > < br > < / pre > < / td > < td class = "code" > < pre > < span class = "line" > typedef struct _IMAGE_SECTION_HEADER { < / span > < br > < span class = "line" > BYTE Name[IMAGE_SIZEOF_SHORT_NAME];< / span > < br > < span class = "line" > union { < / span > < br > < span class = "line" > DWORD PhysicalAddress;< / span > < br > < span class = "line" > DWORD VirtualSize; //该节的实际字节数< / span > < br > < span class = "line" > } Misc;< / span > < br > < span class = "line" > DWORD VirtualAddress; //本节的相对虚拟地址< / span > < br > < span class = "line" > DWORD SizeOfRawData; //对齐后的节尺寸< / span > < br > < span class = "line" > DWORD PointerToRawData; //本节在文件中的地址< / span > < br > < span class = "line" > DWORD PointerToRelocations; //本节调入内存后的存放位置< / span > < br > < span class = "line" > DWORD PointerToLinenumbers;< / span > < br > < span class = "line" > WORD NumberOfRelocations;< / span > < br > < span class = "line" > WORD NumberOfLinenumbers;< / span > < br > < span class = "line" > DWORD Characteristics; //节的属性< / span > < br > < span class = "line" > } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;< / span > < br > < / pre > < / td > < / tr > < / table > < / figure >
2019-05-13 12:44:06 +00:00
< / li >
< li > < p > 节< / p >
< / li >
< / ol >
< ul >
< li > < p > 引入函数节(.rdata/.idata)< / p >
2019-07-16 09:15:34 +00:00
< figure class = "highlight plain" > < table > < tr > < td class = "gutter" > < pre > < span class = "line" > 1< / span > < br > < span class = "line" > 2< / span > < br > < span class = "line" > 3< / span > < br > < span class = "line" > 4< / span > < br > < span class = "line" > 5< / span > < br > < span class = "line" > 6< / span > < br > < span class = "line" > 7< / span > < br > < span class = "line" > 8< / span > < br > < span class = "line" > 9< / span > < br > < span class = "line" > 10< / span > < br > < span class = "line" > 11< / span > < br > < span class = "line" > 12< / span > < br > < span class = "line" > 13< / span > < br > < span class = "line" > 14< / span > < br > < / pre > < / td > < td class = "code" > < pre > < span class = "line" > typedef struct _IMAGE_IMPORT_DESCRIPTOR { < / span > < br > < span class = "line" > union { < / span > < br > < span class = "line" > DWORD Characteristics; // 0 for terminating null import descriptor< / span > < br > < span class = "line" > DWORD OriginalFirstThunk; // RVA to original unbound IAT (PIMAGE_THUNK_DATA)< / span > < br > < span class = "line" > } ;< / span > < br > < span class = "line" > DWORD TimeDateStamp; // 0 if not bound,< / span > < br > < span class = "line" > // -1 if bound, and real datetime stamp< / span > < br > < span class = "line" > // in IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT (new BIND)< / span > < br > < span class = "line" > // O.W. date/time stamp of DLL bound to (Old BIND)< / span > < br > < span class = "line" > < / span > < br > < span class = "line" > DWORD ForwarderChain; // -1 if no forwarders< / span > < br > < span class = "line" > DWORD Name;< / span > < br > < span class = "line" > DWORD FirstThunk; // RVA to IAT (if bound this IAT has actual addresses)< / span > < br > < span class = "line" > } IMAGE_IMPORT_DESCRIPTOR;< / span > < br > < / pre > < / td > < / tr > < / table > < / figure >
2019-05-13 12:44:06 +00:00
< / li >
< li > < p > 一个exe程序加载dll的IMAGE_IMPORT_DESCRIPTOR< br > < img src = "https://res.cloudinary.com/dozyfkbg3/image/upload/v1556519313/1506049226526485.jpg" alt > < / p >
< / li >
< / ul >
< / div >
< div >
< div style = "padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;" >
< div > 您的支持将鼓励我继续创作!< / div >
< button id = "rewardButton" disable = "enable" onclick = "var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}" >
< span > 打赏< / span >
< / button >
< div id = "QR" style = "display: none;" >
< div id = "wechat" style = "display: inline-block" >
< img id = "wechat_qr" src = "/images/Wechatpay.png" alt = "Cool-Y 微信支付" >
< p > 微信支付< / p >
< / div >
< div id = "alipay" style = "display: inline-block" >
< img id = "alipay_qr" src = "/images/Alipay.png" alt = "Cool-Y 支付宝" >
< p > 支付宝< / p >
< / div >
< / div >
< / div >
< / div >
< footer class = "post-footer" >
< div class = "post-tags" >
< a href = "/tags/文件格式/" rel = "tag" > # 文件格式< / a >
< a href = "/tags/PE/" rel = "tag" > # PE< / a >
< / div >
< div class = "post-nav" >
< div class = "post-nav-next post-nav-item" >
< a href = "/2019/04/21/XIAOMI-UPnP/" rel = "next" title = "小米路由器_MiniUPnP协议" >
< i class = "fa fa-chevron-left" > < / i > 小米路由器_MiniUPnP协议
< / a >
< / div >
< span class = "post-nav-divider" > < / span >
< div class = "post-nav-prev post-nav-item" >
2019-07-09 06:49:56 +00:00
< a href = "/2019/05/14/pack-and-unpack/" rel = "prev" title = "加壳与脱壳" >
加壳与脱壳 < i class = "fa fa-chevron-right" > < / i >
2019-05-17 08:24:31 +00:00
< / a >
2019-05-13 12:44:06 +00:00
< / div >
< / div >
< / footer >
< / div >
< / article >
< div class = "post-spread" >
< / div >
< / div >
< / div >
< div class = "comments" id = "comments" >
< div id = "gitment-container" > < / div >
< / div >
< / div >
< div class = "sidebar-toggle" >
< div class = "sidebar-toggle-line-wrap" >
< span class = "sidebar-toggle-line sidebar-toggle-line-first" > < / span >
< span class = "sidebar-toggle-line sidebar-toggle-line-middle" > < / span >
< span class = "sidebar-toggle-line sidebar-toggle-line-last" > < / span >
< / div >
< / div >
< aside id = "sidebar" class = "sidebar" >
< div class = "sidebar-inner" >
< ul class = "sidebar-nav motion-element" >
< li class = "sidebar-nav-toc sidebar-nav-active" data-target = "post-toc-wrap" >
文章目录
< / li >
< li class = "sidebar-nav-overview" data-target = "site-overview-wrap" >
站点概览
< / li >
< / ul >
< section class = "site-overview-wrap sidebar-panel" >
< div class = "site-overview" >
< div class = "site-author motion-element" itemprop = "author" itemscope itemtype = "http://schema.org/Person" >
< img class = "site-author-image" itemprop = "image" src = "/images/avatar.png" alt = "Cool-Y" >
< p class = "site-author-name" itemprop = "name" > Cool-Y< / p >
2019-07-24 03:51:42 +00:00
< p class = "site-description motion-element" itemprop = "description" > 没人比我更懂中医#MAGA< / p >
2019-05-13 12:44:06 +00:00
< / div >
< nav class = "site-state motion-element" >
< div class = "site-state-item site-state-posts" >
< a href = "/archives/" >
2019-10-25 13:13:53 +00:00
< span class = "site-state-item-count" > 22< / span >
2019-05-13 12:44:06 +00:00
< span class = "site-state-item-name" > 日志< / span >
< / a >
< / div >
< div class = "site-state-item site-state-categories" >
< a href = "/categories/index.html" >
2019-10-15 11:24:26 +00:00
< span class = "site-state-item-count" > 7< / span >
2019-05-13 12:44:06 +00:00
< span class = "site-state-item-name" > 分类< / span >
< / a >
< / div >
< div class = "site-state-item site-state-tags" >
< a href = "/tags/index.html" >
2019-10-25 13:13:53 +00:00
< span class = "site-state-item-count" > 43< / span >
2019-05-13 12:44:06 +00:00
< span class = "site-state-item-name" > 标签< / span >
< / a >
< / div >
< / nav >
< div class = "links-of-author motion-element" >
< span class = "links-of-author-item" >
< a href = "https://github.com/Cool-Y" target = "_blank" title = "GitHub" >
< i class = "fa fa-fw fa-github" > < / i > GitHub< / a >
< / span >
< span class = "links-of-author-item" >
< a href = "mailto:cool.yim@whu.edu.cn" target = "_blank" title = "E-Mail" >
< i class = "fa fa-fw fa-envelope" > < / i > E-Mail< / a >
< / span >
< span class = "links-of-author-item" >
< a href = "https://www.instagram.com/yan__han/" target = "_blank" title = "Instagram" >
< i class = "fa fa-fw fa-instagram" > < / i > Instagram< / a >
< / span >
< / div >
2019-10-01 12:45:37 +00:00
< div id = "music163player" >
< iframe frameborder = "no" border = "0" marginwidth = "0" marginheight = "0" width = "330" height = "450" src = "//music.163.com/outchain/player?type=4&id=334277093&auto=1&height=430" > < / iframe >
< / div >
2019-05-13 12:44:06 +00:00
< / div >
< / section >
<!-- noindex -->
< section class = "post-toc-wrap motion-element sidebar-panel sidebar-panel-active" >
< div class = "post-toc" >
2019-07-01 11:52:45 +00:00
< div class = "post-toc-content" > < ol class = "nav" > < li class = "nav-item nav-level-1" > < a class = "nav-link" href = "#PE文件格式" > < span class = "nav-text" > PE文件格式< / span > < / a > < / li > < li class = "nav-item nav-level-1" > < a class = "nav-link" href = "#0x01-PE文件与虚拟内存之间的映射" > < span class = "nav-text" > 0x01 PE文件与虚拟内存之间的映射< / span > < / a > < ol class = "nav-child" > < li class = "nav-item nav-level-2" > < a class = "nav-link" href = "#虚拟内存" > < span class = "nav-text" > 虚拟内存< / span > < / a > < / li > < li class = "nav-item nav-level-2" > < a class = "nav-link" href = "#映射关系" > < span class = "nav-text" > 映射关系< / span > < / a > < / li > < / ol > < / li > < li class = "nav-item nav-level-1" > < a class = "nav-link" href = "#0x02-链接库与函数" > < span class = "nav-text" > 0x02 链接库与函数< / span > < / a > < / li > < li class = "nav-item nav-level-1" > < a class = "nav-link" href = "#0x03-PE文件的结构" > < span class = "nav-text" > 0x03 PE文件的结构< / span > < / a > < / li > < / ol > < / div >
2019-05-13 12:44:06 +00:00
< / div >
< / section >
<!-- /noindex -->
< / div >
< / aside >
< / div >
< / main >
< footer id = "footer" class = "footer" >
< div class = "footer-inner" >
< div class = "copyright" > © < span itemprop = "copyrightYear" > 2019< / span >
< span class = "with-love" >
< i class = "fa fa-user" > < / i >
< / span >
< span class = "author" itemprop = "copyrightHolder" > Cool-Y< / span >
< span class = "post-meta-divider" > |< / span >
< span class = "post-meta-item-icon" >
< i class = "fa fa-area-chart" > < / i >
< / span >
2019-10-25 13:13:53 +00:00
< span title = "Site words total count" > 67.4k< / span >
2019-05-13 12:44:06 +00:00
< / div >
< div class = "powered-by" > 由 < a class = "theme-link" target = "_blank" href = "https://hexo.io" > Hexo< / a > 强力驱动< / div >
< div class = "busuanzi-count" >
< script async src = "//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" > < / script >
< span class = "site-uv" >
< i class = "fa fa-user" > < / i >
< span class = "busuanzi-value" id = "busuanzi_value_site_uv" > < / span >
< / span >
< span class = "site-pv" >
< i class = "fa fa-eye" > < / i >
< span class = "busuanzi-value" id = "busuanzi_value_site_pv" > < / span >
< / span >
< / div >
< / div >
< / footer >
< div class = "back-to-top" >
< i class = "fa fa-arrow-up" > < / i >
< / div >
< / div >
< script type = "text/javascript" >
if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
window.Promise = null;
}
< / script >
< script type = "text/javascript" src = "/lib/jquery/index.js?v=2.1.3" > < / script >
< script type = "text/javascript" src = "/lib/fastclick/lib/fastclick.min.js?v=1.0.6" > < / script >
< script type = "text/javascript" src = "/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7" > < / script >
< script type = "text/javascript" src = "/lib/velocity/velocity.min.js?v=1.2.1" > < / script >
< script type = "text/javascript" src = "/lib/velocity/velocity.ui.min.js?v=1.2.1" > < / script >
< script type = "text/javascript" src = "/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5" > < / script >
< script type = "text/javascript" src = "/js/src/utils.js?v=5.1.4" > < / script >
< script type = "text/javascript" src = "/js/src/motion.js?v=5.1.4" > < / script >
2019-08-09 03:54:43 +00:00
< script type = "text/javascript" src = "/js/src/affix.js?v=5.1.4" > < / script >
< script type = "text/javascript" src = "/js/src/schemes/pisces.js?v=5.1.4" > < / script >
2019-05-13 12:44:06 +00:00
< script type = "text/javascript" src = "/js/src/scrollspy.js?v=5.1.4" > < / script >
< script type = "text/javascript" src = "/js/src/post-details.js?v=5.1.4" > < / script >
< script type = "text/javascript" src = "/js/src/bootstrap.js?v=5.1.4" > < / script >
<!-- LOCAL: You can save these files to your site and update links -->
2019-07-10 04:27:33 +00:00
< link rel = "stylesheet" href = "https://jjeejj.github.io/css/gitment.css" >
< script src = "https://jjeejj.github.io/js/gitment.js" > < / script >
2019-05-13 12:44:06 +00:00
<!-- END LOCAL -->
< script type = "text/javascript" >
function renderGitment(){
2019-07-10 04:27:33 +00:00
var gitment = new Gitment({
2019-07-09 06:49:56 +00:00
id: window.location.pathname,
2019-05-13 12:44:06 +00:00
owner: 'Cool-Y',
repo: 'gitment-comments',
oauth: {
client_secret: '1c5db4da72df5e6fc318d12afe5f4406f7c54343',
client_id: '180955a2c3ae3d966d9a'
}});
gitment.render('gitment-container');
}
renderGitment();
< / script >
< script type = "text/javascript" >
// Popup Window;
var isfetched = false;
var isXml = true;
// Search DB path;
var search_path = "search.xml";
if (search_path.length === 0) {
search_path = "search.xml";
} else if (/json$/i.test(search_path)) {
isXml = false;
}
var path = "/" + search_path;
// monitor main search box;
var onPopupClose = function (e) {
$('.popup').hide();
$('#local-search-input').val('');
$('.search-result-list').remove();
$('#no-result').remove();
$(".local-search-pop-overlay").remove();
$('body').css('overflow', '');
}
function proceedsearch() {
$("body")
.append('< div class = "search-popup-overlay local-search-pop-overlay" > < / div > ')
.css('overflow', 'hidden');
$('.search-popup-overlay').click(onPopupClose);
$('.popup').toggle();
var $localSearchInput = $('#local-search-input');
$localSearchInput.attr("autocapitalize", "none");
$localSearchInput.attr("autocorrect", "off");
$localSearchInput.focus();
}
// search function;
var searchFunc = function(path, search_id, content_id) {
'use strict';
// start loading animation
$("body")
.append('< div class = "search-popup-overlay local-search-pop-overlay" > ' +
'< div id = "search-loading-icon" > ' +
'< i class = "fa fa-spinner fa-pulse fa-5x fa-fw" > < / i > ' +
'< / div > ' +
'< / div > ')
.css('overflow', 'hidden');
$("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');
$.ajax({
url: path,
dataType: isXml ? "xml" : "json",
async: true,
success: function(res) {
// get the contents from search data
isfetched = true;
$('.popup').detach().appendTo('.header-inner');
var datas = isXml ? $("entry", res).map(function() {
return {
title: $("title", this).text(),
content: $("content",this).text(),
url: $("url" , this).text()
};
}).get() : res;
var input = document.getElementById(search_id);
var resultContent = document.getElementById(content_id);
var inputEventFunction = function() {
var searchText = input.value.trim().toLowerCase();
var keywords = searchText.split(/[\s\-]+/);
if (keywords.length > 1) {
keywords.push(searchText);
}
var resultItems = [];
if (searchText.length > 0) {
// perform local searching
datas.forEach(function(data) {
var isMatch = false;
var hitCount = 0;
var searchTextCount = 0;
var title = data.title.trim();
var titleInLowerCase = title.toLowerCase();
var content = data.content.trim().replace(/< [^>]+>/g,"");
var contentInLowerCase = content.toLowerCase();
var articleUrl = decodeURIComponent(data.url);
var indexOfTitle = [];
var indexOfContent = [];
// only match articles with not empty titles
if(title != '') {
keywords.forEach(function(keyword) {
function getIndexByWord(word, text, caseSensitive) {
var wordLen = word.length;
if (wordLen === 0) {
return [];
}
var startPosition = 0, position = [], index = [];
if (!caseSensitive) {
text = text.toLowerCase();
word = word.toLowerCase();
}
while ((position = text.indexOf(word, startPosition)) > -1) {
index.push({position: position, word: word});
startPosition = position + wordLen;
}
return index;
}
indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
});
if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
isMatch = true;
hitCount = indexOfTitle.length + indexOfContent.length;
}
}
// show search results
if (isMatch) {
// sort index by position of keyword
[indexOfTitle, indexOfContent].forEach(function (index) {
index.sort(function (itemLeft, itemRight) {
if (itemRight.position !== itemLeft.position) {
return itemRight.position - itemLeft.position;
} else {
return itemLeft.word.length - itemRight.word.length;
}
});
});
// merge hits into slices
function mergeIntoSlice(text, start, end, index) {
var item = index[index.length - 1];
var position = item.position;
var word = item.word;
var hits = [];
var searchTextCountInSlice = 0;
while (position + word.length < = end & & index.length != 0) {
if (word === searchText) {
searchTextCountInSlice++;
}
hits.push({position: position, length: word.length});
var wordEnd = position + word.length;
// move to next position of hit
index.pop();
while (index.length != 0) {
item = index[index.length - 1];
position = item.position;
word = item.word;
if (wordEnd > position) {
index.pop();
} else {
break;
}
}
}
searchTextCount += searchTextCountInSlice;
return {
hits: hits,
start: start,
end: end,
searchTextCount: searchTextCountInSlice
};
}
var slicesOfTitle = [];
if (indexOfTitle.length != 0) {
slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
}
var slicesOfContent = [];
while (indexOfContent.length != 0) {
var item = indexOfContent[indexOfContent.length - 1];
var position = item.position;
var word = item.word;
// cut out 100 characters
var start = position - 20;
var end = position + 80;
if(start < 0 ) {
start = 0;
}
if (end < position + word . length ) {
end = position + word.length;
}
if(end > content.length){
end = content.length;
}
slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
}
// sort slices in content by search text's count and hits' count
slicesOfContent.sort(function (sliceLeft, sliceRight) {
if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
return sliceRight.searchTextCount - sliceLeft.searchTextCount;
} else if (sliceLeft.hits.length !== sliceRight.hits.length) {
return sliceRight.hits.length - sliceLeft.hits.length;
} else {
return sliceLeft.start - sliceRight.start;
}
});
// select top N slices in content
var upperBound = parseInt('1');
if (upperBound >= 0) {
slicesOfContent = slicesOfContent.slice(0, upperBound);
}
// highlight title and content
function highlightKeyword(text, slice) {
var result = '';
var prevEnd = slice.start;
slice.hits.forEach(function (hit) {
result += text.substring(prevEnd, hit.position);
var end = hit.position + hit.length;
result += '< b class = "search-keyword" > ' + text.substring(hit.position, end) + '< / b > ';
prevEnd = end;
});
result += text.substring(prevEnd, slice.end);
return result;
}
var resultItem = '';
if (slicesOfTitle.length != 0) {
resultItem += "< li > < a href = '" + articleUrl + "' class = 'search-result-title' > " + highlightKeyword(title, slicesOfTitle[0]) + "< / a > ";
} else {
resultItem += "< li > < a href = '" + articleUrl + "' class = 'search-result-title' > " + title + "< / a > ";
}
slicesOfContent.forEach(function (slice) {
resultItem += "< a href = '" + articleUrl + "' > " +
"< p class = \"search-result\" > " + highlightKeyword(content, slice) +
"...< / p > " + "< / a > ";
});
resultItem += "< / li > ";
resultItems.push({
item: resultItem,
searchTextCount: searchTextCount,
hitCount: hitCount,
id: resultItems.length
});
}
})
};
if (keywords.length === 1 & & keywords[0] === "") {
resultContent.innerHTML = '< div id = "no-result" > < i class = "fa fa-search fa-5x" / > < / div > '
} else if (resultItems.length === 0) {
resultContent.innerHTML = '< div id = "no-result" > < i class = "fa fa-frown-o fa-5x" / > < / div > '
} else {
resultItems.sort(function (resultLeft, resultRight) {
if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
return resultRight.searchTextCount - resultLeft.searchTextCount;
} else if (resultLeft.hitCount !== resultRight.hitCount) {
return resultRight.hitCount - resultLeft.hitCount;
} else {
return resultRight.id - resultLeft.id;
}
});
var searchResultList = '< ul class = \"search-result-list\" > ';
resultItems.forEach(function (result) {
searchResultList += result.item;
})
searchResultList += "< / ul > ";
resultContent.innerHTML = searchResultList;
}
}
if ('auto' === 'auto') {
input.addEventListener('input', inputEventFunction);
} else {
$('.search-icon').click(inputEventFunction);
input.addEventListener('keypress', function (event) {
if (event.keyCode === 13) {
inputEventFunction();
}
});
}
// remove loading animation
$(".local-search-pop-overlay").remove();
$('body').css('overflow', '');
proceedsearch();
}
});
}
// handle and trigger popup window;
$('.popup-trigger').click(function(e) {
e.stopPropagation();
if (isfetched === false) {
searchFunc(path, 'local-search-input', 'local-search-result');
} else {
proceedsearch();
};
});
$('.popup-btn-close').click(onPopupClose);
$('.popup').click(function(e){
e.stopPropagation();
});
$(document).on('keyup', function (event) {
var shouldDismissSearchPopup = event.which === 27 & &
$('.search-popup').is(':visible');
if (shouldDismissSearchPopup) {
onPopupClose();
}
});
< / script >
< script src = "https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js" > < / script >
< script > AV . initialize ( "EWwoJgHNdlj6iBjiFlMcabUO-gzGzoHsz" , "x8FxDrYG79C8YFrTww9ljo8K" ) ; < / script >
< script >
function showTime(Counter) {
var query = new AV.Query(Counter);
var entries = [];
var $visitors = $(".leancloud_visitors");
$visitors.each(function () {
entries.push( $(this).attr("id").trim() );
});
query.containedIn('url', entries);
query.find()
.done(function (results) {
var COUNT_CONTAINER_REF = '.leancloud-visitors-count';
if (results.length === 0) {
$visitors.find(COUNT_CONTAINER_REF).text(0);
return;
}
for (var i = 0; i < results.length ; i + + ) {
var item = results[i];
var url = item.get('url');
var time = item.get('time');
var element = document.getElementById(url);
$(element).find(COUNT_CONTAINER_REF).text(time);
}
for(var i = 0; i < entries.length ; i + + ) {
var url = entries[i];
var element = document.getElementById(url);
var countSpan = $(element).find(COUNT_CONTAINER_REF);
if( countSpan.text() == '') {
countSpan.text(0);
}
}
})
.fail(function (object, error) {
console.log("Error: " + error.code + " " + error.message);
});
}
function addCount(Counter) {
var $visitors = $(".leancloud_visitors");
var url = $visitors.attr('id').trim();
var title = $visitors.attr('data-flag-title').trim();
var query = new AV.Query(Counter);
query.equalTo("url", url);
query.find({
success: function(results) {
if (results.length > 0) {
var counter = results[0];
counter.fetchWhenSave(true);
counter.increment("time");
counter.save(null, {
success: function(counter) {
var $element = $(document.getElementById(url));
$element.find('.leancloud-visitors-count').text(counter.get('time'));
},
error: function(counter, error) {
console.log('Failed to save Visitor num, with error message: ' + error.message);
}
});
} else {
var newcounter = new Counter();
/* Set ACL */
var acl = new AV.ACL();
acl.setPublicReadAccess(true);
acl.setPublicWriteAccess(true);
newcounter.setACL(acl);
/* End Set ACL */
newcounter.set("title", title);
newcounter.set("url", url);
newcounter.set("time", 1);
newcounter.save(null, {
success: function(newcounter) {
var $element = $(document.getElementById(url));
$element.find('.leancloud-visitors-count').text(newcounter.get('time'));
},
error: function(newcounter, error) {
console.log('Failed to create');
}
});
}
},
error: function(error) {
console.log('Error:' + error.code + " " + error.message);
}
});
}
$(function() {
var Counter = AV.Object.extend("Counter");
if ($('.leancloud_visitors').length == 1) {
addCount(Counter);
} else if ($('.post-title-link').length > 1) {
showTime(Counter);
}
});
< / script >
< script >
(function(){
var bp = document.createElement('script');
var curProtocol = window.location.protocol.split(':')[0];
if (curProtocol === 'https') {
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
}
else {
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
}
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(bp, s);
})();
< / script >
< / body >
< / html >