mUgfƼ ddlmZmZddlZddlZddlZddlmZddlmZej ej ej dkrddlm Z m Z e j Ze jZe jZnddlmZmZejZejZejZddlmZmZmZddlZddlZddlZddlZdd lmZdd lmZdd l m Z!dd l"Tdd l#m$Z$m%Z%m&Z&m'Z'ddl(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0ddl(m1Z1m2Z2m3Z3m4Z4m5Z5ddl(m6Z6m7Z7m8Z8ddl9m:Z:m;Z;ddlZ>m?Z?ddl@mAZAddlBmCZCddlDmEZEddlFmGZGmHZHmIZIddlJmKZKGddejLZMGddejNZOGddeMZPedGdd eZQed!Gd"d#eZRGd$d%e'ZSejTd&ejU'ZVe&eVeVWd(ZXeXYd)d*dd+,eXYd-d.dd/,eXYd0d1gd2d34eXYd5d6d.d78eXZZ[e[Yd9d:d;de[Yd?d@dAdBddC>eVYdDdEdFGeVYdHej\dIGe]dJkreV^Z_e_j`r6ejae_j`ejbsecdKejddLiZee_jfD]ZgeghdM\ZiZjejeeei< ekejldNzZme_jnreHeGe_jneeejopemreHeGemeee$e_eSOZqe_jrre;de_jrZsn e:eqZs eKeq\ZtZuePeueteqese_j`Zvn#ecdPekeqzecdQejwecdQecdRecdSecdTecdUe_j`r ecdVePddeqesZvYnxYwe1e_jxe_jyZxexrUeus ecdW eujz{euj|e)exn#e}$rZ~ece~YdZ~[~ndZ~[~wwxYwe_jrHevdXe_jdYe_jdSevdSdS)Z)ListOptionalN)version)stylez2.3.0)fgbg)FgBg) CommandSetwith_default_categorywith_argparser)Path)StringIO)pprint)*) init_reader ApduTracerargparse_add_reader_argsProactiveHandler)h2bb2hi2h swap_nibblesrpad JsonEncoderbertlv_parse_onesw_match)sanitize_pin_admtabulate_str_listboxed_heading_strHexstr dec_iccid)is_hexstr_or_decimal is_hexstr is_decimal) CardHandlerCardHandlerAuto)CardMFCardDFCardADF)Ts102222Commands) DF_EIRENE)ProactiveCommand)CardKeyProviderCsvcard_key_provider_registercard_key_provider_get_field) init_cardc:eZdZdZddeeddffd ZxZS) Cmd2CompatzBackwards-compatibility wrapper around cmd2.Cmd to support older and newer releases. See https://github.com/python-cmd2/cmd2/blob/master/CHANGELOG.mdN file_pathreturnctjtjtjdkr||St |SN2.0.0)rparsecmd2 __version__ _run_editorsuper run_editor)selfr4 __class__s /build/pySim-shell.pyr>zCmd2Compat.run_editorFsQ =) * *W]7-C-C C C##I.. .77%%i00 0N)__name__ __module__ __qualname____doc__rstrr> __classcell__r@s@rAr3r3Cs`RR11HSM1T1111111111rBr3c"eZdZdZfdZxZS)Settable2CompatzBackwards-compatibility wrapper around cmd2.Settable to support older and newer releases. See https://github.com/python-cmd2/cmd2/blob/master/CHANGELOG.mdc tjtjtjdkrt j|||fi|dSt j||||fi|dSr7)rr9r:r;r=__init__)r?nameval_type descriptionsettable_objectkwargsr@s rArNzSettable2Compat.__init__Osy =) * *W]7-C-C C C EGG T8[ C CF C C C C C EGG T8[/ T TV T T T T TrB)rDrErFrGrNrIrJs@rArLrLLsMRRUUUUUUUUUrBrLcxeZdZdZdZd8fd ZdZd9dZdZd Z d Z Gd d e Z d Z ejedZdejdefdZejedZejZedededdededddejedZejedZGdd eZd!Z d"Z!d#Z"ejZ#e#d$d%&e#d'd(de#d)e$d*d+,e#d-edd.,e#d/edd0,eje#ejed1Z%ejZ&e&d2d3d45eje&ejed6Z'ejed7Z(xZ)S):PysimApppySim CommandszWelcome to pySim-shell! (C) 2021-2023 by Harald Welte, sysmocom - s.f.m.c. GmbH and contributors Online manual available at https://downloads.osmocom.org/docs/pysim/master/html/shell.html Nc dtjtjtjdkrddi}nddi}t jdddd|d|t |jt|_ d |_ d|_ d|_ d|_ |j |j |j d |_||_||_d|_d|_d|_d|_|t-d t.d ||j |t-dt.d||j |t-dt.d||t-dt.d||j |||dS)Nr8 use_ipythonT include_ipyz~/.pysim_shell_historyF)persistent_history_fileallow_cli_argsauto_load_commandsstartup_scriptrzpySim-shell built-in commands)cardrslchan numeric_pathzPrint File IDs instead of names) onchange_cbconserve_writezRead and compare before writejson_pretty_printzPretty-Print JSON output apdu_tracez+Trace and display APDUs exchanged with card)rr9r:r;r=rNrBANNERREDintrodefault_categoryr_r`ra py_localsslchrbrdrerf add_settablerLbool_onchange_numeric_path_onchange_conserve_write_onchange_apdu_traceequip)r?r_r`rmrnscriptrSr@s rArNzPysimApp.__init__[s =) * *W]7-C-C C C#T*FF#T*F  T1IZ_,1& T TLR T T T4;3/// ?  "&)47TZPP!"!% /.$@acg6:6QSSS T T T /*:DBacg6:6SUUU V V V /*=tE_aeffggg /,>kmq6:6OQQQ R R R 4rBcd}|jr\|jjd}|||jjr)|jjjD]}||t ttfD]4}| |}|r||d5||_ ||_|j ro|jrg|jjd|_| dd|j | dd|j|jjr)|jjjD]}|||t |t|t |jd|t#|jd|jd<n#d|jd<YnxYw|jd|d }n|d ||S) z Equip pySim-shell with the supplied card and runtime state, add (or remove) all required settables and and commands to enable card operations. Frrdrfz MF/EF.ICCIDICCIDNMFTzpySim-shell not equipped!)r`raunregister_cmdsprofile shell_cmdsetsunregister_command_setIso7816Commandsr+ PySimCommandsfind_commandsetsr_rrrdrsrfregister_command_setselectr" read_binaryidentitypoutput update_prompt)r?r_r`rcracmd_setcmdss rArtzPysimApp.equip{s[  7 9GM!$E  ! !$ ' ' 'w 9#w<99G//8888$&6 F 8 8D++D11G 8++GAJ777  9 6 6q)DJ  ) ) %)< > > >  % %lE4? K K Kw 7#w<77G--g6666  % %o&7&7 8 8 8  % %&6&8&8 9 9 9  % %moo 6 6 6 , !!-666'01G1G1I1I!1L'M'M G$$ ,'+ G$$$ J  dD ) ) )BB LL4 5 5 5  s 5AH HFc|s |jdkrtj|t}ntj|td}||dS)z5like cmd2.poutput() but for a JSON serializable dict.Fcls)rindentN)rejsondumpsrr)r?dataforce_no_prettyoutputs rA poutput_jsonzPysimApp.poutput_jsons\  Ad4==Z+666FFZ+a@@@F VrBc.|dSrC)rr? param_nameoldnews rArqzPysimApp._onchange_numeric_paths rBc0|jr||j_dSdSrC)r`rdrs rArrz!PysimApp._onchange_conserve_writes& 7 )%(DG " " " ) )rBc|jrI|dkr+|||jjj_dSd|jjj_dSdS)NT)r_Cmd2ApduTracer_scc_tp apdu_tracerrs rArszPysimApp._onchange_apdu_tracesW 9 6d{{151D1DT1J1J "...15 "...  6 6rBceZdZdZdZdS)PysimApp.Cmd2ApduTracerc||_dSrC)r:)r?cmd2_apps rArNz PysimApp.Cmd2ApduTracer.__init__s  DIIIrBc|jd|ddd|dd|jd|d|dS)Nz->  z<- z: )r:r)r?cmdswresps rAtrace_responsez&PysimApp.Cmd2ApduTracer.trace_responses` I   CHHHc"##hh? @ @ @ I   RRR6 7 7 7 7 7rBN)rDrErFrNrrgrBrArrs2 ! ! ! 8 8 8 8 8rBrc.|jrv|jj|j }|jjj}|r&dt ||jj|fz|_dSd|jj|fz|_dS|j r d|_dSd|_dS)NzpySIM-shell (%s:%02u:%s)> zpySIM-shell (%02u:%s)> zpySIM-shell (no card profile)> zpySIM-shell (no card)> ) ra selected_filefully_qualified_path_strrbsccscprHlchan_nrpromptr_)r?path_strrs rArzPysimApp.update_prompts : 8z/HHTM^I^__H*.$C Z:c#hh H[]e=ff 74:;NPX:YY y 8? 7 rBc:||jdS)zDisplay the intro bannerN)rrjr?_s rAdo_introzPysimApp.do_intros TZ     rBrr5cV|d|dS)N)rdo_quitrs rAdo_eofzPysimApp.do_eofs& R||BrBc|jr5|jjr)|jjjD]}||t |j\}}|||dS)zEquip pySim-shell with cardN)r`rzr{r|r1rmrt)r?optsrr`r_s rAdo_equipzPysimApp.do_equipss 7 5tw 57?8 5 5++G4444TW%%D 4rBAPDUzAPDU as hex stringtypehelpz --expect-swzexpect a specified status word)rrdefaultz--rawz/Bypass the logical channel (and secure channel) store_trueractionc|js|j(|jj|j\}}n'|jj|j\}}|r|d|d|n|d|z|jr*t||jst||jdSdS)a5Send a raw APDU to the card, and print SW + Response. CAUTION: this command bypasses the logical channel handling of pySim-shell and card state changes are not tracked. Dpending on the raw APDU sent, pySim-shell may not continue to work as expected if you e.g. select a different file.NzSW: z, RESP: zSW: %s) rawrar_r send_apdurrr expect_swr SwMatchErrorr?rrrs rAdo_apduzPysimApp.do_apdus 8 ;tz)y~// ::HD""z~// ::HD"  ( LLLrrr448 9 9 9 9 LLB ' ' ' > 7B// 7"2t~666 7 7 7 7rBc|j}|jr"|jjjrd|jj_|dt |z|dS)zReset the Card.Nz Card ATR: %s)r_resetrarrrrr)r?ratrs rAdo_resetzPysimApp.do_reset snioo : &$*., &!%DJN  ^c#hh./// rBc eZdZdZdZdZdS)PysimApp.InterceptStderrc(tj|_dSrC)sysstderr_stderr_backupr?s rArNz!PysimApp.InterceptStderr.__init__s"%*D   rBcNt|_|jt_|SrC)r_stringio_stderrrrrs rA __enter__z"PysimApp.InterceptStderr.__enter__s$,JJD !.CJKrBc|j|_|`|jt _dSrC)rgetvaluestriprrr)r?argss rA__exit__z!PysimApp.InterceptStderr.__exit__s8/88::@@BBDK%,CJJJrBN)rDrErFrNrrrgrBrAInterceptStderrrsA - - -     - - - - -rBrcn|tdt|tdt|tdt|tdt|tdt|tdt|tdt|ddS)N +-------------+r^z + ## ## +z + ## ## +z + ### +r)rr LIGHT_REDrs rA_show_failure_signzPysimApp._show_failure_sign s U.9===>>> U.9===>>> U.9===>>> U.9===>>> U.9===>>> U.9===>>> U.9===>>> RrBcn|tdt|tdt|tdt|tdt|tdt|tdt|tdt|ddS) Nrr^z + ## +z + ## +z + # ## +z + ## # +z + ## +r)rr LIGHT_GREENrs rA_show_success_signzPysimApp._show_success_sign*s U.;???@@@ U.;???@@@ U.;???@@@ U.;???@@@ U.;???@@@ U.;???@@@ U.;???@@@ RrBc t|j\}}|||}n#|d|dt |jz|dt j|d|dYdSxYw|r@|d|d|d|5}||dddn #1swxYwY|d|d|d|j rE|d||j |dn|d|dd|j vrd SdS) Nr2Card initialization (%s) failed with an exception:,---------------------8<---------------------zTranscript stdout:zTranscript stderr:z(none)zEXCEPTION of typer) r1rmrtrrH traceback print_excr do_run_scriptr)r?first script_pathr`r_rloggeds rA _process_cardzPysimApp._process_card4s3  ))HBD"%%BB  LL    LLMPSTXT[P\P\\ ] ] ] LLG H H H   ! ! ! LLG H H H LL   22   LL    LL- . . . LLG H H H%%'' 06"";/// 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 LLG H H H LL    LL- . . .} ' KLLL V]+++ KLLLL X&&& LL   "&-77qrs-0BCEEErzpath to the script file)rz--halt_on_errorz(stop card handling if an exeption occursz--triesz*how many tries before trying the next cardrrrz--on_stop_actionz5commandline to execute when card handling has stoppedz--pre_card_actionz:commandline to execute before actually talking to the cardc2tj|jtjs|ddSd}d}d} t |jD]}|j||j rtj |j | ||j}|dkr5|dz}| |d||fzn4|dz}| |d||fz|dkr|jn|j|jr|dkrdSn-#t"$r.|d|dYdSt$$r$|jrtj |jYdS|d|d t)|jz|d t-j|d |d|dz}| |d||fzYnxYwd }V) z0Run script on multiple cards (bulk provisioning)Invalid script file!NrTz$Statistics: success :%i, failure: %irzTerminated by user!z,Card handling (%s) failed with an exception:rF)osaccessrR_OKrrangetriesrngetpre_card_actionsystemrrrerrordone halt_on_errorKeyboardInterrupt SystemExiton_stop_actionrHrmrr)r?r success_count fail_countrirs rAdo_bulk_scriptzPysimApp.do_bulk_scriptis y)2733  LL/ 0 0 0 F  B  : :tz**88AGKK&&&+8 $"6777++E43CDDBQww(5(9 //111 %K):O7&7888%/!^ //111 %K):O7&78888 66GMMOOOOGLLNNN%"q&&F%    R    2333    &3Id1222 : R    KcRVRYllZ[[[ KLLL#%%% KLLL R   '!^ ''))) C+Z89:::::EEB sD E((4J)J CJstringzstring to echo on the shell+)rnargsc`|d|jdS)z$Echo (print) a string on the consolerN)rjoinr r?rs rAdo_echozPysimApp.do_echos* SXXdk**+++++rBc^ddl}||ddS)z!Print the pySim software version.rNpySim) pkg_resourcesrget_distribution)r?rrs rA do_versionzPysimApp.do_versions6  ]33G<<=====rBrC)F)*rDrErFCUSTOM_CATEGORYrhrNrtrrqrrrsrrrr: with_categoryrargparse NamespacerprrArgumentParserapdu_cmd_parser add_argumentr$rHr rrlistrrrrbulk_script_parserintr echo_parserrrrIrJs@rArUrUUs&O_F@222h)))66688888888 8 8 8T((!!)(! * t    T(()(.h-//O  i>R SSS  5U\_im nnn  /`iu vvvT))77*)7,T(()( - - - - -$ - - -'''R1022##5$777##$5>)(>>>>>rBrUrVceZdZfdZejZedddedddedd ded d dej ed Z d dZ dZ dZ ejZededdedddej edZdZejZeddedej edZdZxZS)!r~cHtdSrCr=rNr?r@s rArNzPySimCommands.__init__ rBz--fidszShow file identifiersrrz--nameszShow file namesz--appszShow applicationsz--allz)Show all selectable identifiers and namesc|jrg}nH|js|js|jr/ddg}|jr|ddgz }|jr|ddgz }|jr|ddgz }ngd}t |jjj|}t|d d d d }|jjj d }|j ||jjj d}|j ||j ||j dt|zdS)z@Show a listing of files available in currently selected DF or MFPARENTSELFFIDSAIDSFNAMESANAMES)r&r'r*r+flagsOrrT)widthhspacelspace align_leftFz%d filesN) allfidsnamesappsr_cmdrarget_selectable_namesrrrlen)r?rr- selectables directory_strpaths rAdo_dirzPySimCommands.do_dirs 8 ;EE Y ;$* ; ;v&Ey *&&))z .(H--y ,(F++:::E IO ) > >U > K KMM ) r!A$HHH y,EEdKK $y,EEeLL $ -((( *s;'7'7788888rBrNc t|jjjtr |r ||fi||jjjddg}|D]j}|s|sd|zt |zdz}|dd}t||tr!|dt ||jzz }n |dt ||j zz }|dt ||j zz }|j |t||trd } |jj ||j} n#t$r} d } |jjj} | d } | d zt |zd zt | z}|r+|d xxdz cc<|d|Yd} ~ nd} ~ wwxYw| d krX|j|dz|||fi||jjjj}|jjj} |jjj}t|t&r|r|jd kr|D]`}t|dtrC|djd kr2|jj |dj|jna|jj | j|j|jj d|j} |ro|jjj}|||fi|||jjjkr?t1dt |jjjdt |dldS)zORecursively walk through the file system, starting at the currently selected DFr*r+r,z z rrFT/, DF_SKIPrDF_SKIP_REASONNz..zinconsistent walk, z% is currently selected but expecting z to be selected) isinstancer7rarr)get_selectablesrHr*aidfiddescrr Exceptionrappendwalkparent selected_adfr(has_fsitemsrOget_mf RuntimeError)r?r action_ef action_dfcontextrSfilesf output_strskip_dffcp_decedfdf_pathdf_skip_reason_strrLadf selectabledf_before_actions rArKzPySimCommands.walks  dio3V < < - - ',,V,,, -==X&>((A fA fA .Y .!F]SVV3yA '"- eAh00:#E!HL(9(9"99JJ#E!HL(9(9"99JcCa $6$666  !!*---%(F++5 f M"io44Q BBGG MMM"G6B 99$??G)0*!!ff*%'+*,.1!ff*5&M ***a/*** 01889KLLLMe##DIfqj)YRR6RRR!Y_:AF6B)/6C!&&11JsJszU?R?R+1*@*@*B*B*H*H*J*J&&J)*Q-AA&jQRmFZ^bFbFb $  6 6z!}7I49 U U U % ..ryy{{/?KKKK#')/"8"8ty"I"I f#'9?#@  !W/////$ty'DDD&,*-dio.K*L*L*L*LcRbNcNcNcNc(efffAA fA fs7%E G.'A=G))G.c.|dS)z3Display a filesystem-tree with all selectable filesN)rKrs rAdo_treezPySimCommands.do_tree?s rBc D |dxxdz cc<|jjj}t|tst dt |z|d}|d}|d}|dzt |z}|j t||j d|d|d  |jj ||j} |j d |jjjj d|jjjj d |jj} |j d t | z|j d t |jjjz|j d t |jjjz|D],} |j dt | z-|j d|jjjj z| dkr|r\|jj} |j dt%j| dt(zn|jj} |j dt | dzn| dks| dkr|jj} | rt/d| dzD]}|r^|jj|} |j d|t%j| dt(fzb|jj|} |j d|t | dfznd} |r^|jj|} |j d|t%j| dt(fznQ|jj|} |j d|t | dfzn$#t4$r}|jdkrYd}~n|d}~wwxYw|dz}n| dkr|jj}|D]t}|jj|} t=t?| d\}}}}|j d|tA|fzunt d| d|dn#tB$rz}|dzt |zdzt |z}|j d |z|d!xxdz cc<|d""|Yd}~nd}~wwxYw||jjjkr1|jj |j p|j#|j|j d#dS)$z1 Select and export a single elementary file (EF) COUNTrz-currently selected file %s is not a DF or ADFTFr@z # directory:  ()z# file: z# structure: %sz# RAW FCP Template: %sz# Decoded FCP Template: %szselect transparentzupdate_binary_decoded '%s'rrzupdate_binary cyclic linear_fixedzupdate_record_decoded %d '%s'zupdate_record %d %s9402Nber_tlvzset_data 0x%02x %szUnsupported structure "z " of file ""rAz# bad file: %sERRBAD#)$r7rarrDr)rQrHfully_qualified_pathrrr rrOrGselected_file_structureselected_file_fcp_hexselected_file_fcpread_binary_decrrrrselected_file_num_of_recrread_record_dec read_recordr sw_actual retrieve_tags retrieve_datarrrrIrJrF)r?filenamerTas_jsonr[ df_path_listr\ df_path_fidfile_strrY structurerVresult num_of_recrrZtagsttaglvalremainer bad_file_strs rA export_efzPySimCommands.export_efCsA Y_ * "f%% K?#b''IKK K..t44 --d3311%88 S=3x==0 +H55666 GGG[[[IJJJB 0io,,XtyAAG I    -222DIO4Q4U4U4UW X X X ??AAI I  /#i..@ A A A I  6TY_=b9c9cc d d d I  :SAb=c=cc d d d! 6 6 !!)c!ff"45555 I  i$)/*G*LL M M MM))I!Y_<<>>FI%%&BTZPVWXPY_jEkEkEk&kllll!Y_88::FI%%&6VAY&GHHHHh&&)~*E*E!Y_EEGG ""1j1n55[["[%)Y_%D%DQ%G%GF I--.MQRTXT^_efg_hnyTzTzTzP{.{||||%)Y_%@%@%C%CF I--.Cq#fUVi..FY.YZZZZ [A" (&_)-)H)H)K)K $ 1 12QUVX\Xbcijkclr}X~X~X~T2!A!A!A!A)-)D)DQ)G)G $ 1 12G1cRXYZR[nnJ]2] ^ ^ ^+((( {f44 %'( (E"!& i''y4466LLA!Y_::1==F.>s6!9~~.N.N+S!S(I%%&:aS]&JKKKKL #lAJHHHUWWW 0 0 0"S=3x==84?#a&&HL I  .= > > > ENNNa NNN EN ! !, / / / / / / / /  0 . . . IO " "26#3RVTY ? ? ? #sL3L5V9)B1SV9 S<% S70V95S77S<'%.RcAddff f f fsA BABBc|jjjj}|r|j|dS|jddS)zGDisplay human readable file description for the currently selected filezno description availableN)r7rarrHr)r?rrHs rAdo_desczPySimCommands.do_descsRy,1  : I  d # # # # # I  8 9 9 9 9 9rBADM1?z7ADM1 pin value. If none given, CSV file will be queried)r rrc|jrt|j}nn|jjjd}t dd|}t|}|r"|jd|d|dntd|z|rC|jjj |jj j t|d Std) zVerify the ADM (Administrator) PIN specified as argument. This is typically needed in order to get write/update permissions to most of the files on SIM cards. Currently only ADM1 is supported.rwrkeyvaluezfound ADM-PIN ' ' for ICCID ''z"cannot find ADM-PIN for ICCID '%s'z'error: cannot authenticate, no adm-pin!N)rrr7r`rr0r ValueErrorrar verify_chvr_ _adm_chv_numr)r?rpin_admiccidrs rA do_verify_admzPySimCommands.do_verify_adms 9 Q&ty11GGIL)'2E0WERRRF&v..G Q !!!QVQVQV"WXXXX !E!OPPP  H IO  * *49>+FG U U U U UFGG GrBcF|jd|jd|jjjz|jd|jjjdz|jjjdd}|r|jd|z|jd|jjjd z|jd |jjjj z|jd |jjjj z|jd |jjj j D]}|jd |z dS)z5Display information about the currently inserted cardz Card info:z Name: %sz ATR: %sATREIDNz EID: %sz ICCID: %srwz Class-Byte: %sz Select-Ctrl: %sz AIDs:z %s) r7rr_rOr`rrrarcla_bytesel_ctrlmf applications)r?reidas rA do_cardinfozPySimCommands.do_cardinfosh ,''' + (;;<<< *ty|'w)GGHHH +dio.A.JJKKK ,ty/B/KKLLL (###- . .A !!&1*---- . .rB)rNNN)rDrErFrNrr dir_parserrr:r r=rKrbr export_parserrHrrverify_adm_parserr#rrrIrJs@rAr~r~sk)(**J.|EEE),@@@*<AAAA,XXXT$$99%$92JfJfJfJfX___B,H+--M33NPPP,LNNNT''%f%f('%fN:::0/11""6;O(a#cccT*++HH,+H. . . . . . . .rBr~zISO7816 CommandscFeZdZfdZdZdeefdZdZe j Z e de dd e d ed eje d Ze j Ze de dd e ded e ded ejedZe j Ze de dd e d ed e ded ejedZe j Ze de dd e d ed ejedZe j Ze de dd e d ed ejedZdZe j Ze ded ejedZdeefdZe j Ze de dd ejedZe j Z e de dd eje d Z!e j Z"e" de dd eje"d!Z#d"Z$xZ%S)#r}cHtdSrCr"r#s rArNzIso7816Commands.__init__r$rBct|jdkrk|jjjd}|jjjd}|jd|d|ddS|jd}|jj||j}|j|j |dS)zSELECT a File (ADF/DF/EF)rTFzcurrently selected file: rerfN) r9arg_listr7rarrrrrr)r?rr<path_fidrYs rA do_selectzIso7816Commands.do_select s t}   " "9?0II$OODy4MMeTTH I   DDD(((S T T T F}Q)/((ty99 !!! w'''''rBr5cd|jjji}|j|||||S)z&Command Line tab completion for SELECTr index_dictr7rarr8index_based_completer?textlinebegidxendidxrs rAcomplete_selectzIso7816Commands.complete_selectA6KKMMN y--dD&&U_-```rBc d}t||vrt|S|jjjd}t t|d|}t|}|r7|jd|d|d|dn3|jd|d |d|S) zCUse code either directly or try to get it from external data source)PIN1PIN2PUK1PUK2rwrzfound z 'rrz cannot find z for ICCID ')rHupperrr7r`rr0r)r?codeautorrs rAget_codezIso7816Commands.get_codes/ t99??  D ( (#D)) ) %g.,SYYG5QQQ!&))  W I    fffV[V[V[\ ] ] ] ] I   uuuU V V V rBz--pin-nrrz4PIN Number, 1=PIN1, 2=PIN2 or custom value (decimal)rpin_codezKPIN code digits, "PIN1" or "PIN2" to get PIN code from external data sourcerc||j}|jjj|jt|\}}|jddS)zVerify (authenticate) using specified CHV (PIN) code, which is how the specifications call it if you authenticate yourself using the specified PIN. There usually is at least PIN1 and PIN2.zCHV verification successfulN) rrr7rarrpin_nrrrr?rpinrrs rA do_verify_chvzIso7816Commands.do_verify_chv2s\ mmDM**Y_(33DKSJJ r 788888rBz4PUK Number, 1=PIN1, 2=PIN2 or custom value (decimal)puk_codezJPUK code digits "PUK1" or "PUK2" to get PUK code from external data source new_pin_codezJPIN code digits "PIN1" or "PIN2" to get PIN code from external data sourcec2||j}||j}|jjj|jt|t|\}}|j ddS)z)Unblock PIN code using specified PUK codezCHV unblock successfulN) rrrr7rar unblock_chvrrr)r?rnew_pinpukrrs rAdo_unblock_chvzIso7816Commands.do_unblock_chvCs}-- 122mmDM**Y_(44 KS3w<<11 r 233333rBc2||j}||j}|jjj|jt|t|\}}|j ddS)z!Change PIN code to a new PIN codezCHV change successfulN) rrrr7rar change_chvrrr)r?rrrrrs rA do_change_chvzIso7816Commands.do_change_chvTs}-- 122mmDM**Y_(33 KS3w<<11 r 122222rBc||j}|jjj|jt|\}}|jddS)z)Disable PIN code using specified PIN codezCHV disable successfulN) rrr7rar disable_chvrrrrs rAdo_disable_chvzIso7816Commands.do_disable_chvcs\mmDM**Y_(44T[#c((KK r 233333rBc||j}|jjj|jt|\}}|jddS)z(Enable PIN code using specified PIN codezCHV enable successfulN) rrr7rar enable_chvrrrrs rA do_enable_chvzIso7816Commands.do_enable_chvps\mmDM**Y_(33DKSJJ r 122222rBcR|jjj\}}dS)z$Deactivate the currently selected EFN)r7rardeactivate_filers rAdo_deactivate_filez"Iso7816Commands.do_deactivate_filews"Y_(88:: rrrrBNAMEz$File name or FID of file to activatecT|jj|j\}}dS)aActivate the specified EF by sending an ACTIVATE FILE apdu command (used to be called REHABILITATE in TS 11.11 for classic SIM). This command is used to (re-)activate a file that is currently in deactivated (sometimes also called "invalidated") state. You need to call this from the DF above the to-be-activated EF and specify the name or FID of the file to activate. Note that for *deactivation* the to-be-deactivated EF must be selected, but for *activation*, the DF above the to-be-activated EF must be selected!N)r7ra activate_filerrs rAdo_activate_filez Iso7816Commands.do_activate_file}s%Y_2249== rrrrBcd|jjji}|j|||||S)z-Command Line tab completion for ACTIVATE FILErrrrs rAcomplete_activate_filez&Iso7816Commands.complete_activate_filerrBchan_nrrzChannel Numberc|jjjd|j\}}|jj|jdS)zOpen a logical channel.openmoderN)r7rarmanage_channelr add_lchanrs rAdo_open_channelzIso7816Commands.do_open_channelsRY_(77$,800 r !!$,/////rBc|jjjd|j\}}|jj|jdS)zClose a logical channel.closerN)r7rarrrr` del_lchanrs rAdo_close_channelz Iso7816Commands.do_close_channelsRY_(774<811 r t|,,,,,rBc|jj|j|jjj|j|j_|jj|j|jdS)z(Switch currently active logical channel.N)r7ra _select_prer`r _select_postrrs rAdo_switch_channelz!Iso7816Commands.do_switch_channelsi ##DI...),,T\:  $$TY/// !!!!!rBcv|jj}|j|dS)zPerform the STATUS command.N)r7rastatusr)r?rrYs rA do_statuszIso7816Commands.do_statuss3)/((** w'''''rB)&rDrErFrNrrrHrrrrverify_chv_parserrrr%r:r runblock_chv_parserrchange_chv_parserrdisable_chv_parserrenable_chv_parserrractivate_file_parserrropen_chan_parserrclose_chan_parserrswitch_chan_parserrrrIrJs@rAr}r}sZ ( ( (aT#Yaaaa  0/11""a.d#fff""*{#}}}T*++99,+91022##a.d$fff##*z$|||##Z.~$@@@T+,,44-,40/11""a.d#fff""*z#|||""Z.~#@@@T*++33,+31022##a.d$fff##*{$}}}T+,,44-,4 0/11""a.d#fff""*{#}}}T*++33,+3 ;;;38244%%f3=c%dddT-.. > >/. >aDIaaaa /x.00!!Q-="???T)**00+*00/11""Q-=#???T*++--,+-1022##Q-=$???T+,,""-,"(((((((rBr}ceZdZdefdZdS)Proactpcmdc.t|jdSrC)rdecoded)r?rs rA receive_fetchzProact.receive_fetchs dlrBN)rDrErFr-r rgrBrArrs0"2rBrzinteractive SIM card shell)rQformatter_classzGeneral Optionsz--scriptPATHzIscript with pySim-shell commands to be executed automatically at start-up)metavarrrz--csvFILEzRead card data from CSV filez--csv-column-keyzFIELD:AES_KEY_HEXrJz per-CSV-column AES transport key)r rrrz--card_handlercard_handler_configz#Use automatic card handling machine)destr rz-az --pin-admPIN_ADM1rz2ADM PIN used for provisioning (overwrites default))r rrrz-Az --pin-adm-hex PIN_ADM1_HEX pin_adm_hexzAADM PIN used for provisioning, as hex string (16 characters long)commandrzBA pySim-shell command that would optionally be executed at startup)r r command_argszOptional Arguments for command__main__rr:z/.osmocom/pysim/card_data.csv)proactive_handlerrrzO(you may still try to recover from this manually by using the 'equip' command.)zL it should also be noted that some readers may behave strangely when no cardz is inserted.)rzBwill not execute startup script due to card initialization errors!zACard error, cannot do ADM verification with supplied ADM pin now.z{} {}r)typingrrrrr: packagingrrr9r;rrredri bright_redr bright_greenrr r r r r rrrinspectpathlibriorrpppySim.exceptionspySim.transportrrrr pySim.utilsrrrrrrrrrrr r!r"r#r$r%pySim.card_handlerr&r'pySim.filesystemr(r)r*pySim.ts_102_222r+ pySim.gsm_rr, pySim.catr-pySim.card_key_providerr.r/r0 pySim.appr1Cmdr3SettablerLrUr~r}rrArgumentDefaultsHelpFormatter option_parseradd_argument_group global_groupradd_mutually_exclusive_group adm_group REMAINDERrD parse_argsrrurrrexitcsv_column_keyscsv_column_keyparsplitrOrrHhome csv_defaultcsvr<isfilermrrnr`r_apprrrrrrrIrZronecmd_plus_hooksformatr rcmdlooprgrBrArCs+ ("!!!!!!!  7=!""]W]7%;%;;; &C I/KK &C I.KBBBBBBBBBB ____________bbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaCCCCCCCCCC;;;;;;;;4444444444------!!!!!!&&&&&&oooooooooo11111111UUUUUdmUUUs>s>s>s>s>zs>s>s>j '((x.x.x.x.x.Jx.x.)(x.t )**p(p(p(p(p(jp(p(+*p(f ('4P8@8^``` '''//0ABB *fdjlll '6"&-KMMM ,6ISU^fACCC *1FPVDFFF  5 5 7 7 t[*9VZPRRR t_n=bf_aaa9C dfff >1C @BBB z  # # % %D {rybg..  E( ) ) ) CHQKKKO"$$IIcNN c ##idikk""%DDK xR""#5#5dh#P#PQQQ w~~k""U""#5#5k?#S#STTT Tvvxx 8 8 8B  _T4#; < < [__ +9R==DhtRR55 + BSSWWLMMM <===  <=== _```  Z \ \ \  b ; X EV W W WhtT2r**t|T-=>>G W EU V V V  I !2CCLL A A A A    E!HHHHHHHH  | gnnT\388DDU;V;VWWXXXXX Gs% "PB R8)S""S<' S77S<