Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decoding erros #171

Open
Alhamdou opened this issue Nov 24, 2023 · 9 comments
Open

decoding erros #171

Alhamdou opened this issue Nov 24, 2023 · 9 comments

Comments

@Alhamdou
Copy link

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
Copy link

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
Copy link
Author

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
Copy link

@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
Copy link

ladjigar commented Nov 26, 2023

@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
Copy link
Author

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
Copy link

ladjigar commented Nov 28, 2023

@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
Copy link
Author

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
Copy link

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
Copy link
Author

Alhamdou commented Dec 1, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants