All perfect praise be to Allah the Lord of the Worlds. May His peace and blessings be upon Prophet Mohammad and upon all his family and companions.
Similar to other contracts, a sale contract has pillars and conditions that must be met in order for it to be considered valid. One of these conditions is knowledge of what is being sold, by sight, and each commodity has its own features. However, if what is being sold isn`t known by sight, this sale contract is considered invalid and unconcluded. Imam Ibn An-Naqeeb said about invalid sale contracts: "It isn`t permissible to sell that which isn`t specified, such as selling one of two salves, nor that which is unseen, such as selling a horse that is in a stable."{Umdat As-Salik, P.150}.
When what is being sold is unseen, even if it is present in the council where the contract is concluded, the sale is invalidated. It is stated in the Shafie book {Nehayat Az-Zain, P.226}: "It isn`t permissible to sell, in terms of price or commodity, that which hasn`t been seen by one of the contracting parties, even if it was present in the same council."
The form of sale mentioned in the above question involves two cases:
First: What is being sold is the box itself and its contents are granted to the buyer as a prize; in which case this sale contract is valid because what is being sold and its price are known.
Second: If the contents of the box are what is being sold, then they are unseen and unknown, so this is a kind of deceptive sale such as was forbidden in the Hadith of Abu Hurayrah, who narrated that the Prophet (Peace and blessings be upon him) forbade sales of deception. For more information, kindly check {Asna Al-Matalib Fi Shareh Rawd At-Talib, Vol.2:P.11}.
Here, deception lies in the fact that the contents as well as the value of the boxes are unknown. Accordingly, their value could be higher or lesser than the paid price, which renders this sale invalid under Sharia.
In conclusion, it isn`t permissible to sell or buy these random boxes because the conditions of valid sale are lacking in addition to being a kind of deceptive sale forbidden under Sharia. And Allah the Almighty knows best.