3 O+e[:@sddlZddlZddlmZddlZddlmZmZddl m Z m Z ddl m Z ddlmZdgZGd ddZGd d d ZdS) N)suppress) NetworkXErrorNetworkXNoPath)EdgeAttrIntMax EdgeAttrList)RuleNotConditional) TERuletypeInfoFlowAnalysisc@seZdZdZd!ddZeddZejddZed d Zejd d Zed d Z e jdd Z ddZ d"ddZ ddZ d#ddZ ddZddZddZdd ZdS)$r zInformation flow analysis.rNcCsJtjt|_||_||_||_||_||_d|_ d|_ t j |_ d|_dS)a Parameters: policy The policy to analyze. perm_map The permission map or path to the permission map file. minweight The minimum permission weight to include in the analysis. (default is 1) exclude The types excluded from the information flow analysis. (default is none) booleans If None, all rules will be added to the analysis (default). otherwise it should be set to a dict with keys corresponding to boolean names and values of True/False. Any unspecified booleans will use the policy's default values. TN)loggingZ getLogger__name__logpolicy min_weightperm_mapexcludebooleans rebuildgraphrebuildsubgraphnxZDiGraphGsubG)selfrrrrrr /usr/lib64/python3.6/infoflow.py__init__%s  zInfoFlowAnalysis.__init__cCs|jS)N) _min_weight)rrrrrAszInfoFlowAnalysis.min_weightcCs0d|kodkns td||_d|_dS)Nr z4Min information flow weight must be an integer 1-10.T) ValueErrorrr)rweightrrrrEs cCs|jS)N) _perm_map)rrrrrNszInfoFlowAnalysis.perm_mapcCs||_d|_d|_dS)NT)r rr)rrrrrrRscCs|jS)N)_exclude)rrrrrXszInfoFlowAnalysis.excludecs*|rfdd|D_ng_d_dS)Ncsg|]}jj|qSr)r lookup_type).0t)rrr _sz,InfoFlowAnalysis.exclude..T)r!r)rtypesr)rrr\sc csj|jj|}|jj|}|jr&|j|jjdj||tt|j t j |j ||VWdQRXdS)a Generator which yields one shortest path between the source and target types (there may be more). Parameters: source The source type. target The target type. Yield: generator(steps) steps Yield: tuple(source, target, rules) source The source type for this step of the information flow. target The target type for this step of the information flow. rules The list of rules creating this information flow step. z@Generating one shortest information flow path from {0} to {1}...N) rr"r_build_subgraphr infoformatrr!_InfoFlowAnalysis__generate_stepsr shortest_pathr)rsourcetargetsr$rrrr+es     zInfoFlowAnalysis.shortest_pathc cs|dkrtd|jj|}|jj|}|jr6|j|jjdj|||tt .x&t j |j |||D]}|j |VqjWWdQRXdS)a Generator which yields all paths between the source and target up to the specified maximum path length. This algorithm tends to get very expensive above 3-5 steps, depending on the policy complexity. Parameters: source The source type. target The target type. maxlen Maximum length of paths. Yield: generator(steps) steps Yield: tuple(source, target, rules) source The source type for this step of the information flow. target The target type for this step of the information flow. rules The list of rules creating this information flow step. rz%Maximum path length must be positive.zHGenerating all information flow paths from {0} to {1}, max length {2}...N)rrr"rr'r r(r)rrrZall_simple_pathsrr*)rr,r-maxlenr.r$pathrrr all_pathss     zInfoFlowAnalysis.all_pathsc csx|jj|}|jj|}|jr&|j|jjdj||tt,x$t j |j ||D]}|j |VqVWWdQRXdS)a Generator which yields all shortest paths between the source and target types. Parameters: source The source type. target The target type. Yield: generator(steps) steps Yield: tuple(source, target, rules) source The source type for this step of the information flow. target The target type for this step of the information flow. rules The list of rules creating this information flow step. zAGenerating all shortest information flow paths from {0} to {1}...N) rr"rr'r r(r)rrrall_shortest_pathsrr*)rr,r-r.r$r1rrrr3s     z#InfoFlowAnalysis.all_shortest_pathsTc cs|jj|}|jr|j|jjdj|r,dnd|ttF|rR|j j |}n |j j |}x |D]\}}t |j ||VqdWWdQRXdS)a( Generator which yields all information flows in/out of a specified source type. Parameters: source The starting type. Keyword Parameters: out If true, information flows out of the type will be returned. If false, information flows in to the type will be returned. Default is true. Yield: generator(steps) steps A generator that returns the tuple of source, target, and rules for each information flow. z(Generating all information flows {0} {1}zout ofZintoN) rr"rr'r r(r)rrrZ out_edgesZin_edgesEdge)rZtype_outr.Zflowsr,r-rrr infoflowss    zInfoFlowAnalysis.infoflowscCs|jr|jtj|jS)zQ Get the information flow graph statistics. Return: str )r _build_graphrr(r)rrrr get_statsszInfoFlowAnalysis.get_statsccs8x2tdt|D] }t|j||d||VqWdS)a Generator which returns the source, target, and associated rules for each information flow step. Parameter: path A list of graph node names representing an information flow path. Yield: tuple(source, target, rules) source The source type for this step of the information flow. target The target type for this step of the information flow. rules The list of rules creating this information flow step. rN)rangelenr4r)rr1r.rrrZ__generate_stepssz!InfoFlowAnalysis.__generate_stepscCs,|jjdj|j|j_|jj|j|jjdj|jx|jj D]}|j t j krZqH|jj |\}}x|tj|jj|jjD]`\}}||kr|rt|j||dd}|jj|||_|rt|j||dd}|jj|||_qWqHWd|_d|_|jjd|jjdjtj|jtj|jdS)NzInformation flow graph for {0}.z+Building information flow graph from {0}...T)createFz*Completed building information flow graph.z$Graph stats: nodes: {0}, edges: {1}.)rclearr)rnamerZ map_policyr r(ZterulesZruletyper ZallowZ rule_weight itertoolsproductr,expandr-r4rulesappendrrrdebugrnumber_of_nodesnumber_of_edges)rruleZrweightZwweightr.r$edgerrrr7&s0  "     zInfoFlowAnalysis._build_graphc sjrjjjdjjdjjjjdjjjjdjjdk fddj j D}j j |j _ jdkrg}x:j jD],\}}tj ||}|jjkr|j|qWj j|jdk rg}xj jD]\}}tj ||}g}x*|jD] }|jfjs|j|qWg}x.|D]&}||krF|jj||j|qFW|js|j|qWj j|d_jjd jjd jtjj tjj dS) Nz%Building information flow subgraph...zExcluding {0!r}zMin weight {0}z(Exclude disabled conditional policy: {0}csg|]}|jkr|qSr)r)r#n)rrrr%Tsz4InfoFlowAnalysis._build_subgraph..rFz-Completed building information flow subgraph.z'Subgraph stats: nodes: {0}, edges: {1}.)rr7r r(rCr)rrrrnodesZsubgraphcopyrZedgesr4rrBZremove_edges_fromrAZenabledremoverrrDrE) rrIZ delete_listr.r$rGZ rule_listrFZ deleted_rulesr)rrr'IsJ              z InfoFlowAnalysis._build_subgraph)rNN)r/)T)r __module__ __qualname____doc__rpropertyrsetterrrr+r2r3r6r8r*r7r'rrrrr !s      ! (" ' #c@s:eZdZdZedZedZd ddZddZ d d Z d S) r4aR A graph edge. Also used for returning information flow steps. Parameters: graph The NetworkX graph. source The source type of the edge. target The target type of the edge. Keyword Parameters: create (T/F) create the edge if it does not exist. The default is False. rAZcapacityFcCsP||_||_||_|jj||sL|rD|jj||ddd|_d|_ntddS)Nr)rzEdge does not exist in graph)rr,r-Zhas_edgeZadd_edgerArr)rZgraphr,r-r;rrrrsz Edge.__init__cs4t|tr&fddt|jdDSj|SdS)Ncsg|]}j|qSr)_index_to_item)r#i)rrrr%sz$Edge.__getitem__..r/) isinstanceslicer9indicesrQ)rkeyr)rr __getitem__s zEdge.__getitem__cCs.|dkr|jS|dkr|jStdj|dS)z'Return source or target based on index.rrz,Invalid index (edges only have 2 items): {0}N)r,r- IndexErrorr))rindexrrrrQs zEdge._index_to_itemN)F) r rLrMrNrrArrrrWrQrrrrr4s   r4)r>r contextlibrZnetworkxrZnetworkx.exceptionrrZ descriptorsrrZ exceptionrZ policyrepr __all__r r4rrrrs   b