There is such a thing as complementarity of interests uniting even presumably sworn enemies. And it is not new if you remember the Iran-Contra affair when the US shipped weapons to Iran. There is also a certain continuum that runs through humanity. ISIS members have friends and acquaintances in other countries even in the officialdom especially in the intelligence community. These are able to establish informal channels to supply oil. A good example of a clandestine affair is that of the Pakistani intelligence and the Afghan Taliban. So when push comes to shove, there are no permanent foes, only rivals. This is cold hard international realpolitik at its element.