X86/X64 on Windows ARM build

** This are some notes i have taken/prepared few years back while doing some research on x86/64 bit support on windows arm build. I couldnot finish it and then never got time to work on it again, this may be useful for someone.**

windows 10 supported x86 on arm
windows 11 added support for x64 as well

from: https://learn.microsoft.com/en-us/windows/win32/winprog64/wow64-implementation-details

WOW64 is an emulator which allows 32 bit windows apps as well as 32 bit arm application in 64bit arm windows.
wow64 emulator runs in user mode and it provides interfaces between 32 bit version of ntdll.dll and the kernel of proceccsor and intercepts kernel calls.
following are the dlls used for this:
Wow64.dll provides the core emulation infrastructure and the thunks for the Ntoskrnl.exe entry-point functions.
Wow64Win.dll provides thunks for the Win32k.sys entry-point functions.
(x64 only) Wow64Cpu.dll provides support for running x86 programs on x64.
(Intel Itanium only) IA32Exec.bin contains the x86 software emulator.
(Intel Itanium only) Wowia32x.dll provides the interface between IA32Exec.bin and WOW64.
(ARM64 only) xtajit.dll contains the x86 software emulator.
(ARM64 only) wowarmw.dll provides support for running ARM32 programs on ARM64.

These DLLs, along with the 64-bit version of Ntdll.dll, are the only 64-bit binaries that can be loaded into a 32-bit process. On Windows 10 on ARM, CHPE (Compiled Hybrid Portable Executable) binaries may also be loaded into an x86 32-bit process.

At startup, Wow64.dll loads the x86 version of Ntdll.dll (or the CHPE version, if enabled) and runs its initialization code, which loads all necessary 32-bit DLLs.
Almost all 32-bit DLLs are unmodified copies of 32-bit Windows binaries, though some are loaded as CHPE for performance reasons. Some of these DLLs are written to behave differently on WOW64 than they do on 32-bit Windows, usually because they share memory with 64-bit system components. All user-mode address space above the 32-bit limit is reserved by the system.

Instead of using the x86 system-service call sequence, 32-bit binaries that make system calls are rebuilt to use a custom calling sequence. This calling sequence is inexpensive for WOW64 to intercept because it remains entirely in user mode. When the custom calling sequence is detected, the WOW64 CPU transitions back to native 64-bit mode and calls into Wow64.dll. Thunking is done in user mode to reduce the impact on the 64-bit kernel and to reduce the risk of a bug in the thunk that might cause a kernel-mode crash, data corruption, or a security hole. The thunks extract arguments from the 32-bit stack, extend them to 64 bits, then make the native system call.

Registry Redirector – isolates 32-bit and 64-bit applications by providing separate logical views of certain portions of the registry on WOW64. The registry redirector intercepts 32-bit and 64-bit registry calls to their respective logical registry views and maps them to the corresponding physical registry location. The redirection process is transparent to the application. Therefore, a 32-bit application can access registry data as if it were running on 32-bit Windows even if the data is stored in a different location on 64-bit Windows.

Redirected keys are mapped to physical locations under Wow6432Node. For example, HKEY_LOCAL_MACHINE\Software is redirected to HKEY_LOCAL_MACHINE\Software\Wow6432Node. However, the physical location of redirected keys should be considered reserved by the system. Applications should not access a key’s physical location directly, because this location may change. For more information, see Accessing an Alternate Registry View.

Windows 10 on ARM: Redirected 32-bit ARM keys are mapped to physical locations under WowAA32Node.

To help 32-bit applications that write REG_SZ or REG_EXPAND_SZ data containing %ProgramFiles% or %commonprogramfiles% to the registry, WOW64 intercepts these write operations and replaces them with “%ProgramFiles(x86)%” and “%commonprogramfiles(x86)%”. For example, if the Program Files directory is on the C drive, then “%ProgramFiles(x86)%” expands to “C:\Program Files (x86)”. The replacement occurs only if the following conditions are met:

