File size: 3,212 Bytes
3ef37b3
12102c5
 
92387b0
21fd4fa
12102c5
 
59b7cd2
12102c5
59b7cd2
12102c5
 
 
 
 
 
 
cc2e390
21fd4fa
 
 
 
 
92387b0
 
 
 
101246c
21fd4fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59b7cd2
 
01f3963
12102c5
 
59b7cd2
12102c5
 
 
92387b0
21fd4fa
12102c5
59b7cd2
 
 
 
92387b0
101246c
92387b0
6e92fb6
92387b0
 
 
 
 
01f3963
92387b0
 
 
 
01f3963
92387b0
 
101246c
92387b0
12102c5
 
92387b0
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import streamlit as st
import os
import subprocess
import shutil
import requests

# Function to process APK file
def process_apk(input_path, output_dir):
    # Run apk-mitm command
    command = f"apk-mitm {input_path} -o {output_dir}"
    result = subprocess.run(command, shell=True, capture_output=True, text=True)
    return result

# Streamlit app interface
st.title("APK File Processor")

# File upload
uploaded_file = st.file_uploader("Upload APK file", type=['apk', 'xapk', 'apks'])

# URL upload
url_input = st.text_input("Or enter APK URL")

if uploaded_file is not None or url_input:
    # Create directories if they don't exist
    upload_dir = "uploads"
    if not os.path.exists(upload_dir):
        os.makedirs(upload_dir)

    if uploaded_file is not None:
        # Save uploaded file
        input_path = os.path.join(upload_dir, uploaded_file.name)
        with open(input_path, "wb") as f:
            f.write(uploaded_file.read())
    elif url_input:
        # Download APK from URL
        st.write("Downloading APK from URL...")
        response = requests.get(url_input)
        if response.status_code == 200:
            input_path = os.path.join(upload_dir, os.path.basename(url_input))
            with open(input_path, "wb") as f:
                f.write(response.content)
        else:
            st.error("Failed to download APK from URL. Please check the URL and try again.")
            st.stop()

    # Define output directory for the patched APK
    output_dir = upload_dir

    # Process APK
    st.write("Processing APK...")
    result = process_apk(input_path, output_dir)
    
    if result.returncode == 0:
        st.success("APK processed successfully!")
        st.write("Processing result:")
        st.text(result.stdout)
        
        # Extract the patched APK file name from the stdout
        output_file_name = result.stdout.split("Patched file: ")[-1].strip()
        output_path = os.path.join(output_dir, output_file_name)
        
        # Check if the processed APK file exists
        if os.path.exists(output_path):
            st.write(f"Processed APK file found at: {output_path}")
            
            # Provide download link for the patched APK file
            with open(output_path, "rb") as f:
                file_data = f.read()
                if file_data:
                    st.write("File data read successfully.")
                    st.download_button(
                        label="Download Patched APK",
                        data=file_data,
                        file_name=os.path.basename(output_path),
                        mime="application/vnd.android.package-archive"
                    )
                else:
                    st.error("Failed to read file data.")
        else:
            st.error("Processed APK file not found. Please try again.")
    else:
        st.error(f"Error processing APK: {result.stderr}")
    
    # Clean up the uploaded and processed files
    def cleanup_files(input_path, output_path):
        if os.path.exists(input_path):
            os.remove(input_path)
        if os.path.exists(output_path):
            os.remove(output_path)

    cleanup_files(input_path, output_path)