The MASM Forum

General => The Campus => Topic started by: jimg on November 25, 2018, 08:35:47 AM

Title: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: jimg on November 25, 2018, 08:35:47 AM
The intent is to have only a .asm file, without any .rc file.  I usually create my controls and such in asm code rather than using a resource compiler.


I briefly asked about this in the thread http://masm32.com/board/index.php?topic=7526.msg82341#msg82341 (http://masm32.com/board/index.php?topic=7526.msg82341#msg82341). Sharing values.

Rather than hijacking that thread any further, I'm starting the topic here for us manifest noobs.

TImoVJL said:

Quote from: TimoVJL on November 24, 2018, 11:11:07 PM
search -manifest:embed link.exe commandline option for latest linkers.
OPTION DOTNAME

.drectve SEGMENT INFO DISCARD
db "-subsystem:console "
db 34,"-manifestdependency:type=",39,"Win32",39
db " name=",39,"Microsoft.Windows.Common-Controls",39
db " version=",39,"6.0.0.0",39
db " processorArchitecture=",39,"*",39
db " publicKeyToken=",39,"6595b64144ccf1df",39
db " language=",39,"*",39,34
;db " -manifest:embed "
.drectve ENDS


1. I don't understand the purpose of using a name starting with a dot, is this required?
2. When searching the masm programmers reference manual, I could find no mention of DISCARD as an option to a SEGMENT statement.
3. I've never used a Segment statement, so a lot of this is pretty nebulous to me.
4. How does one use the option manifest:embed to point at the segment?

I'm using polink 8.00.02 in which the help says "MANIFEST[:{NO|EMBED[,ID=#]}]", so I called it with
/SUBSYSTEM:WINDOWS /RELEASE /MANIFEST:EMBED,ID=drectve
    (I also tried it with .drectve and without any ID)
     ( it looks like it might want a number for the ID, but I have no idea what that might be)

but using SigCheck from SysInternalsSuite,  I only got this in all attempts-

F:\WinAsm\Progs\Ascii\withlog\striped\Ascii.exe:
Verified: Unsigned
Link date: 12:27 PM 11/24/2018
Publisher: n/a
Company: n/a
Description: n/a
Product: n/a
Prod version: n/a
File version: n/a
MachineType: 32-bit
Manifest:
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level='asInvoker' uiAccess='false' />
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>


So I'm only getting a stock manifest and not what I put in the code.

Clearly I'm making some noobish mistake here I can't seem to find.

Any enlightenment will be appreciated :)
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: jimg on November 25, 2018, 08:38:32 AM
In response to jj's post-
Quote from: jj2007 on November 25, 2018, 06:04:46 AM
Quote from: jimg on November 24, 2018, 03:49:54 PMI have searched in vain how to get around manifests, how do you do it without a resource file?

Jim,

It's not impossible but impractical, see the Weird problem with UpdateResource (http://masm32.com/board/index.php?topic=5824.0) thread. You can indeed add a resource to an existing exe, but it requires elevation, i.e. you need to insert the admin password every time you build a project as a normal user. At that point, having a standard manifest.rc is much easier.

I'm not concerned about having to enter a password, so I'd still like to do it in code. 
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: hutch-- on November 25, 2018, 10:29:05 AM
Jim,

We all have our own dispositions and while many find having to add a manifest a pain, with practice its easy enough to do if you use a resource compiler, either Pelle's or Microsoft's and with a resource compiler you can also add a version control block. Do both of these properly and you are close to free from crappy AV scanners, many of which will flag your binary as suspicious if it does not find both.

They were one of Microsoft's tricks to trash older programs so the user had to buy a new one. One mod they did made sense, even though it involved having to re-write many programs was the DEP that prevented code being stored and executed as data.

One of the great advantages of using RC.EXE is you can manually edit both the manifest and RC script before it is compiled into a resource and RC.EXE will also read a UNICODE script if you want to use exotic characters or symbols from unusual fonts.
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: jj2007 on November 25, 2018, 10:50:39 AM
I did some tests, and this works fine with polink - and only with polink, the M$ linkers don't recognise the -embed:manifest, even version 14.10 doesn't like it:

include \masm32\MasmBasic\MasmBasic.inc         ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  Inkey "This exe uses common controls version ", ComCtl32$()

  OPTION DOTNAME
  .drectve SEGMENT INFO
        db 34,"-manifestdependency:type='Win32'"
        db " name='Microsoft.Windows.Common-Controls'"
        db " version='6.0.0.0'"
        db " processorArchitecture='*'"
        db " publicKeyToken='6595b64144ccf1df'"
        db " language='*'", 34
        db " -manifest:embed "  ; polink only!
  .drectve ENDS
EndOfCode
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: jimg on November 25, 2018, 11:52:46 AM
Hutch-  Certainly a valid opinion, just not what I'm trying to figure out here.

