match (source:Method {NAME:"readObject",CLASSNAME:"java.util.HashMap"}) match (sink:Method {NAME0:"com.example.b4bycoffee.model.CoffeeBean.toString"}) with source, collect(sink) as sinks call tabby.algo.findJavaGadget(source, sinks, 8, false, false) yield path return path limit 1
再根据结果排除一些误报:
1 2 3 4 5 6 7 8
match (source:Method {NAME:"readObject",CLASSNAME:"java.util.HashMap"}) match (sink:Method {NAME0:"com.example.b4bycoffee.model.CoffeeBean.toString"}) with source, collect(sink) as sinks call tabby.algo.findJavaGadget(source, sinks, 8, false, false) yield path where none(n in nodes(path) where n.NAME0 in ["com.sun.xml.internal.ws.api.BindingID.equals","org.yaml.snakeyaml.events.Event.equals","com.sun.corba.se.spi.orb.OperationFactory$OperationBase.equals","org.springframework.cache.interceptor.CacheOperation.equals","javax.swing.text.html.HTML$UnknownTag.equals"] ) return path limit 1
match (source:Method {NAME:"readObject",CLASSNAME:"java.util.HashMap"}) match (sink:Method {NAME0:"com.example.b4bycoffee.model.CoffeeBean.toString"}) with source, collect(sink) as sinks call tabby.algo.findJavaGadget(source, sinks, 8, false, false) yield path where none(n in nodes(path) where n.NAME0 in ["com.sun.xml.internal.ws.api.BindingID.equals","org.yaml.snakeyaml.events.Event.equals","com.sun.corba.se.spi.orb.OperationFactory$OperationBase.equals","org.springframework.cache.interceptor.CacheOperation.equals","javax.swing.text.html.HTML$UnknownTag.equals"] ) and none(n in nodes(path) where (:Method{ NAME0:"java.util.HashMap.putVal" })-[:CALL]->(:Method{NAME0:"java.lang.Object.equals"})-[:ALIAS]->(n) and n.NAME0 <> "org.springframework.aop.target.HotSwappableTargetSource.equals" ) return path limit 1
再排除一些误报:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
match (source:Method {NAME:"readObject",CLASSNAME:"java.util.HashMap"}) match (sink:Method {NAME0:"com.example.b4bycoffee.model.CoffeeBean.toString"}) with source, collect(sink) as sinks call tabby.algo.findJavaGadget(source, sinks, 8, false, false) yield path where none(n in nodes(path) where n.NAME0 in ["com.sun.xml.internal.ws.api.BindingID.equals","org.yaml.snakeyaml.events.Event.equals","com.sun.corba.se.spi.orb.OperationFactory$OperationBase.equals","org.springframework.cache.interceptor.CacheOperation.equals","javax.swing.text.html.HTML$UnknownTag.equals"] ) and none(n in nodes(path) where (:Method{ NAME0:"java.util.HashMap.putVal" })-[:CALL]->(:Method{NAME0:"java.lang.Object.equals"})-[:ALIAS]->(n) and n.NAME0 <> "org.springframework.aop.target.HotSwappableTargetSource.equals" ) and none(n in nodes(path) where n.CLASSNAME in ["com.sun.corba.se.spi.orb.OperationFactory$OperationBase"] ) return path limit 1
查询出了一开始人工审的那条链:
排除这条链,排除题目中的黑名单,增大maxNodeLength,继续查询:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
match (source:Method {NAME:"readObject",CLASSNAME:"java.util.HashMap"}) match (sink:Method {NAME0:"com.example.b4bycoffee.model.CoffeeBean.toString"}) with source, collect(sink) as sinks call tabby.algo.findJavaGadget(source, sinks, 12, false, false) yield path where none(n in nodes(path) where n.NAME0 in ["com.sun.xml.internal.ws.api.BindingID.equals","org.yaml.snakeyaml.events.Event.equals","com.sun.corba.se.spi.orb.OperationFactory$OperationBase.equals","org.springframework.cache.interceptor.CacheOperation.equals","javax.swing.text.html.HTML$UnknownTag.equals","com.rometools.rome.feed.impl.EqualsBean.hashCode"] ) and none(n in nodes(path) where (:Method{ NAME0:"java.util.HashMap.putVal" })-[:CALL]->(:Method{NAME0:"java.lang.Object.equals"})-[:ALIAS]->(n) and n.NAME0 <> "org.springframework.aop.target.HotSwappableTargetSource.equals" ) and none(n in nodes(path) where n.CLASSNAME in ["com.sun.corba.se.spi.orb.OperationFactory$OperationBase","javax.management.BadAttributeValueExpException","com.rometools.rome.feed.impl.ObjectBean","com.rometools.rome.feed.impl.ToStringBean","com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl"] ) return path limit 1
运行了好一会没有跑出结果
那么放宽我们source的条件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
match (source:Method {NAME:"readObject"}) match (sink:Method {NAME0:"com.example.b4bycoffee.model.CoffeeBean.toString"}) with source, collect(sink) as sinks call tabby.algo.findJavaGadget(source, sinks, 8, false, false) yield path where none(n in nodes(path) where n.NAME0 in ["com.sun.xml.internal.ws.api.BindingID.equals","org.yaml.snakeyaml.events.Event.equals","com.sun.corba.se.spi.orb.OperationFactory$OperationBase.equals","org.springframework.cache.interceptor.CacheOperation.equals","javax.swing.text.html.HTML$UnknownTag.equals","com.rometools.rome.feed.impl.EqualsBean.hashCode"] ) and none(n in nodes(path) where (:Method{ NAME0:"java.util.HashMap.putVal" })-[:CALL]->(:Method{NAME0:"java.lang.Object.equals"})-[:ALIAS]->(n) and n.NAME0 <> "org.springframework.aop.target.HotSwappableTargetSource.equals" ) and none(n in nodes(path) where n.CLASSNAME in ["com.sun.corba.se.spi.orb.OperationFactory$OperationBase","javax.management.BadAttributeValueExpException","com.rometools.rome.feed.impl.ObjectBean","com.rometools.rome.feed.impl.ToStringBean","com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl"] ) return path limit 1
match (c:Class) where ( (c)-[:EXTENDS*]->(:Class{NAME:"java.awt.Window"}) or c.NAME="java.awt.Window" or (c)-[:EXTENDS*]->(:Class{NAME:"java.awt.GraphicsConfiguration"}) or c.NAME="java.awt.GraphicsConfiguration" ) and ( (c)-[:HAS]->(:Method{NAME:"valueOf"}) or (c)-[:HAS]->(:Method{NAME:"toString"}) ) return c.NAME
match (source:Method {NAME:"readObject"}) match (sink:Method {NAME0:"com.example.b4bycoffee.model.CoffeeBean.toString"}) with source, collect(sink) as sinks call tabby.algo.findJavaGadget(source, sinks,6, false, false) yield path where any(n in nodes(path) where (:Method{ NAME0:"org.springframework.util.MimeType.getParameter" })-[:CALL]->()-[:ALIAS]->()-[:ALIAS]->(n) and n.NAME0 = "javax.swing.UIDefaults$TextAndMnemonicHashMap.get" ) return path limit 1
match (source:Method {NAME:"readObject"}) match (sink:Method {NAME0:"com.example.b4bycoffee.model.CoffeeBean.toString"}) with source, collect(sink) as sinks call tabby.algo.findJavaGadget(source, sinks,5, false, false) yield path where any(n in nodes(path) where (n)-[:CALL]->(:Method{NAME0:"java.util.Hashtable.put"})-[:ALIAS]->(:Method{NAME0:"java.security.Provider.put"}) and n.NAME0 = "javax.swing.text.html.CSS.readObject" ) return path limit 1
(n)-[:CALL]->(:Method{NAME0:"java.util.Hashtable.put"})-[:ALIAS]->(:Method{NAME0:"java.security.Provider.put"}) and n.NAME0 = "javax.swing.text.html.CSS.readObject"
match (source:Method {NAME:"readObject"}) match (sink:Method {NAME0:"com.example.b4bycoffee.model.CoffeeBean.toString"}) with source, collect(sink) as sinks call tabby.algo.findJavaGadget(source, sinks,5, false, false) yield path where any(n in nodes(path) where n.NAME0 = "javax.swing.text.html.CSS.readObject" ) return path limit 1
none(n in nodes(path) where (:Method{ NAME0:"java.util.HashMap.putVal" })-[:CALL]->(:Method{NAME0:"java.lang.Object.equals"})-[:ALIAS]->(n) and n.NAME0 <> "org.springframework.aop.target.HotSwappableTargetSource.equals" )
测试的时候发现可以关闭Neo4j设置中的 “Connect result nodes”,这样查询结果就只会显示调用路径,而不会显示多余的关系边
1 2 3 4 5 6 7 8 9 10 11 12
match (source:Method {NAME:"readObject"}) match (sink:Method {NAME0:"com.example.b4bycoffee.model.CoffeeBean.toString"}) with source, collect(sink) as sinks call tabby.algo.findJavaGadget(source, sinks,6, false, false) yield path where none(n in nodes(path) where n.NAME0 in ["com.sun.xml.internal.ws.api.BindingID.equals","org.yaml.snakeyaml.events.Event.equals","com.sun.corba.se.spi.orb.OperationFactory$OperationBase.equals","org.springframework.cache.interceptor.CacheOperation.equals","javax.swing.text.html.HTML$UnknownTag.equals","com.rometools.rome.feed.impl.EqualsBean.hashCode","java.awt.Window.setGraphicsConfiguration","java.awt.KeyboardFocusManager.setMostRecentFocusOwner","org.apache.logging.log4j.spi.AbstractLogger.readObject","javax.swing.JTree.unarchiveExpandedState","javax.swing.tree.TreeSelectionModel.addTreeSelectionListener","javax.swing.tree.TreeModel.addTreeModelListener","java.lang.Throwable.initCause","javax.swing.event.EventListenerList.add","java.security.Provider.putId","java.util.EnumMap.typeCheck","java.awt.datatransfer.DataFlavor.readExternal"] ) and none(n in nodes(path) where n.CLASSNAME in ["com.sun.corba.se.spi.orb.OperationFactory$OperationBase","javax.management.BadAttributeValueExpException","com.rometools.rome.feed.impl.ObjectBean","com.rometools.rome.feed.impl.ToStringBean","com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl"] )and none(n in nodes(path) where n.NAME0 in ["javax.swing.JTree.readObject","java.text.SimpleDateFormat.readObject","org.springframework.util.MimeType.readObject","org.apache.logging.log4j.message.ObjectMessage.equals","javax.naming.directory.BasicAttributes.equals"] ) return path limit 1