3
O+e[: @ s d dl Z d dlZd dlmZ d dlZd dlmZmZ ddl m
Z
mZ ddlm
Z
ddlmZ dgZG d d dZG d
d dZdS ) N)suppress)
NetworkXErrorNetworkXNoPath )EdgeAttrIntMaxEdgeAttrList)RuleNotConditional)
TERuletypeInfoFlowAnalysisc @ s e Zd ZdZd!ddZedd Zejdd Zed d
Zejdd
Zedd
Z e jdd
Z dd Z
d"ddZdd Zd#ddZ
dd Zdd Zdd Zdd ZdS )$r
zInformation flow analysis.r Nc C sJ t jt| _|| _|| _|| _|| _|| _d| _ d| _
tj | _
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_mapexcludebooleansrebuildgraphrebuildsubgraphnxZDiGraphGsubG)selfr r r r r r /usr/lib64/python3.6/infoflow.py__init__% s
zInfoFlowAnalysis.__init__c C s | j S )N)_min_weight)r r r r r A s zInfoFlowAnalysis.min_weightc C s0 d| kodkn s t d|| _d| _d S )Nr
z4Min information flow weight must be an integer 1-10.T)
ValueErrorr r )r weightr r r r E s
c C s | j S )N) _perm_map)r r r r r N s zInfoFlowAnalysis.perm_mapc C s || _ d| _d| _d S )NT)r r r )r r r r r r R s c C s | j S )N)_exclude)r r r r r X s zInfoFlowAnalysis.excludec s* |r fdd|D _ ng _ d _d S )Nc s g | ]} j j|qS r )r lookup_type).0t)r r r
_ s z,InfoFlowAnalysis.exclude..T)r! r )r typesr )r r r \ s c c sj | j j|}| j j|}| jr&| j | jjdj|| tt | j t
j| j||V W dQ R X dS )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)
r r" r _build_subgraphr
infoformatr r !_InfoFlowAnalysis__generate_stepsr
shortest_pathr )r sourcetargetsr$ r r r r+ e s
zInfoFlowAnalysis.shortest_path c c s |dk rt d| jj|}| jj|}| jr6| j | jjdj||| tt . x&t
j| j|||D ]}| j
|V qjW W dQ R X dS )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.
r z%Maximum path length must be positive.zHGenerating all information flow paths from {0} to {1}, max length {2}...N)r r r" r r' r
r( r) r r r Zall_simple_pathsr r* )r r, r- maxlenr. r$ pathr r r all_paths s
zInfoFlowAnalysis.all_pathsc c sx | j j|}| j j|}| jr&| j | jjdj|| tt, x$t j
| j||D ]}| j|V qVW W dQ R X dS )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)
r r" r r' r
r( r) r r r all_shortest_pathsr r* )r r, r- r. r$ r1 r r r r3 s
z#InfoFlowAnalysis.all_shortest_pathsTc c s | j j|}| jr| j | jjdj|r,dnd| ttF |rR| j j
|}n| j j|}x |D ]\}}t| j ||V qdW W dQ R X dS )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)
r r" r r' r
r( r) r r r Z out_edgesZin_edgesEdge)r Ztype_outr. Zflowsr, r- r r r infoflows s
zInfoFlowAnalysis.infoflowsc C s | j r| j tj| jS )zQ
Get the information flow graph statistics.
Return: str
)r _build_graphr r( r )r r r r get_stats s zInfoFlowAnalysis.get_statsc c s8 x2t dt|D ] }t| j||d || V qW dS )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.
r N)rangelenr4 r )r r1 r. r r r Z__generate_steps s z!InfoFlowAnalysis.__generate_stepsc C s, | j j dj| j| j _| jj| j | jjdj| j x| jj D ]}|j
tjkrZqH| jj
|\}}x|tj|jj |jj D ]`\}}||kr|rt| j ||dd}|jj| ||_|rt| j ||dd}|jj| ||_qW qHW d| _d| _| jjd | jjdjtj| j tj| j d S )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}.)r clearr) r namer Z
map_policyr
r( ZterulesZruletyper ZallowZrule_weight itertoolsproductr, expandr- r4 rulesappendr r r debugr number_of_nodesnumber_of_edges)r ruleZrweightZwweightr. r$ edger r r r7 & s0
"
zInfoFlowAnalysis._build_graphc s j r j jjd jjdj j jjdj j jjdj jd k fdd j j
D } j j|j _
jdkrg }x: j
j D ],\}}t j
||}|j jk r|j| qW j
j| jd k rg }x j
j D ]\}}t j
||}g }x*|jD ] }|jf js|j| qW g }x.|D ]&}||krF|jj| |j| qFW |js|j| qW j
j| d _ jjd jjd
jtj j
tj j
d S )Nz%Building information flow subgraph...zExcluding {0!r}zMin weight {0}z(Exclude disabled conditional policy: {0}c s g | ]}| j kr|qS r )r )r# n)r r r r% T s z4InfoFlowAnalysis._build_subgraph..r Fz-Completed building information flow subgraph.z'Subgraph stats: nodes: {0}, edges: {1}.)r r7 r
r( rC r) r r r r nodesZsubgraphcopyr Zedgesr4 r rB Zremove_edges_fromrA Zenabledremover r rD rE ) r rI Zdelete_listr. r$ rG Z rule_listrF Z
deleted_rulesr )r r r' I sJ
z InfoFlowAnalysis._build_subgraph)r NN)r/ )T)r
__module____qualname____doc__r propertyr setterr r r+ r2 r3 r6 r8 r* r7 r' r r r r r
! s
!
("
' #c @ s: e Zd ZdZedZedZdddZdd Z d d
Z
dS )
r4 aR
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.
rA ZcapacityFc C sP || _ || _|| _| j j||sL|rD| j j||dd d | _d | _ntdd S )Nr )r zEdge does not exist in graph)r r, r- Zhas_edgeZadd_edgerA r r )r Zgraphr, r- r; r r r r s z
Edge.__init__c s4 t |tr&