Skip to content

Commit 3966c09

Browse files
committed
more details in pe info module
1 parent 222904f commit 3966c09

File tree

6 files changed

+84
-17
lines changed

6 files changed

+84
-17
lines changed

config.nims

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
import strutils
2-
switch("cincludes", gorgeEx("nimble path libpe").output.strip & "/libpe/libpe/include/libpe")
3-
switch("mm", "orc")
2+
switch("cincludes", gorgeEx("nimble path libpe").output.strip & "/libpe/libpe/include/libpe")

peni.nimble

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "0.3.0"
3+
version = "0.3.2"
44
author = "srozb"
55
description = "PE tool based on libpe (with no S)"
66
license = "MIT"
@@ -10,4 +10,4 @@ bin = @["peni"]
1010

1111
# Dependencies
1212

13-
requires "nim >= 1.6.4, libpe >= 0.3.0, cligen >= 1.5.24, nancy >= 0.1.0, termstyle >= 0.1.0"
13+
requires "nim >= 1.6.4, libpe >= 0.3.2, cligen >= 1.5.24, nancy >= 0.1.0, termstyle >= 0.1.0"

src/nim.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#-d:release
1+
-d:release
22
--mm:orc

src/peni.nim

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,41 @@ import peni/cmdEntropy
88

99

