Thursday, April 08, 2010, 15:54

Story with SX76x config file

So checked deeper in active_configfile router makes when configuration saved, there is really nothing readable in it (no wonder as it is crypted by router's aes key) once the device breached easy to get from it still some wild reason praticing with signing tool (ssign) was always resulted with "Mismatch between calculated signature and signfile (signfile)", started to practice a bit with signing part it was successful but realized result was not like active_configfile saved from router as it had a header. So wrote a simple C application to split it to signfile and encrypted_configfile.

#include "fileheader.h"
#include <stdio.h>

int main()
{
FILE_HEADER a;
FILE *fi,*fo,*fo2;
char bigbuff[1024000];
unsigned long siker;

fi=fopen("active_configfile","rb");
fo=fopen("signfile","wb");
fo2=fopen("encrypted_configfile","wb");

fread(&a,sizeof(a),1,fi);
printf("%s\n",a.signature);
printf(a.fileName);
fwrite(a.signature,1,32,fo);
success=fread(&bigbuff,1,sizeof(bigbuff),fi);
fwrite(bigbuff,1,success,fo2);
fclose(fi);
fclose(fo);
fclose(fo2);
};

 but after getting signature and encrypted_configfile out was still getting "Mismatch between calculated signature and signfile (signfile)".

 

Was think it might be me extracting wrong parts, but got the very same error when tried to validate the the test files i was signing so looked into ssign's source code  (what was available on Siemens site before (not anymore) and if anyone have SX76x_OSS_052_010.b.tar tarball (what was containing toolchain would be happy to take a copy).

First thing i done is removed signature checking to see what is the result if let it try to decode configfile even with bad signature, that resulted a perfect readable decrypted configfile, but well signature checking is not there for joke wondered why ssign computed different SHA256 hash then what stored in saved configfile. Found that there is a variable called size what would refer to encrypted_configfile size and some reason it is 0 so no sha generation happened because if filesize<buffersize readsize becomes filesize what is 0, well to fix it with haste added two lines and recompiled.

+   fseek(infile,0,SEEK_END); +   size=ftell(infile);     fseek(infile, offset, 0);

    //-------------------------------------------------
    // Generate a SH-256 secure hash over the file
    //-------------------------------------------------
    // Only the first 'check_size' number of bytes of each block
    // of size 'block_size' is used to generate the hash
    unsigned char sha256sum[32];
    sha256_context ctx;
    sha256_starts(&ctx);
    unsigned char * buffer = new unsigned char[block_size];
    size_t numRead;
    
    int readsize = block_size;
    
    if(size < block_size)
        readsize = size;
    int totalread = 0;
    printf ("bs: %d, rs: %d\n", numRead,readsize);   
    while( ( numRead = fread( buffer, 1, readsize, infile ) ) > 0 )
    {
        if ( numRead > check_size )
        {
    printf ("%d, %d\n", numRead,check_size);   
            sha256_update(&ctx, buffer, (unsigned long)check_size);
        }
        else
        {
    printf ("V:%d, %d\n", numRead,check_size);   
            sha256_update(&ctx, buffer, (unsigned long)numRead);
        }
    totalread += numRead;
    printf("read %d",totalread);
    
    if ((totalread + block_size) < size)
        readsize = block_size;
    else
        readsize = size - totalread;
    printf("readsize %d",readsize);    
    }
    sha256_finish( &ctx, sha256sum );
    delete [] buffer;

    //-------------------------------------------------
    // Close input file
    //-------------------------------------------------
    fclose(infile);

    //-------------------------------------------------
    // Compare the calculated and decrypted hash
    //-------------------------------------------------
    if ( memcmp(sha256sum, decrypted_sha256sum, 32) )
    {

        return SSIGN_SIGNATURE_VERIFICATION_ERROR;
    }

After this the stuff i signed validated back successfully, stuff saved from router still telling SIGNATURE VERIFICATION ERROR then used example, remembered there was some shifting examples in ssign source so tried those values:

ssign -v -s signfile -c 0x10000 -b 0x10000 -f hopefully_decrypted_configfile -a aes.key encrypted_configfile

encrypted_configfile,0,signfile,0,,65536,65536,hopefully_decrypted_configfile,4266312
bs: 1245104, rs: 65536
V:65536, 65536
read 65536readsize 65536V:65536, 65536
read 131072readsize 25984V:25984, 65536
read 157056readsize 0
Verification was successful!

And successfully validated now:

				encrypted_configfile:

GóK(ŐŁ;˜ząI»*’/»|˛Đµ»ý‰5Đ–éŃEĚGU|&+‹ćĎ‚–ź,Ôß
ź¨a^ţOô‹ôĎlˇ…ľ¨_âPd÷ĚU€LoĹßTăŚä(E¬Çl?Ű–5ˆ;vBÚ1ÖZ"ä)OVČŐ˝ĆhŔ¤*/4ńś
Ţ]|\)PŔég;UäI˛
7Ţ7#Ď5/ĽťŞ!'ŐgâĂ>Ž–GI&)Ŕ/XÝ@jxÓÁuS%ĽńL~*gîA„5žĂLŐ!ŠůŮţíĎéípÚKMB
G$^sżÄö    Gáz>»Ü<0|
zúi*řz*‡C”¬äť(˜N2Tző]ëÜŹQcµ$ĺ·, LÔÎŻÎż|pyxݛ̬Ą&·đžő% ‹ŃčiĐDJú$
§Ő”ÂW”.G¸«5"Ď‘iâx@vŇů§
€ ëU÷¸źşŃpLVq&h†˛oľ7WÉJw•oXâké1v<üruYĆźŚť˘ů°iP?˘Ţýž*±ź|ÔŕƒcuŐ&
F,â-ęa®)nîFŰč‘(âu
¤"éňť˙`•{PYMÓ—ŞŃ?Óî‚a…†÷-‘=Th
ŰÇ&čş:Hó"<ľÂÓ"®žŞ$íozż!c]R„—Yéź7=tŹÍ¸rŹ=$8G:y$I|Ĺ#ܢSđ¦ş
kF^1ÔŚăw¶%>KkÎr'©’µŇ*ů
ă"-+‡˙n’śk˘în/żD±ńÁ˘ÝJoW
µ¦8ŘßţwL8Ĺ|âjś‹Ž“e»
öľ¨MÁ2úyN ÖĽ¦uI^"´Śˇ-Q1‡Ú,Îś    v0}6‹gźÄ(R.Ó
ĎqÁCă<iJ+¸$*BéÍPh…î¸'ś×~ݵN÷*a˝6‘Ł6«¬ěuĹŞUŹs,łT?_O×BďÄq4đ•ÔČ
އ:/•bé(F¸aÜP@:HwąŢ*ő.Pžµ¬§Ćć-ő˜jŔs~÷A:r
M‚ŮŰŢ%Çďĺü=d&ÍĽśß›çţźRžLÍ—OÚ"¬lĽ&6Ęć$ţÍŻů’ťĄ¬<XpĹŐuĽ    YđÚg܉äý
±†fť€
-kB{Ë6`ëX6©mw‘WŐĄ—>u†Spđëîz[®˝qŕHYĚ"Ú#ś®¬M†C‚i˙ű
*i\Ĺř6÷µţ‡.ÉŐ
'[ŢŠ–~či‘    Śm¤ŢkŠs™/~l±mŇŃt»Gdˆ•Ď8kň
3y\3€µňTm kęńs
W…ě}ż)D{ń÷©úęÜśřkOĺýƒu°µ*VG˝»×ˇ®5ÓáI?‰
ô=¦ő¶ĺ#(M•|"|áŹTâĹ9ń¦3    ęĆLĂçř˝‹Ő,šYç§ Ć[üÔhµ‹
»-˛dóTÄ&´‡oç?ŕĎRţńd­ăÔ
				hopefully_decrypted_configfile:

<o version="2.3.52.6.300" file_type="active" name="IGD" attr="5408">
<o name="V_Logging" attr="5408">
<b name="DefaultConfiguration" attr="6692">0</b>
<i name="SyslogServerAddressMethod" attr="6692">0</i>
<s name="SyslogServerAddress" attr="6692"></s>
<i name="SyslogServerPortNumber" attr="6692">514</i>
<b name="ReportEmergencyToSNMPTrapManager" attr="5408">0</b>
<i name="ReportToSyslogServer" attr="6692">224</i>
<i name="ReportToLocalLogfile" attr="6692">255</i>
<i name="ReportToLocalMaintenanceConsole" attr="6692">248</i>
<i name="LocalReportFormat" attr="6692">long</i>
<i name="DateAndTimeFormat" attr="6692">default</i>
<i name="OldEventReporting" attr="6692">on</i>
<b name="EnableKernelLogging" attr="6692">1</b>
<i name="MaximumSizeLocalEventLog" attr="6692">500</i>
<b name="FiltersEnabled" attr="6692">1</b>
<b name="DebugLevelFiltering" attr="6692">1</b>
<t name="Filters" attr="6692">
<o name="1" attr="6692">
<b name="Enable" attr="6692">0</b>
<i name="Level" attr="6692">3</i>
<s name="Originator" attr="6692">scm-*-*-*</s>
</o>
<o name="2" attr="6692">
<b name="Enable" attr="6692">0</b>
<i name="Level" attr="6692">3</i>

Still to figure out how to make it in the opposite way (to create an encrypted image accepted by the router from a decrypted modified configfile), but that active_configfile is saved from my router (after removing my passwords from it) and ssh enabled it should be loadable to any device what use the same AES key as this one (and it listens on 192.168.1.10 here).

ATTACHMENTS:
active_configfile.
encrypted_configfile.
hopefully_decrypted_configfile.

Last modified: 2012-02-17, 10:37

© 2011 …