jj-
using polink-
I found that the db" -manifest:embed" line did nothing.
Without /MANIFEST:EMBED switch on the polink command line, I did not get a manifest embeded.
With this switch, I get the stock manifest I showed above, whether or not I included the db line.
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: sinsi on November 25, 2018, 12:25:01 PM
You can use a link.exe switch, see http://masm32.com/board/index.php?topic=6046.msg64054#msg64054
Not sure of the link version you need though.
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: jj2007 on November 25, 2018, 12:54:19 PM
For me, it works fine without any commandline fumbling. With polink only, though. Which version do you have? Mine is Pelles Linker, Version 8.00.2
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: jimg on November 25, 2018, 02:55:07 PM
Okay, I got it to work.   I had tried so many variations along the way I didn't see a change I made that was keeping it from working.
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: jimg on November 25, 2018, 02:58:13 PM
For those mystified like me, the .drectve sectin is defined in the PE Format specification -
https://docs.microsoft.com/en-us/windows/desktop/Debug/pe-format#the-drectve-section-object-only (https://docs.microsoft.com/en-us/windows/desktop/Debug/pe-format#the-drectve-section-object-only)

QuoteThe .drectve Section (Object Only)

A section is a directive section if it has the IMAGE_SCN_LNK_INFO flag set in the section header and has the .drectve section name. The linker removes a .drectve section after processing the information, so the section does not appear in the image file that is being linked.

A .drectve section consists of a string of text that can be encoded as ANSI or UTF-8. If the UTF-8 byte order marker (BOM, a three-byte prefix that consists of 0xEF, 0xBB, and 0xBF) is not present, the directive string is interpreted as ANSI. The directive string is a series of linker options that are separated by spaces. Each option contains a hyphen, the option name, and any appropriate attribute. If an option contains spaces, the option must be enclosed in quotes. The .drectve section must not have relocations or line numbers.
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: jimg on November 25, 2018, 03:15:48 PM
And thank you TimoVJL.  What you told me was correct from the beginning.  I just fouled something up at the start and floundered for a while :(

Works with Polink.  I don't have any of the later Microsoft linkers to test with.

Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: jimg on November 25, 2018, 04:07:15 PM
Also, it doesn't work with masm 6.14/6.15 as the assembler complains about the INFO entry in the SEGMENT line.  Works with UASM :)
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: jj2007 on November 25, 2018, 08:26:17 PM
Quote from: jimg on November 25, 2018, 03:15:48 PMWorks with Polink.
Quote from: jimg on November 25, 2018, 04:07:15 PMWorks with UASM :)

Two Davids united against Goliath :P
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: hutch-- on November 26, 2018, 12:12:06 AM
Urrrrgh,
When "masm 6.14/6.15" were introduced, manifests were not in common use.  :biggrin:
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: aw27 on November 26, 2018, 12:48:50 AM
Quote from: hutch-- on November 26, 2018, 12:12:06 AM
Urrrrgh,
When "masm 6.14/6.15" were introduced, manifests were not in common use.  :biggrin:
Yeap, only the communist manifest has been invented at the time.  :lol:
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: jimg on November 26, 2018, 01:09:57 AM
Here's an interesting result---

I tried the program on https://www.virustotal.com/#/home/upload (https://www.virustotal.com/#/home/upload)

Without the manifest, three virus checkers complain.
WITH the manifest, fifteen checkers complain.
???
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: jj2007 on November 26, 2018, 01:22:08 AM
Quote from: jimg on November 26, 2018, 01:09:57 AMWithout the manifest, three virus checkers complain.
WITH the manifest, fifteen checkers complain.

The AV sh*t brigade have finally understood that virus writers have learned how to include a manifest :icon_mrgreen:
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: aw27 on November 26, 2018, 01:32:07 AM
There is nothing in a manifest to reassure antivirus that the code is safe. Only code signing does some reassurance.
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: hutch-- on November 26, 2018, 01:43:24 AM
I think you would see the manifest first to know why.  :biggrin:
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: TimoVJL on November 26, 2018, 02:07:30 AM
Quote from: hutch-- on November 26, 2018, 12:12:06 AM
Urrrrgh,
When "masm 6.14/6.15" were introduced, manifests were not in common use.  :biggrin:
Even ml 7.1 didn't know about of SEGMENT INFO and manifest is a linker issue.

EDIT: link 8.0 -> support /MANIFEST options.
EDIT: ml.exe 8.0 (https://www.microsoft.com/en-us/download/details.aspx?id=12654) accept SEGMENT INFO
Title: Re: How do I create a manifest within the .asm file and embed it in the final exe?
Post by: hutch-- on November 26, 2018, 03:27:45 AM
> and manifest is a linker issue

It gets fed through a resource compiler first, this is why you could build exe files with manifests and version control blocks using the very old versions as you just linked the RES file after converting it to an OBJ. To make the point, ML 6.14 and the matching era linker knew nothing about manifests and from memory they were introduced in Vista. The OS and AV scanners expect a manifest and version control block, avoid them at your own risk.  :P