The string must begin with %ProgramFiles% or %commonprogramfiles%. If the string begins with a space or any character other than %, it is not replaced.
The case of %ProgramFiles% or %commonprogramfiles% must be exactly as shown because the string comparison is case-sensitive. For example, if the string begins with %CommonProgramFiles% instead of %commonprogramfiles%, it is not replaced.
The string cannot exceed MAX_PATH*2+15 characters. If it exceeds this length, it is not replaced.
The key cannot be opened with KEY_WOW64_64KEY. This flag specifies that operations on the key should be performed on the 64-bit registry view, so it is not replaced. For more information, see Accessing an Alternate Registry View.

File System Redirector – The %windir%\System32 directory is reserved for 64-bit applications on 64-bit Windows. Most DLL file names were not changed when 64-bit versions of the DLLs were created, so 32-bit versions of the DLLs are stored in a different directory. WOW64 hides this difference by using a file system redirector.

In most cases, whenever a 32-bit application attempts to access %windir%\System32, %windir%\lastgood\system32, or %windir%\regedit.exe, the access is redirected to an architecture-specific path.

Original Path Redirected Path for 32-bit x86 Processes Redirected Path for 32-bit ARM Processes
%windir%\System32 %windir%\SysWOW64 %windir%\SysArm32
%windir%\lastgood\system32 %windir%\lastgood\SysWOW64 %windir%\lastgood\SysArm32
%windir%\regedit.exe %windir%\SysWOW64\regedit.exe %windir%\ SysArm32\regedit.exe

Arm 32 bit and 64 bit

Arm64x PE files which contains arm64 bit code and arm64EC code togehter.
EC= emulation compatible

can use a function is IsWow64Process2
BOOL IsWow64Process2(
[in] HANDLE hProcess,
[out] USHORT *pProcessMachine,
[out, optional] USHORT *pNativeMachine
);

outputl values wil be
IMAGE_FILE_MACHINE_ARM

0x01c0

ARM Little-Endian

IMAGE_FILE_MACHINE_THUMB

0x01c2

ARM Thumb/Thumb-2 Little-Endian

IMAGE_FILE_MACHINE_ARMNT

0x01c4

ARM Thumb-2 Little-Endian

IMAGE_FILE_MACHINE_ARM64

0xAA64

ARM64 Little-Endian

from: https://learn.microsoft.com/en-us/windows/arm/arm64ec?source=recommendations
arm64ec – Arm64EC (“Emulation Compatible”) enables you to build new native apps or incrementally transition existing x64 apps to take advantage of the native speed and performance possible with Arm-powered devices, including better power consumption, battery life, and accelerated AI & ML workloads.

Arm64EC is a new application binary interface (ABI) for apps running on Arm devices with Windows 11. It is a Windows 11 feature that requires the use of the Windows 11 SDK and is not available on Windows 10 on Arm.

Interoperability
Code built as Arm64EC is interoperable with x64 code running under emulation within the same process. The Arm64EC code in the process runs with native performance, while any x64 code runs using emulation that comes built-in with Windows 11. Even if your app relies on existing dependencies or plugins that don’t yet support Arm, you can start to rebuild parts of your app as Arm64EC to gain the benefits of native performance.

Arm64EC guarantees interoperability with x64 by following x64 software conventions including calling convention, stack usage, data structure layout, and preprocessor definitions. However, Arm64EC code is not compatible with code built as Arm64, which uses a different set of software conventions.

The Windows 11 on Arm operating system itself relies heavily on Arm64EC’s interoperability to enable running x64 applications. Most operating system code loaded by an x64 app running on Windows 11 on Arm will have been compiled as Arm64EC, enabling native performance for that code without the application knowing.

An x64 or Arm64EC process can load and call into both x64 and Arm64EC binaries, whereas an Arm64 process can only load Arm64 binaries. Both architectures can load Arm64X binaries as those contain code for both x64 and Arm64.

