 Find all real parameters $p$ for which the equation $x^3 -2p(p+1)x^2+(p^4 +4p^3 -1)x-3p^3 = 0$
has three distinct real roots which are sides of a right triangle.

Given the polynomial equation \(x^3 - 2p(p+1)x^2 + (p^4 + 4p^3 - 1)x - 3p^3 = 0\), we need to find all real parameters \(p\) for which the equation has three distinct real roots that are the sides of a right triangle.

Let \(a, b, c\) be the roots of the equation such that \(a^2 + b^2 = c^2\). Using Viète's formulas, we have:
\[
\begin{cases}
a + b + c = 2p(p+1), \\
ab + bc + ca = p^4 + 4p^3 - 1, \\
abc = 3p^3.
\end{cases}
\]

1. **Express \(a^2 + b^2 + c^2\) using Viète's formulas:**

   Squaring the sum of the roots:
   \[
   (a + b + c)^2 = [2p(p+1)]^2 = 4p^2(p+1)^2.
   \]
   Expanding and using the identity \(a^2 + b^2 + c^2 = (a + b + c)^2 - 2(ab + bc + ca)\):
   \[
   a^2 + b^2 + c^2 = 4p^2(p+1)^2 - 2(p^4 + 4p^3 - 1).
   \]

2. **Simplify the expression for \(a^2 + b^2 + c^2\):**

   \[
   a^2 + b^2 + c^2 = 4p^2(p^2 + 2p + 1) - 2(p^4 + 4p^3 - 1).
   \]
   \[
   a^2 + b^2 + c^2 = 4p^4 + 8p^3 + 4p^2 - 2p^4 - 8p^3 + 2.
   \]
   \[
   a^2 + b^2 + c^2 = 2p^4 + 4p^2 + 2.
   \]

3. **Use the right triangle condition \(a^2 + b^2 = c^2\):**

   Since \(a^2 + b^2 = c^2\), we have:
   \[
   a^2 + b^2 + c^2 = 2c^2.
   \]
   Equating the two expressions for \(a^2 + b^2 + c^2\):
   \[
   2c^2 = 2p^4 + 4p^2 + 2.
   \]
   \[
   c^2 = p^4 + 2p^2 + 1.
   \]

4. **Solve for \(c\):**

   \[
   c = \pm (p^2 + 1).
   \]
   Since \(c\) is a side length of a triangle, it must be positive:
   \[
   c = p^2 + 1.
   \]

5. **Find \(a\) and \(b\):**

   Using \(a + b = 2p(p+1) - c\) and \(ab = \frac{3p^3}{c}\):
   \[
   a + b = 2p(p+1) - (p^2 + 1) = p^2 + 2p - 1.
   \]
   \[
   ab = \frac{3p^3}{p^2 + 1}.
   \]

6. **Apply the triangle inequality \(a + b > c\):**

   \[
   p^2 + 2p - 1 > p^2 + 1.
   \]
   \[
   2p - 1 > 1.
   \]
   \[
   p > 1.
   \]

7. **Verify the consistency of the roots:**

   Using the identity \((p^2 + 1)^2 = (p^2 + 2p - 1)^2 - \frac{6p^3}{p^2 + 1}\):
   \[
   (p^2 + 1)^2 = (p^2 + 2p - 1)^2 - \frac{6p^3}{p^2 + 1}.
   \]
   Simplifying:
   \[
   (p^2 + 1)^3 = (p^2 + 1)(p^2 + 2p - 1)^2 - 6p^3.
   \]
   \[
   -4p^5 + 6p^3 + 4p = 0.
   \]
   Dividing by \(p\) (neglecting \(p = 0\)):
   \[
   -4p^4 + 6p^2 + 4 = 0.
   \]
   Factoring:
   \[
   -2(p^2 - 2)(2p^2 + 1) = 0.
   \]
   The only solution satisfying \(p > 1\) is:
   \[
   p = \sqrt{2}.
   \]

The final answer is \( \boxed{ p = \sqrt{2} } \).