Wings of Hermes – Berin's Infosec Blog

Infosec the world and everything

Browsing Posts published by berin

Parts of the Windows Crypto API are quite well documented (particularly the original CAPI stuff).  Other parts…..

I’ve been trying to get PKCS8 files working between OpenSSL and Windows.  The Import/Export on Windows is so badly documented I eventually resorted to ASN.1 decodes and header file trawls to find what I needed.

Anyway – if you need a password encrypted (using PBE) PKCS8 file that Windows can read, the best you’ll get is PBE-SHA1-3DES.  So once you’ve generated your RSA key (let’s call it rsa.key) then you need to run openssl as follows:

openssl pkcs8 –in ./rsa.key –topk8 –outform DER –out ./key.pk8 –v1 PBE-SHA1-3DES

If you don’t want to have a password – use –nocrypt

Generating something that can go back the other way is just plain difficult.

You need to use NCryptExportKey (BCryptExportKey doesn’t have the capability).  You also need to specify the algorithm OID, parameters and password in the export parameters.

Some code to demonstrate.  It’s messy and it probably won’t compile immediately as I’ve just done a cut and paste – but hopefully it will be useful to someone.

NCryptBufferDesc params, *pparams;
NCryptBuffer buffers[3];
DWORD pkcs8_blob_sz;
unsigned char * pkcs8_blob;
CRYPT_PKCS12_PBE_PARAMS * pbe_params;
unsigned char * salt;

// Generate the parameters
pbe_params = (CRYPT_PKCS12_PBE_PARAMS *)malloc(sizeof(CRYPT_PKCS12_PBE_PARAMS) + 8);
memset(pbe_params, 0, sizeof(CRYPT_PKCS12_PBE_PARAMS) + 8);
salt = (unsigned char *) pbe_params + sizeof(CRYPT_PKCS12_PBE_PARAMS);

// First some random for the salt
if (!NT_SUCCESS(BCryptGenRandom(amp->amp_bcrypt_rng, salt, 8, 0)))
        /* ERROR STUFF HERE */

// Now the params
pbe_params->cbSalt = 8;
pbe_params->iIterations = 2048;

buffers[2].BufferType = NCRYPTBUFFER_PKCS_ALG_PARAM;
buffers[2].cbBuffer = sizeof(CRYPT_PKCS12_PBE_PARAMS) + 8;
buffers[2].pvBuffer = pbe_params;

buffers[1].BufferType = NCRYPTBUFFER_PKCS_ALG_OID;
buffers[1].pvBuffer = szOID_PKCS_12_pbeWithSHA1And3KeyTripleDES;
buffers[1].cbBuffer = strlen(szOID_PKCS_12_pbeWithSHA1And3KeyTripleDES) + 1; // Terminator needed

buffers[0].BufferType = NCRYPTBUFFER_PKCS_SECRET;
buffers[0].pvBuffer = L”PASSWORD”; // Yes you need to replace this :)
buffers[0].cbBuffer = 12; /* Include bytes for the wchar terminator */

params.cBuffers = 3;
params.pBuffers = buffers;
params.ulVersion = NCRYPTBUFFER_VERSION;

pparams = &params;

/* Do the export */
if (NCryptExportKey(ncrypt_master_key, 0, NCRYPT_PKCS8_PRIVATE_KEY_BLOB,
        pparams, NULL, 0, &pkcs8_blob_sz, NCRYPT_SILENT_FLAG) != ERROR_SUCCESS)
        /* ERROR CODE HERE */

pkcs8_blob = (unsigned char *)malloc(pkcs8_blob_sz);
if (NCryptExportKey(ncrypt_master_key, 0, NCRYPT_PKCS8_PRIVATE_KEY_BLOB,
        pparams, pkcs8_blob, pkcs8_blob_sz, &pkcs8_blob_sz, NCRYPT_SILENT_FLAG) != ERROR_SUCCESS)
        /* ERROR CODE HERE */


I seem to always add little posts about trivial things that messed me up.

Android studio 1.0.2 – couldn’t work out how to access the libs dir to add a jar file from another project I have.

So simply copy it by hand into the libs directory (under app – same level as src).

