eerimoq / asn1tools

ASN.1 parsing, encoding and decoding.
MIT License
295 stars 100 forks source link

decoding erros #171

Open Alhamdou opened 11 months ago

Alhamdou commented 11 months ago

Hello I need help with regards to decoding my this .asn file. In this my code here when i tried to decode the `MTCallRecord i do see an empty { } in my save.txt file.

This is my code: import asn1tools

Compile ASN.1 schema

path_asn = 'cdr_schema.asn' schema = asn1tools.compile_files(path_asn, codec='der')

Input file path

file_path = 'b06044079.dat'

Output file path

output_file_path = 'output_decode.txt'

with open(file_path, "rb") as f: bit_cdr = f.read()

Decode the data

decoded_data = schema.decode('MTCallRecord', bit_cdr)

Save the decoded data to a text file

with open(output_file_path, "w") as output_file: output_file.write(str(decoded_data))

print(f"Decoded data saved to: {output_file_path}")

but when I change the type that i want to decode let's say for instance to CallEventRecord i get an error like this. " raise MissingMandatoryFieldError(member, offset) asn1tools.codecs.ber.MissingMandatoryFieldError: CallEventRecord.outGatewayRecord:"

If i also try to change the decoding to CallEventDataFIle i do get an output but almost all the data in the output file will still be encoded.

I will definity appreciate it if assisted. link to the asn file am using: https://drive.google.com/file/d/19dfbl6TUEZDNm61zEm8UEp3qNco8SoGH/view?usp=sharing .dat link: https://drive.google.com/file/d/1ibNekvIGXrmW7-bYENRBNMXkDwdNWDIJ/view?usp=sharing

Thanks.

ladjigar commented 11 months ago

Hello @Alhamdou,

you have to use root name from which cdr spec begins. replace MTCallRecord with CallEventDataFile as shown below. It will work.

decoded_data = schema.decode('CallEventDataFile', bit_cdr)

Alhamdou commented 11 months ago

Hello @ladjigar thanks for the help. After making the necessary changes you suggested I was able decode the headers but the content inside was still encoded.

This here is a sample of what am seeing in the output file. 'headerRecord': {'productionDateTime': b'#\x11\x07\x12\x00\x17+\x00\x00', 'recordingEntity': b'\x00', 'extensions': []}, 'callEventRecords': [('mtSMSRecord', {'recordType': 7, 'serviceCentre': b'\x91"0\x00\x00\x10', 'servedIMSI': b'\x06\x07\x04\x00%d\x05\xf3', 'servedIMEI': b'S8pPBQ8\xf0', 'servedMSISDN': b'\x91"0w\x16x', 'msClassmark': b'W\x18\xa2', 'recordingEntity': b'\x91"0\x00%w'

As you can see some are still encoded Thanks

ladjigar commented 11 months ago

@Alhamdou I checked the asn schema. Those fields have type OCTET STRING which are represented by bytes when decoded. When you iterate over dictionary of decoded record, those fields you can covet it to hex ASCII string when you use it.

ladjigar commented 11 months ago

@Alhamdou There is work around hack where in class of OctetString asn1tools/codecs/der.py you could change return type to hex string, but it will impact all octet string decoding in asn schema.

class OctetString(StandardEncodeMixin, Type):

def __init__(self, name):
    super(OctetString, self).__init__(name,
                                      'OCTET STRING',
                                      Tag.OCTET_STRING)

def encode_content(self, data, values=None):
    return data

def decode_content(self, data, offset, length):
    end_offset = offset + length

    return bytes(data[offset:end_offset]), end_offset
Alhamdou commented 11 months ago

Hi @ladjigar i did modified the recomendation you made above. However, am still having some issues with the output. Execuse me am kinda new in this kind of problems, I do also appreciate the help you are given.

This the new error message am having.

CallEventRecord.outGatewayRecord: Set(outGatewayRecord, [Integer(recordType), OctetString(callingNumber), OctetString(calledNumber), OctetString(recordingEntity), ExplicitTag(mscIncomingROUTE), ExplicitTag(mscOutgoingROUTE), OctetString(seizureTime), OctetString(answerTime), OctetString(releaseTime), Integer(callDuration), Integer(causeForTerm), ExplicitTag(diagnostics), OctetString(callReference), Integer(sequenceNumber), SetOf(recordExtensions, Sequence(, [ObjectIdentifier(identifier), Boolean(significance), OctetString(information)])), Enumerated(partialRecordType), ExplicitTag(basicService), Sequence(additionalChgInfo, [Integer(chargeIndicator), OctetString(chargeParameters)]), Null(ussdCallBackFlag), Enumerated(chargedParty), OctetString(originalCalledNumber), OctetString(chargeAreaCode), OctetString(rateIndication), OctetString(roamingNumber), Integer(mscOutgoingCircuit), OctetString(orgMSCId), OctetString(callEmlppPriority), OctetString(eaSubscriberInfo), OctetString(selectedCIC), Enumerated(callerportedflag), Enumerated(cUGOutgoingAccessIndicator), OctetString(cUGInterlockCode), Enumerated(cUGIncomingAccessUsed), Enumerated(mscIncomingRouteAttribute), Enumerated(mscOutgoingRouteAttribute), OctetString(networkCallReference), OctetString(setupTime), OctetString(alertingTime), Enumerated(voiceIndicator), Enumerated(bCategory), Enumerated(callType), Integer(chargePulseNum), Enumerated(disconnectparty), Integer(chargePulseNumforITXTXA), OctetString(networkOperatorId), Enumerated(audioDataType), Integer(recordNumber), Sequence(partyRelCause, [Enumerated(releaseParty), Integer(releaseCause)]), Enumerated(chargeLevel), OctetString(locationNum), Enumerated(locationNumberNai), OctetString(translatedNumber), Sequence(location, [OctetString(locationAreaCode), OctetString(cellIdentifier)]), SequenceOf(changeOfLocation, Sequence(, [Sequence(location, [OctetString(locationAreaCode), OctetString(cellIdentifier)]), OctetString(changeTime)])), OctetString(firstmccmnc), OctetString(lastmccmnc), OctetString(icidvalue), OctetString(origioi), OctetString(termioi), Enumerated(calledportedflag), OctetString(locationroutingnumber), Enumerated(intermediateChargingInd), Integer(mscOutgoingROUTENumber), Integer(mscIncomingROUTENumber), OctetString(drcCallId), OctetString(drcCallRN)]) is missing and has no default value (At offset: 1046597)

Thanks.

ladjigar commented 11 months ago

@Alhamdou , Kindly modify your python path/to/site-packages/asn1tools/codecs/der.py file

convert return bytes to hex with return bytes(data[offset:end_offset]).hex()

class OctetString(StandardEncodeMixin, Type):

    def __init__(self, name):
        super(OctetString, self).__init__(name,
                                          'OCTET STRING',
                                          Tag.OCTET_STRING)

    def encode_content(self, data, values=None):
        return data

    def decode_content(self, data, offset, length):
        end_offset = offset + length

        return bytes(data[offset:end_offset]).hex(), end_offset

Sample code.

`$ cat t2.py

