SafeArrayGetVartype


Author: David Zimmer
Date: 09.12.19 - 8:22pm



Tonight's tip comes from Mark Johnstone who was doing research into how safe arrays can store type information. The safe array structure itself does not have an internal type field.

In my vbdec data viewer form I was giving the user the choice on how to decode the data based on element size, essentially brute forcing it and going on intution base on if displayed values made sense, Not the best approach! lol

Mark found an undocumented pre-structure data blob that can contain type information.
Awesome find and thanks for sharing the hard fought research Mark!

Its pretty safe to say I would have never stumbled across this. (Had never even heard of SafeArrayGetVartype yet)

Here is Marks code: ManagedCharSafeArray.cls along with some links that turned up in the course of his research: ReactOS Safearray Source, and Where is SAFEARRAY var type stored?.

I will mirror the key bits of the stack overflow info here:

Where is SAFEARRAY var type stored?

I'd like to know about SAFEARRAY implementation.

It seems to me that there's no field in SAFEARRAY structure
 that is used for storing element type information, such as
 VT_I4(3) or VT_R4(4), but SafeArrayGetVartype function 
returns the correct type.

Somebody commented on the MSDN page below saying that the 
high word of the cLocks holds the type info: SAFEARRAY 
structure on MSDN

But when I passed Long and Single arrays from VBA to a DLL 
function via a type libray, those arrays' fFeatures are
 both 0x80, cLocks are both 0, and stll SafeArrayGetVartype 
can tell VT_I4(3) and VT_R4(4).

asked Sep 13 '13 at 10:49 - Fumito Hamamura

--------------------------------

Depending on how the safearray was created, the variant type
 may be stored in memory before (at the offset -4 from the 
start of) the SAFEARRAY structure. FADF_HAVEVARTYPE flag 
in fFeatures indicates whether the type is available.

Similarly, FADF_HAVEIID indicates that the GUID (see 
SafeArrayCreateEx) is stored at offset of -16, and 
available via SafeArrayGetIID. FADF_HAVEVARTYPE and 
FADF_HAVEIID can never be present simultaneously 
(because otherwise the VARTYPE and the GUID would overlap
 in memory), but SafeArrayGetVartype is smart enough to 
synthesize one of VT_RECORD, VT_DISPATCH or VT_UNKNOWN 
types when it sees corresponding feature flags.

answered Sep 13 '13 at 12:55 - Igor Tandetnik

--------------------------------------

Thank you for the clear answer. So, to summarize, for 
the fFeatures field, 
A) FADF_HAVEVARTYPE or 
B) FADF_HAVEIID or 
C) FADF_RECORD can be set. 

For A) 4, for B) 16, for C) 4 bytes before SAFEARRAY are 
used to carry supplemental information about the SAFEARRAY. 

Only one of the A, B or C can be set at a time. – 

Fumito Hamamura Sep 13 '13 at 14:08






Comments: (0)

 
Leave Comment:
Name:
Email: (not shown)
Message: (Required)
Math Question: 33 + 2 = ? followed by the letter: X 



About Me
More Blogs
Main Site
Posts: (year)
2023 (4)
     Yara Workbench Automation
     VS linker versions
     IDA decompiler comments
     DispCallFunc
2022 (5)
     VB6 Implements
     VB6 Stubs BS
     VB6 TypeInfo
     VB6 VTable Layout
     Yara isPCode rule
2021 (2)
     rtcTypeName
     VB6 Gosub
2020 (5)
     AutoIT versions
     IDA JScript 2
     Using VB6 Obj files from C
     Yara Corrupt Imports
     Yara Undefined values
2019 (6)
     Yara WorkBench
     SafeArrayGetVartype
     vb6 API and call backs
     PrintFile
     ImpAdCallNonVirt
     UConnect Disable Cell Modem
2017 (5)
     IDA python over IPC
     dns wildcard blocking
     64bit IDA Plugins
     anterior lines
     misc news/updates
2016 (4)
     KANAL Mod
     Decoders again
     CDO.Message Breakpoints
     SysAnalyzer Updates
2015 (5)
     SysAnalyzer and Site Updates
     crazy decoder
     ida js w/dbg
     flash patching #2
     JS Graphing
2014 (5)
     Delphi IDA Plugin
     scdbg IDA integration
     API Hash Database
     Winmerge plugin
     IDACompare Updates
2013 (9)
     Guest Post @ hexblog
     TCP Stream Reassembly
     SysAnalyzer Updates
     Apilogger Video
     Shellcode2Exe trainer
     scdbg updates
     IDA Javascript w/IDE
     Rop Analysis II
     scdbg vrs ROP
2012 (13)
     flash patching
     x64 Hooks
     micro hook
     jmp api+5 *2
     SysAnalyzer Updates
     InjDll runtime config
     C# Asm/Dsm Library
     Shellcode Hook Detection
     Updates II
     findDll
     Java Hacking
     Windows 8
     Win7 x64
2011 (19)
     Graphing ideas
     .Net Hacking
     Old iDefense Releases
     BootLoaders
     hll shellcode
     ActionScript Tips
     -patch fu
     scdbg ordinal lookup
     scdbg -api mode
     Peb Module Lists
     scdbg vrs Process Injection
     GetProcAddress Scanner
     scdbg fopen mode
     scdbg findsc mode
     scdbg MemMonitor
     demo shellcodes
     scdbg download
     api hashs redux
     Api hash gen
2010 (11)
     Retro XSS Chat Codes
     Exe as DLL
     Olly Plugins
     Debugging Explorer
     Attach to hidden process
     JS Refactoring
     Asm and Shellcode in CSharp
     Fancy Return Address
     PDF Stream Dumper
     Malcode Call API by Hash
     WinDbg Cheat Sheet
2009 (1)
     GPG Automation