Then in Android Studio there is a little dropdown on the left hand top side of the project window. By default it says Android with a little droid symbol. Click on it and you get other options. Select “Project” and you can now see all the directories and can right click your library and “add as library”. It even does the gradle integration for you.

Very annoying. Mysql jdbc connection, defined in glassfish and Netbeans can’t load it. Again no helpful hints anywhere except bug entries for netbeans.

Turns out if you add a “driverClass” property to the connection pool with value “com.mysql.jdbc.Driver” the problem goes away.

Obvious really….

Just ran into a great error on upgrading to Glassfish 4.  Couple of references to it from Google but no solution.

Glassfish log:

[2013-12-22T07:02:51.014+1100] [glassfish 4.0] [SEVERE] [] [javax.enterprise.system.core] [tid: _ThreadID=36 _ThreadName=admin-listener(4)] [timeMillis: 1387656171014] [levelValue: 1000] [[
  Exception while loading the app : CDI deployment failure:WELD-001301 Annotation @org.glassfish.api.admin.RestEndpoints(value=[@org.glassfish.api.admin.RestEndpoint(description=disable-secure-admin, path=disable-secure-admin, opType=POST, params=[], useForAuthorization=false, configBean=interface com.sun.enterprise.config.serverbeans.Domain)]) is not a qualifier
org.jboss.weld.exceptions.IllegalArgumentException: WELD-001301 Annotation @org.glassfish.api.admin.RestEndpoints(value=[@org.glassfish.api.admin.RestEndpoint(description=disable-secure-admin, path=disable-secure-admin, opType=POST, params=[], useForAuthorization=false, configBean=interface com.sun.enterprise.config.serverbeans.Domain)]) is not a qualifier

Turns out I had some modules from Glassfish included into a library in the project so I could compile some EJBs.  Turns out as well that including that library in the deployed EAR is a bad idea :).

Simply told Netbeans not to package it in the Project Properties -> Libraries page and all was good again.

Simple error – only put it here because I couldn’t find anything on the net.  Had to do the hard yards myself.  Hate that!

Oh yes – and you have to clean and build otherwise the library doesn’t get cleaned out from the build directory.  (I know it’s obvious – but it messed me up for a while.)

I’ve been messing around with the Windows LSA and implementing my own authentication package. It’s all part of an authentication server I’m building.  The *NIX component (a PAM module) was pulled together in an afternoon.

Windows is a slightly different story…..

As always, MSDN has great documentation on the APIs, but there is not a lot of other reference material out there.  I did find an article on building an LSA proxy which was some help, but there was no real source code to work from.

For anyone looking to implement an SSP/AP, a couple of things to remember…

  • Use a virtual machine with nothing on it bu Windows for testing.  Make sure you have a Restore Point – you will break the boot process frequently!
  • Remember to copy the Visual Studio shared runtime library over to \windows\System32 on your build system. (OK should have been obvious 🙂 ).  When LSASS.EXE can’t load a DLL it just ignores it quietly.  No error, no nothing.
  • Build the right architecture for your test system (yeah – again obvious…)

There were some other things I found interesting.  The API talks about a function table that both the Sp and the Ap functions should be registered in.  Interestingly MSV1_0 (the NTLM authentication library) doesn’t include all the Ap functions in the table.  So you need to find them in the DLL exports.  Seems to be a hangover from old implementation I guess.

Anyway – in the interests of helping others out there, the source codefor a basic proxy can be found here.

It’s got the VS 2012 project in there.  Just compile it, copy the DLL to \Windows\System32 on your test system and then add the library name to the list of library names in the following registry key:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\Security Packages

Hope it’s of use to someone.  The Pwitc name is the name of the authentication server I’m building – just pretend it says "LsassProxy" or somesuch!


I have just spent the arvo playing with IPv6 and brought up a tunnel using my web server as the tunnel endpoint in my network.

Next thing I know every machine on the network (OS X/Windows/Linux) have been allocated IPv6 addresses in the /56 network I requested from the broker.  Turns out I activated the routing functionality and all the machines on the network received a broadcast routing update.  IPv6 has a thing called stateless autoconfiguration that the devices then use to grab an IP address (based off the MAC address).

And because the tunnel goes through the firewall, everything is visible on the Internet (found a nice site that lets you port scan IPv6 addresses and verified).

As IPv6 becomes more available, I wonder how many people are going to find themselves opening up their network without realising it?  I wonder also if the bad guys have started scanning IPv6 address ranges – or is it still not worth the effort.  And if so – for how long.

Now playing with ip6tables before I bring that tunnel back up…..

I just upgraded from Glassfish 3.1 to 3.1.1 and all logging for my app below INFO (i.e. FINE/FINER/FINEST) stopped working.  Turns out there is a bug that normally expresses itself by stopping logging working altogether.  I think another symptom is glassfish starts to ignore logging if the level for the particular logger is set to FINE or below.

Anyway – fix seems to be to run the command:

asadmin set-log-levels com.sun.enterprise.server.logging.GFFileHandler=ALL

For more info see the 3.11 Release Notes

Well – JAAS and GlassFish. I naively assumed this would be easy to get to work. It’s not.

I started out by building a login module that extended AppservPasswordLoginModule and plugging it directly into login.conf. I then created a Realm that used it and continued on my merry way.

The moment I tried to extend beyond a simple user name and password (being passed through vi the j_security_check form) things broke down.

It turns out that the Realms in GlassFish don’t allow you to plug in “any” JAAS compliant LoginModule. In fact they seem to assume that any kind of authentication you want to do relies on username and password. That makes sense as the realm handles the user input, so if you want to deviate from that standard, you need to go outside what the realm can handle.

In the end, I ended up implementing a JSR 196 SAM (ServerAuthModule). This in turn was an interesting exercise – much of the stuff that GlassFish looks after for you in configuration now needs to be handled directly. For example detecting a not logged in user and redirecting to a login page and then redirecting back when the auth occurs. My SAM plugs direct into GlassFish and I no longer use the realm for those parts of the app with my customised authentication process.

Very interesting. But I now have a SAM that operates as a login bridge and calls a JAAS LoginModule implementation for my authentication.

Patrick Hogan has done a great project on GitHub – IOS Certificate Key and Trust Sample Project – that pulls together all the concepts for RSA key importing/exporting and handling. It’s used some of the code from my import and export posts + code from a few other places together with his own work. Great single place to go for guidance in this space.

Awesome! Wish I’d thought of it :).