!/usr/bin/python

import pprint import asn1tools

schema = asn1tools.compile_files('huawei-cdr.asn', codec='der') bit_cdr=open('cdr_africell.dat', "rb").read() decoded_data = schema.decode('CallEventDataFile', bit_cdr)

pprint.pprint(decoded_data)`

Sample output

('incGatewayRecord', {'additionalChgInfo': {'chargeIndicator': 2}, 'alertingTime': '1812010006182b0000', 'answerTime': '1812010006192b0000', 'audioDataType': 'audio', 'bCategory': 'subscriberFree', 'basicService': ('teleservice', '11'), 'cUGOutgoingAccessIndicator': 'notCUGCall', 'callDuration': 1, 'callEmlppPriority': '01', 'callReference': '040701e0f9b4', 'callType': 'incoming', 'calledNumber': '913292893946f7', 'calledportedflag': 'numberNotPorted', 'callerportedflag': 'numberNotPorted', 'callingNumber': '913272278951f0', 'causeForTerm': 0, 'chargeAreaCode': '000000', 'chargeLevel': 'chargeBySecond', 'diagnostics': ('gsm0408Cause', 144), 'disconnectparty': 'unknowparty', 'firstmccmnc': '16f950', 'intermediateChargingInd': 'sINGLE-BILL', 'lastmccmnc': '16f950', 'location': {'cellIdentifier': '3783', 'locationAreaCode': '025a'}, 'mscIncomingROUTE': ('rOUTEName', 'FTBPTESTB'), 'mscIncomingRouteAttribute': 'sip', 'mscOutgoingROUTE': ('rOUTEName', 'BSC02B'), 'networkCallReference': '1f144b3231', 'recordNumber': 1021753356, 'recordType': 3, 'recordingEntity': '913272170808f0', 'releaseTime': '1812010006202b0000', 'roamingNumber': '913272170689f2', 'seizureTime': '1812010006182b0000', 'setupTime': '1812010006152b0000'}),

Alhamdou commented 11 months ago

Hi @ladjigar thanks so very much for the support that indeed works for me, however, i think it is not fully decoded looking at the 'callingNumber': '913272278951f0' this is still in hex and i was told this has to be a phone number but rather it is still in hex format. Thanks

ladjigar commented 11 months ago

Hi @Alhamdou, I am seeing the huawei-cdr.asn ASN1 spec you have shared The callingNumber have following definition. See if it helps hex is converted to BCD string manually. CallingNumber ::= BCDDirectoryNumber

BCDDirectoryNumber ::= OCTET STRING

-- This type contains the binary coded decimal representation of
-- a directory number e.g. calling/called/connected/translated number.
-- The encoding of the octet string is in accordance with the
-- the elements "Calling party BCD number", "Called party BCD number"
-- and "Connected number" defined in TS 24.008.
-- This encoding includes type of number and number plan information
-- together with a BCD encoded digit string.
-- It may also contain both a presentation and screening indicator
-- (octet 3a).
-- For the avoidance of doubt, this field does not include
-- octets 1 and 2, the element name and length, as this would be
-- redundant.

Regards
Alhamdou commented 11 months ago

Hi @ladjigar am sorry but am faily new to this projects, so if am to understand you properly does that mean that i cant convert it using this package ? If i can convert it from the BCDDirecotoryNumber what should be the step should i take. Thanks once again am so sorry if the question sounds silly