Skip to content

Instantly share code, notes, and snippets.

@evilthreads669966
Created October 5, 2020 03:33
Show Gist options
  • Save evilthreads669966/724a343630c734de7939b3e419edeb3e to your computer and use it in GitHub Desktop.
Save evilthreads669966/724a343630c734de7939b3e419edeb3e to your computer and use it in GitHub Desktop.
proxy port scanner
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import java.io.File
import java.net.InetSocketAddress
import java.net.Proxy
import java.net.Proxy.Type.SOCKS
import java.net.Socket
import kotlin.random.Random
/*
( ( ) ( ( (
)\ ))\ ) * ) ( /( )\ ) ( )\ ) )\ )
( ( ( (()/(()/( ` ) /( )\()|()/(( )\ (()/( (()/(
)\ )\ )\ /(_))(_)) ( )(_)|(_)\ /(_))\((((_)( /(_)) /(_))
((_)((_)((_|_))(_)) (_(_()) _((_|_))((_))\ _ )(_))_ (_))
| __\ \ / /|_ _| | |_ _|| || | _ \ __(_)_\(_) \/ __|
| _| \ V / | || |__ | | | __ | / _| / _ \ | |) \__ \
|___| \_/ |___|____| |_| |_||_|_|_\___/_/ \_\|___/|___/
....................../´¯/)
....................,/¯../
.................../..../
............./´¯/'...'/´¯¯`·¸
........../'/.../..../......./¨¯\
........('(...´...´.... ¯~/'...')
.........\.................'...../
..........''...\.......... _.·´
............\..............(
..............\.............\...
*/
suspend fun main() = withContext(Dispatchers.Default){
val results = async{
scanner<IProbeable>("greenlightsocial.com")
}
System.out.println("Scan finished exposing open ports of ${results.await()}")
}
suspend inline fun <reified T: IProbeable> CoroutineScope.scanner(host: String, ports: IntRange = IntRange(1,65535)): Collection<T>{
val results = mutableListOf<T>()
kotlin.runCatching { }
ports.forEach { port ->
flow<T> {
val destination = InetSocketAddress(host, port)
Connection().apply {
if(Connection.host == null)
Connection.host = host
this.probe(port)
}.takeIf { conn -> conn.isOpen }?.let { conn -> emit(conn as T) }
delay(1000)
yield()
}.flowOn(Dispatchers.IO).onEach { conn ->
//do transaction with open port
with(conn){
this.print()
Logger.getLogger().write(this)
results.add(this)
}
}.launchIn(this)
}
return results
}
suspend inline fun Socket.grabBanner(): String{
val buf = StringBuffer()
this.getInputStream().use { input ->
input.bufferedReader().useLines { lines ->
lines.forEach { line ->
buf.appendLine(line)
}
}
}
return buf.toString()
}
interface IPrintable{
fun template(): String
fun print() = System.out.println(this.template())
}
interface IProbeable: IPrintable{
var isOpen: Boolean
var banner: String
suspend fun probe(port: Int)
}
data class Connection(var port: Int? = null, ): IProbeable{
override var isOpen: Boolean = false
override var banner: String = "Unknown"
companion object{
var host: String? = null
}
override fun template(): String{
var status = "OPEN"
if(!isOpen) status = "CLOSED"
return "${System.currentTimeMillis()} - $host:$port\\TCP $status $banner"
}
override suspend fun probe(port: Int) {
runCatching {
if(host == null) return
this.port = port
ProxySocketFactory.getDefault().createSocket{ sock ->
sock.connect(InetSocketAddress(host,port))
sock.grabBanner().takeIf { banner -> banner.isNotEmpty() }?.let { banner ->
this.banner = banner
}
}
}.onSuccess {
this.isOpen = true
}
}
}
interface IWritable<in T>{
suspend fun write(output: T)
}
class Logger: IWritable<IPrintable>{
private val writer = File("history-${System.currentTimeMillis()}.log").printWriter()
companion object{
private val INSTANCE by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { Logger() }
fun getLogger() = INSTANCE
}
override suspend fun write(probe: IPrintable) = writer.write("${probe.template()}\n")
}
typealias ProxyAddr = InetSocketAddress
class ProxySocketFactory{
companion object{
private val INSTANCE = ProxySocketFactory()
fun getDefault() = INSTANCE
}
suspend fun createSocket(operation: suspend (Socket) -> Unit ) = Socket(proxies.get(Random.nextInt(proxies.size))).use{ sock ->
operation(sock)
}
private val proxies = listOf<Proxy>(
Proxy(SOCKS, ProxyAddr("72.195.34.59", 4145)),
Proxy(SOCKS, ProxyAddr("98.188.47.132", 4145)),
Proxy(SOCKS, ProxyAddr("98.190.102.40",4145)),
Proxy(SOCKS, ProxyAddr("98.188.47.132",4145)),
Proxy(SOCKS, ProxyAddr("72.195.34.59",4145)),
Proxy(SOCKS, ProxyAddr("128.199.113.80",1080)),
Proxy(SOCKS, ProxyAddr("181.6.114.141",1080)),
Proxy(SOCKS, ProxyAddr("47.114.8.133",1080)),
Proxy(SOCKS, ProxyAddr("181.102.140.198",1080)),
Proxy(SOCKS, ProxyAddr("47.106.168.235",1080)),
Proxy(SOCKS, ProxyAddr("181.6.181.28",1080)),
Proxy(SOCKS, ProxyAddr("204.101.61.82",4145)),
Proxy(SOCKS, ProxyAddr("181.3.123.213",1080)),
Proxy(SOCKS, ProxyAddr("82.208.153.25",36918)),
Proxy(SOCKS, ProxyAddr("91.134.157.11",31913)),
Proxy(SOCKS, ProxyAddr("2.55.112.210",1080)),
Proxy(SOCKS, ProxyAddr("98.143.145.29",62354)),
Proxy(SOCKS, ProxyAddr("104.248.63.15",30588)),
Proxy(SOCKS, ProxyAddr("94.130.72.201",14395)),
Proxy(SOCKS, ProxyAddr("188.120.245.247",12432)),
Proxy(SOCKS, ProxyAddr("47.244.186.177",4576)),
Proxy(SOCKS, ProxyAddr("216.144.230.233",15993)),
Proxy(SOCKS, ProxyAddr("188.166.83.17",1080)),
Proxy(SOCKS, ProxyAddr("192.111.139.163",4145)),
Proxy(SOCKS, ProxyAddr("128.199.202.122",1080)),
Proxy(SOCKS, ProxyAddr("192.252.208.70",4145)),
Proxy(SOCKS, ProxyAddr("207.97.174.134", 1080)),
Proxy(SOCKS, ProxyAddr("155.138.142.99", 30564)),
Proxy(SOCKS, ProxyAddr("191.96.42.80", 1080)),
Proxy(SOCKS, ProxyAddr("192.252.215.5", 16137)),
Proxy(SOCKS, ProxyAddr("96.44.133.110", 58690)),
Proxy(SOCKS, ProxyAddr("98.143.145.30", 62353)),
Proxy(SOCKS, ProxyAddr("192.111.139.165", 4145)),
Proxy(SOCKS, ProxyAddr("49.12.43.80", 3128)),
Proxy(SOCKS, ProxyAddr("192.111.129.148", 4145)),
Proxy(SOCKS, ProxyAddr("192.111.129.145", 4145)),
Proxy(SOCKS, ProxyAddr("198.8.94.170", 4145)),
Proxy(SOCKS, ProxyAddr("192.111.137.37", 4145)),
Proxy(SOCKS, ProxyAddr("192.111.137.34", 4145)),
Proxy(SOCKS, ProxyAddr("104.248.63.49", 30588)),
Proxy(SOCKS, ProxyAddr("72.11.148.222", 56533)),
Proxy(SOCKS, ProxyAddr("192.111.135.18", 4145)),
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment