Your magic box would be an electronic circuit breaker, with different current limits if you want. You may want to consider the following video https://m.youtube.com/watch?v=fqeUpATJlZY which presents the basics of a current limit circuit that can be applied to any circuit. The part2 and part3, although having a different name (look in the video description for their links), builds on it with the final solution presented in part 3. You will have to change the pass transistor to a big one that can accept a higher current and still have a saturation Vce of 0.7V or less. The 2N3055 (or MJ2955 for the part 2 video circuit) can be driven at Ic=4A with an Ib=200mA. The 4A will be below the 5A DUT capacity and still allow for a short circuit test on the DUT.
EDIT: You are right SamGibson, I have linked the video because I find it very instructional, and I apologize for that.
Anyway here are the schematics of my answer:
RB needs to have a current value that keeps the VCE of Q1 below 0.7V.
For that you can use the transistor base current, collector voltage like this one:
RT needs to be such that will keep the VCE of Q2 also below 0.7V so there is not much heat dissipation in it. For that you use its base current, collector voltage like this one:
And here are the results of an ngspice test with RB=250R and RT=10K:
Before the circuit triggers:
Node Voltage
vin 40
vb1 39.1217
vb2 39.3187
vout 39.2447
vin#branch -4.08096
After the circuit triggers:
Node Voltage
vin 40
vb1 39.6644
vb2 39.1545
vout 0.0352077
vin#branch -0.16257