One of the biggest issues with security awareness is knowing what it is you are trying to achieve. It’s not enough to just run an awareness program – you have to be trying to drive an outcome.

Over a period of time I have defined in my own head a kind of CMM for security awareness. It’s something that helps me think about how mature is an organisation in its security awareness culture, which in turn helps me guide an awareness program to continually improve that culture.

It’s not really a CMM in the sense of measuring maturity process – it’s more looking at the output of all the awareness processes than measuring the processes themselves. But it’s a nice analogy that works for me.

A good CMM always has five levels (paraphrasing from the wikipedia article referenced above) :


  1. Initial – ad-hoc process
  2. Repeatable – Trying to do the same thing every time
  3. Defined – Documented as a standard business process
  4. Managed – Good metrics that measure success of the process
  5. Optimising – ongoing improvement

To make use of this, I try to put myself in the shoes of Fred Bloggs, some person in the organisation (not in the Information Security team) and I measure culture using statements to represent the way Fred is thinking:

  • Initial – “I know there is a security team and they’ll look after all that security stuff. I don’t need to do anything”.
  • Repeatable – “I’ll drag the information security person into this conversation – he/she will worry about the security requirements”.
  • Defined – “I know what the security requirements are, but I’ll drag the security team in anyway as they will do the ‘fighting’ for what is required”.
  • Managed – “I know the security requirements and I’ll fight for them because we need them. I’ll use the security team for final sign-off or where I have to escalate”.
  • Optimising – Security requirements are just inherent in everything people do.

Of course the above is quite a naive way of looking at things in some ways, but a key part of an awareness program is understanding where you are and what you are trying to achieve. By knowing roughly where your organisation culture sits and what you want to take it to, you can start to taylor your security awareness program to achieve that next step.

One last thing to note – different parts of an organisation will be at different levels, and probably should be. Having an IT team that is higher up the scale than the rest of the organisation might not be a bad place for the company you are looking at. And of course that’s another key part of awareness – understanding there are different messages and results that you want for different groups.