1010
dispatchMulti(
11-
[info, short = {
11+
[info, help = {
12+
"all": "show everything",
13+
"summary": "short summary (default)",
14+
"headers": "headers",
15+
"sections": "sections",
16+
"directories": "directories",
17+
"imports": "imports",
18+
"exports": "exports",
19+
"recursive": "be recursive",
20+
}, short = {
1221
"headers": 'H',
1322
"sections": 'S'
1423
}
1524
],
1625
[grep, help={
1726
"imports": "in imports",
1827
"exports": "in exports",
19-
"pattern": "pattern to match with"
28+
"pattern": "pattern to match with",
29+
"recursive": "be recursive"
2030
}, short={
2131
"imports": 'I',
2232
"exports": 'E'
2333
}],
24-
[hash, short={
34+
[hash, help={
35+
"imphash": "imphash",
36+
"md5": "md5",
37+
"sha1": "sha1",
38+
"sha256": "sha256",
39+
"ssdeep", "ssdeep",
40+
"recursive": "be recursive"
41+
}, short={
2542
"sha256": 'S',
2643
"ssdeep": 'd'
2744
}],
28-
[entropy]
45+
[entropy, help={
46+
"recursive": "be recursive"
47+
}]
2948
)

src/peni/cmdInfo.nim

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import libpe/hdr_optional
44
import libpe/imports
55
import libpe/exports
66
import libpe/hashes
7+
import libpe/sections
8+
import libpe/directories
79
import ctx
810
import output
911
import strformat
@@ -53,7 +55,7 @@ proc printSummary(ctx: var pe_ctx_t) =
5355
for dirType, _ in ctx.directories:
5456
dirs.add $pe_directory_name(dirType)
5557
for sec in ctx.sections:
56-
sects.add $sec.Name
58+
sects.add sec[].getName()
5759
withTable "":
5860
table.add "File Name", ctx.getFilename
5961
table.add "File Size", $ctx.map_size & " bytes"
@@ -130,6 +132,15 @@ proc printOptionalHeaderValues[T: ptr IMAGE_OPTIONAL_HEADER_32 | ptr IMAGE_OPTIO
130132
table.add "Size of image", fmt"{hOpt.SizeOfImage:#x}"
131133
table.add "Size of headers", fmt"{hOpt.SizeOfHeaders:#x}"
132134
table.add "Checksum", fmt"{hOpt.CheckSum:#x}"
135+
table.add "Subsystem required", fmt"{hOpt.CheckSum:#x}"
136+
table.add "Checksum", fmt"{hOpt.CheckSum:#x}"
137+
table.add "DLL characteristics", fmt"{hOpt.DllCharacteristics:#x}"
138+
table.add "DLL characteristics names", fmt"TODO"
139+
table.add "Size of stack to reserve", fmt"{hOpt.SizeOfStackReserve:#x}"
140+
table.add "Size of stack to commit", fmt"{hOpt.SizeOfStackCommit:#x}"
141+
table.add "Size of heap space to reserve", fmt"{hOpt.SizeOfHeapReserve:#x}"
142+
table.add "Size of heap space to commit", fmt"{hOpt.SizeOfHeapCommit:#x}"
143+
133144

134145
proc printOptionalHeader(ctx: var pe_ctx_t) = # TODO: cleanup
135146
let hOpt = pe_optional(addr ctx)
@@ -145,21 +156,60 @@ proc printHeaders(ctx: var pe_ctx_t) =
145156
printOptionalHeader(ctx)
146157

147158
proc printSections(ctx: var pe_ctx_t) =
159+
const validFlags = @[
160+
(IMAGE_SCN_CNT_CODE, "contains executable code"),
161+
(IMAGE_SCN_CNT_INITIALIZED_DATA, "contains initialized data"),
162+
(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "contains uninitialized data"),
163+
(IMAGE_SCN_GPREL, "contains data referenced through the GP"),
164+
(IMAGE_SCN_LNK_NRELOC_OVFL, "contains extended relocations"),
165+
(IMAGE_SCN_MEM_DISCARDABLE, "can be discarded as needed"),
166+
(IMAGE_SCN_MEM_NOT_CACHED, "cannot be cached"),
167+
(IMAGE_SCN_MEM_NOT_PAGED, "is not pageable"),
168+
(IMAGE_SCN_MEM_SHARED, "can be shared in memory"),
169+
(IMAGE_SCN_MEM_EXECUTE, "is executable"),
170+
(IMAGE_SCN_MEM_READ, "is readable"),
171+
(IMAGE_SCN_MEM_WRITE, "is writable")
172+
]
148173
for sec in ctx.sections:
174+
var charNames: string
175+
for flag in validFlags:
176+
if bool(sec.Characteristics and flag[0].uint32):
177+
charNames &= flag[1] & ", "
149178
withTable "Sections":
150-
table.add "Section Name", $sec.Name.bold
179+
table.add "Section Name", sec[].getName.bold
151180
table.add "Virtual Size", fmt"{sec.Misc.VirtualSize:#x}"
152181
table.add "Size Of Raw Data", fmt"{sec.SizeOfRawData:#x}"
153182
table.add "Pointer To Raw Data", fmt"{sec.PointerToRawData:#x}"
154183
table.add "Number Of Relocations", $sec.NumberOfRelocations
155184
table.add "Characteristics", fmt"{sec.Characteristics:#x}"
156-
table.add "Characteristics Names", "TODO"
185+
table.add "Characteristics Names", charNames
157186

158187
proc printDirectories(ctx: var pe_ctx_t) =
188+
const dirNames = @[
189+
(IMAGE_DIRECTORY_ENTRY_EXPORT, "Export Table"),
190+
(IMAGE_DIRECTORY_ENTRY_IMPORT, "Import Table"),
191+
(IMAGE_DIRECTORY_ENTRY_RESOURCE, "Resource Table"),
192+
(IMAGE_DIRECTORY_ENTRY_EXCEPTION, "Exception Table"),
193+
(IMAGE_DIRECTORY_ENTRY_SECURITY, "Certificate Table"),
194+
(IMAGE_DIRECTORY_ENTRY_BASERELOC, "Base Relocation Table"),
195+
(IMAGE_DIRECTORY_ENTRY_DEBUG, "Debug"),
196+
(IMAGE_DIRECTORY_ENTRY_ARCHITECTURE, "Architecture"),
197+
(IMAGE_DIRECTORY_ENTRY_GLOBALPTR, "Global Ptr"),
198+
(IMAGE_DIRECTORY_ENTRY_TLS, "Thread Local Storage (TLS)"),
199+
(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, "Load Config Table"),
200+
(IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT, "Bound Import"),
201+
(IMAGE_DIRECTORY_ENTRY_IAT, "Import Address Table (IAT)"),
202+
(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, "Delay Import Descriptor"),
203+
(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, "CLR Runtime Header"),
204+
(IMAGE_DIRECTORY_RESERVED, "")
205+
]
206+
proc resolve(i: ImageDirectoryEntry): string =
207+
for (dn, descr) in dirNames:
208+
if dn == i: return descr
159209
withTable "Directories":
160210
table.add "Directory Name", "Virtual Address", "Size"
161211
for dirType, dirVal in ctx.directories:
162-
table.add $pe_directory_name(dirType), fmt"{dirVal.VirtualAddress:#x}", $dirVal.Size
212+
table.add resolve(dirType), fmt"{dirVal.VirtualAddress:#x}", $dirVal.Size
163213

164214
proc printImports(ctx: var pe_ctx_t) =
165215
withTable "Imported Functions":
@@ -182,7 +232,7 @@ proc printExports(ctx: var pe_ctx_t) =
182232
proc info*(all = false, summary = true, headers = false, sections = false,
183233
directories = false, imports = false, exports = false, recursive = false,
184234
files: seq[string]) =
185-
## Reads information about PE file.
235+
## Show PE file details.
186236
for c in files.peCtx(recursive=recursive):
187237
var ctx = c
188238
echo fmt"{ctx.path}:".magenta.bold

src/peni/cryptUtils.nim

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import libpe/pe
33

44
proc genHash*(ctx: pe_ctx_t, hType: string): string =
55
var hSize = pe_hash_recommended_size()
6-
var outString = newString(hSize)
7-
if pe_hash_raw_data(outString.cstring, hSize, hType.cstring,
6+
result = newString(hSize)
7+
if pe_hash_raw_data(result.cstring, hSize, hType.cstring,
88
cast[ptr uint8](ctx.map_addr), ctx.map_size.uint):
9-
result = outString
109
result.setLen hSize

0 commit comments

Comments
 (0)