Any x64 code, including code from dependencies, in an Arm64EC process will run under emulation in your app. Prioritizing the most CPU-intensive dependencies to transition from x64 to Arm64EC will have the greatest impact toward improving your app’s performance.

Identifying Arm64EC binaries and apps – For developers interested in identifying these binaries, you can see them in a developer command prompt using link /dump /headers

File Type: EXECUTABLE IMAGE
FILE HEADER VALUES
8664 machine (x64) (ARM64X)

The combination of (x64) and (ARM64X) indicates that some portion of the binary has been recompiled as Arm64EC, even though the binary still appears to be x64. A binary with a machine header that contains (ARM64) and (ARM64X) is an Arm64X PE file that can be loaded into both x64 and Arm64 apps.

Windows Task Manager can also be used to identify if an app has been compiled as Arm64EC. In the Details tab of Task manager, the Architecture column will show ARM64 (x64 compatible) for applications whose main executable has been partially or completely compiled as Arm64EC.

from https://learn.microsoft.com/en-us/windows/arm/arm64ec-abi:
It also implies that any Arm64 registers which cannot be fitted into the x64 CONTEXT must not be used, as their values can be lost anytime an operation using CONTEXT occurs (and some can be asynchronous and unexpected, such as the Garbage Collection operation of a Managed Language Runtime, or an APC).

The mapping rules between Arm64EC and x64 registers are represented by the Arm64EC_NT_CONTEXT structure in the Windows headers, present in the SDK. This structure is essentially a union of the CONTEXT structure, exactly as it is defined for x64, but with an extra Arm64 register overlay.

For example, RCX maps to X0, RDX to X1, RSP to SP, RIP to PC, etc. We can also see how the registers x13, x14, x23, x24, x28, v16-v31 have no representation and, thus, cannot be used in Arm64EC.

This register usage restriction is the first difference between the Arm64 Classic and EC ABIs.

My training on fuzzing at TyphoonCon23

About the conference

I had given a 3 days training at TyphoonCon23 which is an offensive security conference organised each year. This time it was organised in Seoul, South Korea. I had to travel to Seoul for this conference. It was a well organised conference and everything has been taken care with full attention to details. All the attendees and speakers got nice Swags too 🙂

About the City

I really liked Seoul, It is very developed city, I was very impressed with the infrastructure, people and atmosphere. From landing at the airport to reaching at my hotel, it was a smooth experiance.

I took a bus which was actually a limousine bus. It starts from the airport terminal and dropped just in front of my hotel which was Marriott, Namdemun.

Hotel and Location

Hotel is situated at Namdemun and the location is very nice. Its very close to famous namdemun market. If you are at this location, you can walk around and explored few places. Also commuting using public transport is a breez. You can get a T-One card from any shop and load some money. Note that to buy T-One card, you can use credit card but to load money, they need cash only. So better to take out some cash from ATM.

There are bus stop near by, Metro station and more there is underground market as well from where yo u can buy some stuff for yourself or family.

Conference Training Experience

For my training we had 11 people. It was a 3 day long training and I really enjoyed delivering this course. few people had lot of knowledge on fuzzing and I enjoy talking to them and listening and sharing experience with them on fuzzing. My training was mostly focused on Linux and I covered variety of topics from How does AFL++ works, coverage guided fuzzing, binary instrumentation and every had lot of hands on exercises for attendees so that people can not only learn theory but try the approach themselves and ask me any questions.

I enjoyed answering their questions and helping them solve their queries. Overall feedback was very positive and I am looking forward to visit next year as well.

Food

People there mostly eat non-veg so me being vegetarian it was a bit of challenge but there are many small hotels near by who has vegetarian options like pizza, fries etc. there was also subway which has veg options. I also took some ready to eat food items from here and some home made stuff so it was not a big issue for me.

Travelling back

I booked Air India flight from Delhi-Seoul-Delhi. I had to travel from Bangalore to Delhi first and then to Seoul. Also while coming back I had to travel to Delhi and come back to Bangalore.

While my onward journey was fine, on my return journey my Air india flight from Seoul to Delhi was 3.30 hours late which led to miss my flight back to Bangalore. I had to rebook another flight as they were not on same PNR. I also found that my flight from Seoul landed at Delhi T3 but my Bangalore flight is from Delhi T1. So I had to take a bus from T3 to T1 in a bus. Bus driver was not driving but seems like he was flying on Delhi Roads 😉 I reached back on time at T1 and did my checkin.

Finally I managed to reach back to Bangalore on same day even though I was too much exhausted.

Conclusion

Overall it was a great experience travelling to Seoul and delivering training. It was nice to meet many people and make new friends. I am looking forward to delivering training at more conferences in future till then good bye!

My First Experience at Defcon30

Since i know about defcon when i was fresh out of college, i had a dream of speaking there one day.

I never thought it will take me 17 years in my career to be able to speak at defcon.

How it started?

I started speaking at conferences since 2020, been doing lot of workshops on fuzzing. I also started creating lot of videos on youtube about fuzzing. One day one of the person watching my videos, approached me about defcon29 and said why don’t i submit my workshop there. I like the idea and so i decided to go ahead and submit my workshop.

Luckily my workshop was selected at defcon29:

https://forum.defcon.org/node/237326

I was very excited but to my surprise, it was covid time and travel was restricted to USA from India. Which means i can not travel for my workshop and there was no option to have virtual workshop. So i had to pass that on with the hope that next year(in 2022) hopefully i am going to make it and can travel.

Submitting Again

So at the start of 2022, when call for workshop for Defcon30 opened, i decided to submit again.

Fast forward, in June 2022 i received the confirmation that my workshop has been selected for Defcon30.

The Experience

I travelled to vegas, and there was lot of interest in my workshop. I also got some stickers printed to distribute over there. Having stickers with you is a good way to strike a conversation with various people.

My workshop went well, there were lot of people from different background. They asked me lot of questions, queries and were very supportive and encouraged me. Instead of powerpoint slides, i used google codelabs for the workshop contents. This makes it easy for attendees to follow along. I also created one VM which i shared with attendees. This VM contents all the tools and softwares which were required for my workshop.

I covered different types of vulnerabilities, various theory part about fuzzers and had lot of exercises for different part of workshop, I was so happy to see that attendees were able to work on all the exercises and fuzz softwares with AFL in the end.

This was one of the best workshop experience i had. Also this was one of the first in person workshop i conducted after covid time.

Gratitude

I would like to thank Defcon for giving me this opportunity to conduct my workshop, also would like to thanks all the attendees for their support, encouragement and enthusiasm for my workshop.

Contents for my workshop

If you want to know more about fuzzing, i have uplaoded my workshop contents at the link given below, it also has a VM download link for the workshop. you can download the VM and follow along the contents:

http://fuzzing.in/codelabs/finding_security_vulnerabilities/index.html?index=..%2F..index#0

I hope to submit my workshop at more conferences in future, if you are coming along, i would be happy to meet!

Thanks

Hardik

My fuzzing workshop contents and video from Texas Cyber Summit 2021

I was invited to speak at Texas Cyber Summit 2021 and I conducted a workshop on “Fuzzing open source softwares with AFL”.

Following is the youtube video recording for my workshop:

If are interested in contents, you can visit: http://fuzzing.in/

Hope you will like it and learn something from it.

My fuzzing workshop video from grayhat con 2020

here is my fuzzing workshop video from GrayHat con 2020. its 3 hours+ contents. I covered fuzzing on Linux with AFL as well as fuzzing on windows with winafl. i will recommend you to go through it if you are interested in fuzzing. you can also subscribe to my youtube channel where i talk about fuzzing and other security related topics here:

https://youtube.com/c/MrHardik05